Migration from R¶
This guide helps R users of the MSUthemes R package transition to the Python version.
Overview¶
The Python implementation of MSUthemes closely mirrors the R package's functionality, making migration straightforward. The main differences are syntax-related due to Python vs R conventions.
Package Comparison¶
| Feature | R Package | Python Package |
|---|---|---|
| Installation | install.packages("MSUthemes") |
pip install msuthemes |
| Import | library(MSUthemes) |
import msuthemes or from msuthemes import ... |
| Colors | msu_green, msu_white |
colors.MSU_GREEN, colors.MSU_WHITE |
| Palettes | msu_palettes$seq |
palettes.msu_seq |
| Theme | theme_msu() |
theme_msu() |
| Big Ten | bigten_colors("MSU") |
get_bigten_colors("MSU") |
| Dataset | bigten (data.frame) |
load_bigten_data() (DataFrame) |
Installation¶
Loading the Package¶
Using Colors¶
Color Palettes¶
Applying Themes¶
Big Ten Colors¶
Working with Data¶
Complete Example¶
R Version¶
library(MSUthemes)
library(ggplot2)
library(dplyr)
# Load and filter data
data(bigten)
rivalry <- bigten %>%
filter(name %in% c("Michigan State", "Michigan", "Ohio State"))
# Get school colors
school_colors <- bigten_palette(c("MSU", "Michigan", "Ohio State"))
# Create plot
ggplot(rivalry, aes(x = entry_term, y = ADM_RATE * 100,
color = name)) +
geom_line(linewidth = 1.5) +
scale_color_manual(values = school_colors) +
labs(
title = "Big Ten Admission Rates Over Time",
x = "Year",
y = "Admission Rate (%)",
color = "Institution"
) +
theme_msu(use_grid = TRUE)
Python Version¶
from msuthemes import (
theme_msu,
get_bigten_colors,
load_bigten_data
)
import matplotlib.pyplot as plt
# Apply theme
theme_msu(use_grid=True)
# Load and filter data
rivalry = load_bigten_data(
institutions=['MSU', 'Michigan', 'Ohio State'],
columns=['name', 'entry_term', 'ADM_RATE']
)
# Get school colors
school_colors = get_bigten_colors(['MSU', 'Michigan', 'Ohio State'])
# Create plot
fig, ax = plt.subplots(figsize=(10, 6))
for school in ['MSU', 'Michigan', 'Ohio State']:
school_data = rivalry[rivalry['name'] == school]
ax.plot(
school_data['entry_term'],
school_data['ADM_RATE'] * 100,
label=school,
color=school_colors[school],
linewidth=1.5
)
ax.set_xlabel('Year')
ax.set_ylabel('Admission Rate (%)')
ax.set_title('Big Ten Admission Rates Over Time')
ax.legend(title='Institution')
plt.show()
Key Differences¶
Naming Conventions¶
| R Convention | Python Convention |
|---|---|
snake_case for functions |
snake_case for functions |
snake_case for variables |
UPPER_CASE for constants |
. for list access |
. for attribute access |
$ for list/dataframe columns |
. for attributes, [] for dict keys |
Data Structures¶
| R | Python |
|---|---|
data.frame |
pandas.DataFrame |
list |
dict or list |
vector |
list or numpy.array |
c() |
[] |
Plotting Libraries¶
| R | Python |
|---|---|
ggplot2 |
matplotlib (primary) |
| Base R graphics | matplotlib |
| - | seaborn (statistical) |
| - | plotly (interactive) |
Common Gotchas¶
Theme Application
- R: Theme is applied per-plot with
+ theme_msu() - Python: Theme is applied globally with
theme_msu()before creating plots
Color Names
- R:
msu_green(lowercase, no quotes) - Python:
colors.MSU_GREEN(uppercase constant)
Palette Access
- R:
msu_palettes$seq(dollar sign) - Python:
palettes.msu_seq(dot notation)
Data Loading
- R:
data(bigten)loads into environment - Python:
df = load_bigten_data()returns DataFrame
Tips for R Users Learning Python¶
- Indexing: Python uses 0-based indexing (R uses 1-based)
- Indentation: Python requires proper indentation (no
{}) - Lists: Use
[]for lists, notc() - Assignment: Use
=for assignment (though<-enthusiasts exist!) - Help: Use
help(function_name)or?function_namein IPython
Additional Resources¶
Getting Help¶
If you encounter issues during migration:
- Check the API Reference
- See the Gallery for Python examples
- Open an issue on GitHub