Data Visualization Hands-on with ggplot2 × seaborn / matplotlib

From scatter plots, categories, box plots, significance asterisks and regression equations to layout, coordinate transforms and internals — R and Python side by side

Published

October 18, 2026

Introduction

English version — machine-assisted translation (by generative AI, Anthropic Claude) of the original Japanese hands-on. 日本語版はこちら.

When making figures for papers and slides, ggplot2 is the usual choice in R, and matplotlib and seaborn in Python. In both, “just drawing something” is easy, but things like

  • giving each group its own colour and keeping the legend order consistent
  • overlaying points on a box plot and drawing significance asterisks and brackets
  • writing a regression line and its equation (y = ax + b, R²) as text on a scatter plot
  • saving at the size, font and resolution required by a journal’s guidelines
  • arranging several figures with A, B, C labels

suddenly send you searching for answers. On top of that, there are problems where the figure is wrong without you noticing, such as

  • narrowing the axis with ylim() and the box plot’s median changes
  • wanting to place text “at the top right of the plot” but specifying its position in data values
  • not being able to tell from the figure whether the error bars are SD, SE or a 95% confidence interval.

In this hands-on, you will build the same figures side by side in R (ggplot2) and Python (seaborn / matplotlib), practising everything from the basics to the internal mechanisms.

Goals of this hands-on

  1. Explain the grammar of ggplot2 (data, mapping, geoms, stats, coordinates, facets, themes), matplotlib’s Figure / Axes, and where seaborn fits in
  2. Choose between scatter plots, categorical breakdowns and distributions (box plots, violins, points) according to the purpose
  3. Draw significance brackets and asterisks yourself from test results (and explain their limitations)
  4. Draw regression lines with their equation, R² and p-value in the right place, per group
  5. Adjust themes, fonts, sizes and file formats, and assemble multi-panel figures
  6. Use internal mechanisms such as coordinate transforms, statistical transformations and drawing objects to adjust figures exactly as you want

Who this is for

  • People who have drawn simple figures in either R or Python
  • People who want to learn how to draw in the other language too, or who go back and forth between both
  • People who want to control every detail of figures for presentations and papers

R and Python run in your browser

The code cells in each chapter run inside your browser (R via webR and Python via Pyodide). Edit the code and press “Run Code”, and the figure changes immediately. No installation is needed.

Each example lets you switch between R and Python using tabs like the ones below. Selecting one tab switches the other tabs on the page to the same language.

R code and figure

Python code and figure

Note
  • Right after opening each chapter, loading both R and Python takes about 1–2 minutes (Python takes longer)
  • If you move the tab to the background while it is loading, processing slows down, so please wait until it is displayed
  • Viewing on a PC is recommended
  • If the connection is unstable and loading packages fails, you will see errors such as name 'sns' is not defined. In that case, reload the page

Japanese font for Python in the browser

Python in the browser (Pyodide) has no Japanese fonts, so this hands-on bundles part of Noto Sans JP (SIL Open Font License) — the JIS level 1 and level 2 kanji, kana and alphanumerics — and registers it with matplotlib in py/setup.py. Setting up Japanese fonts on your own PC is covered in Chapter 7.

Running on your own PC

install.packages(c("ggplot2", "dplyr", "tidyr", "ggsignif", "ggrepel", "ggbeeswarm", "patchwork", "broom", "scales"))
pip install matplotlib seaborn pandas scipy statsmodels

The code in each chapter runs as is if you put R/setup.R, py/setup.py, data/ and fonts/ in the same location as this page. The in-browser Python uses matplotlib 3.8, seaborn 0.13 and pandas 2.3.

Data used

Data Contents
penguins (Palmer penguins) 344 penguins of 3 species around Palmer Station, Antarctica. Bill length and depth, flipper length, body mass, sex, island (Horst, Hill & Gorman, 2020. CC0)
nhanes (NHANES 2009–2012) 11778 people aged 20 and over from the US National Health and Nutrition Examination Survey. Height, weight, BMI, blood pressure, cholesterol, etc. (survey weights are not used, so these are not values for the US population)
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex year
Adelie Torgersen 39.1 18.7 181 3750 male 2007
Adelie Torgersen 39.5 17.4 186 3800 female 2007
Adelie Torgersen 40.3 18.0 195 3250 female 2007
Adelie Torgersen NA NA NA NA NA 2007

Chapter overview

Chapter Contents
1 Grammar of graphics: ggplot2 layers, matplotlib’s Figure / Axes, seaborn’s two kinds of functions
2 Scatter plots: colour, shape and size, colour palettes, overplotting (transparency, bins), labelling points, marginal distributions
3 Categories: ordering, facets (arranging small plots), aligning axes
4 Distributions: histograms, box plots (what they represent), violins, overlaying points, means and error bars (SD, SE, CI)
5 Significance asterisks: tests and multiple comparisons, drawing brackets and asterisks yourself, drawing them per facet
6 Regression lines and equations: confidence bands, equation / R² / p-value text, per-group equations, formatting math, Simpson’s paradox
7 Themes and saving: adjusting the look, Japanese fonts, size and resolution, vector formats
8 Multiple panels: patchwork and subplots / subplot_mosaic, panel labels, inset figures
9 Internals: statistical transformations, the axis-range trap with coordinates, matplotlib coordinate transforms, drawing objects, seaborn.objects
10 Exercises: reproduce target figures in both languages, checklist