Skip to content

DAAF-Contribution-Community/daaf / .claude/skills/python-r-translation

python-r-translation

Verified

Python-to-R translation for data analysis. Maps Python (polars, plotnine, pyfixest, statsmodels, svy, geopandas) to R (tidyverse, ggplot2, fixest, survey, sf). Use when user has Python background or requests Python-equivalent code comments in R pipelines.

references/visualization.md

View on GitHub
# Visualization: R Explained for Python Users

This reference covers R visualization code explained for Python users. The core
mapping is **ggplot2 (R) for plotnine (Python) users** and **plotly R for plotly
Python users**. ggplot2 and plotnine share the same grammar of graphics -- the
translation is syntactic, not conceptual.

> **Versions referenced:**
> R: ggplot2 4.0.2, plotly 4.12.0
> Python: plotnine 0.15.3, plotly 6.5.2
> See SKILL.md § Library Versions for the complete version table.

---

## Section 1: ggplot2 for plotnine Users

plotnine is a near-1:1 port of ggplot2. The grammar is identical: `ggplot()`,
`aes()`, `geom_*()`, `scale_*()`, `facet_*()`, `theme_*()`, composed with `+`.

### Key Syntax Differences

| ggplot2 (R) | plotnine (Python) |
|-------------|-------------------|
| Bare names: `aes(x = year, y = value)` | Strings: `aes(x="year", y="value")` |
| Formula in facets: `facet_wrap(~ var)` | String: `facet_wrap("var")` |
| `library(ggplot2)` | `from plotnine import *` |
| `ggsave("file.png", p, width=10, height=8)` | `p.save("file.png", width=10, height=8, dpi=300)` |
| `face = "bold"` in theme | `weight = "bold"` |
| Dots in theme names: `panel.grid.major` | Underscores: `panel_grid_major` |

### Complete Parallel Example

```r
# R (ggplot2) -- you know this from plotnine
p <- ggplot(mtcars, aes(x = wt, y = mpg, color = factor(cyl))) +
  geom_point(size = 3) +
  geom_smooth(method = "lm", se = FALSE) +
  scale_color_brewer(palette = "Set1") +
  labs(title = "Fuel Efficiency vs Weight", x = "Weight", y = "MPG") +
  theme_minimal() +
  theme(plot.title = element_text(size = 16, face = "bold"))

ggsave("plot.png", p, width = 10, height = 8)
```

```python
# Python (plotnine) -- what you know
p = (
    ggplot(mtcars, aes(x="wt", y="mpg", color="factor(cyl)"))
    + geom_point(size=3)
    + geom_smooth(method="lm", se=False)
    + scale_color_brewer(palette="Set1")
    + labs(title="Fuel Efficiency vs Weight", x="Weight", y="MPG")
    + theme_minimal()
    + theme(plot_title=element_text(size=16, weight="bold"))
)
p.save("plot.png", width=10, height=8, dpi=300)
```

Three differences: (1) bare names vs strings in `aes()`, (2) `face="bold"` vs
`weight="bold"`, (3) dots vs underscores in theme element names.

---

## Section 2: Geoms, Scales, and Faceting

Most geoms, scales, and faceting functions are **identical** between ggplot2 and
plotnine. All `geom_point()`, `geom_line()`, `geom_bar()`, `geom_histogram()`,
`geom_boxplot()`, `geom_smooth()`, `geom_tile()`, etc. work the same way.

**R-only geoms:** `geom_sf()` (spatial) exists in ggplot2 but not plotnine.
Use `geom_map()` with a GeoDataFrame in plotnine instead.

### Scales

| ggplot2 (R) | plotnine (Python) |
|-------------|-------------------|
| `scale_color_viridis_c()` | `scale_color_cmap(cmap_name="viridis")` |
| `scale_fill_viridis_c()` | `scale_fill_cmap(cmap_name="viridis")` |
| `scale_y_continuous(labels = scales::dollar)` | `scale_y_continuous(labels=lambda x: [f"${v:,.0f}" for v in x])` |

R's `scales` package helpers (`dollar`, `percent`, `comma`) have no direct plotnine
equivalents. Use lambda functions.

### Faceting

```r
# R
facet_wrap(~ state, ncol = 3, scales = "free_y")
facet_grid(sector ~ year)
```

```python
# Python
facet_wrap("state", ncol=3, scales="free_y")
facet_grid("sector ~ year")
```

Formula (`~`) becomes string in plotnine.

---

## Section 3: plotly R for plotly Python Users

| plotly R | plotly Python |
|----------|---------------|
| `plot_ly(df, x = ~year, y = ~value)` | `px.scatter(df, x="year", y="value")` |
| `~column_name` (tilde formula) | `"column_name"` (string) |
| `list(title = "X")` | `dict(title="X")` |
| `TRUE` / `FALSE` | `True` / `False` |
| `%>% layout(...)` | `.update_layout(...)` |
| `htmlwidgets::saveWidget(p, "f.html")` | `fig.write_html("f.html")` |

R uses tilde formula for variable references (`~column_name`), Python uses strings.
R's `list()` maps to Python's `dict()`.

---

## Section 4: Coefficient and Effect Plots

| R (fixest) | Python (pyfixest) |
|-----------|-------------------|
| `coefplot(fit)` | `pf.coefplot(fit)` |
| `coefplot(list(fit1, fit2))` | `pf.coefplot([fit1, fit2])` |
| `iplot(fit)` | `fit.iplot()` |
| `iplot(fit, joint = TRUE)` | `fit.iplot(joint="both")` |

pyfixest's plot functions are functionally equivalent to fixest's.

---

## Section 5: Patchwork (Multi-Panel Layouts)

```r
# R (patchwork)
library(patchwork)
(p1 | p2 | p3) / p4
```

```python
# Python (plotnine v0.15+) -- same operators
(p1 | p2 | p3) / p4
```

plotnine now supports patchwork-style composition with identical operators.

---

## Quick Reference

| Need | R Tool | Python Tool |
|------|--------|-------------|
| Static grammar-of-graphics | ggplot2 | plotnine |
| Interactive chart | plotly R | plotly Python |
| Coefficient plot | fixest::coefplot | pf.coefplot() |
| Event study plot | fixest::iplot | fit.iplot() |
| Regression table | etable / modelsummary | pf.etable() |
| Combining plots | patchwork | plotnine `|` and `/` |
| Spatial / map | ggplot2 + geom_sf | geopandas .plot() |

> **Sources:** Wickham, H., Navarro, D., & Pedersen, T. L., *ggplot2: Elegant Graphics for Data Analysis*, 3rd ed. (online work-in-progress edition, Springer; ggplot2-book.org, accessed 2026-10-13);
> plotnine documentation (plotnine.org, accessed 2026-03-28)
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 319kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 196kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall