Learnr

Learn ggplot2

Build charts in R by adding layers, the grammar of graphics way.

Free to start. No credit card. Or browse all courses.

What's inside your ggplot2 course

A sample path Learnr builds for ggplot2. Your real course adapts to your goal and level.

  1. The grammar of graphics and your first ggplot
  2. Aesthetics and mappings versus fixed settings
  3. Geoms and statistical transformations
  4. Facets for small multiples
  5. Scales, coordinates, labels and themes
  6. Tidy data, reshaping and saving with ggsave

ggplot2 is the plotting package at the heart of the tidyverse in R, and it works differently from most charting tools. Instead of picking a chart type from a menu, you describe a plot: which data, which variables map to x, y, color or size (the aesthetics), and which geometric objects draw them. Layers are joined with a plus sign, so a scatter plot becomes a scatter plot with a trend line by adding geom_smooth. This grammar of graphics makes complex figures surprisingly short once it clicks. Learnr teaches ggplot2 in small steps with code you read, predict and modify, building one layer at a time.

How to learn ggplot2, step by step

  1. Learn the three-part template. Every plot starts as ggplot(data, aes(x, y)) plus at least one geom. Type it out with the built-in mpg or penguins data until the pattern is automatic, then vary only one piece at a time to see what it controls.
  2. Understand mapping versus setting. color = class inside aes() maps a variable to color and creates a legend. color = "blue" outside aes() just paints everything blue. Getting these backwards is the classic early bug, so test both versions deliberately.
  3. Stack layers and meet stats. Add geom_smooth on top of geom_point, then learn that geom_bar counts rows by default while geom_col plots values you already have. Knowing which stat a geom uses explains most surprising bar charts.
  4. Get your data tidy first. ggplot2 expects one row per observation and one column per variable. Use tidyr pivot_longer to turn wide year columns into a year column and a value column, and many hard plots become a single line.
  5. Use facets, scales and themes to polish. Try facet_wrap(~ variable) for small multiples, scale_y_log10 for skewed data, labs() for titles and theme_minimal() as a clean base. Save with ggsave at an explicit width, height and dpi.
  6. Rebuild a chart a day. Copy charts from TidyTuesday submissions or the R Graph Gallery and recreate them from scratch. Pair that with short daily review in a Learnr course so function names and arguments stay at your fingertips.

What you'll learn

  • Write a ggplot call that maps variables to aesthetics inside aes()
  • Layer geoms such as points, lines, bars, boxplots and smoothers
  • Split a plot into small multiples with facet_wrap and facet_grid
  • Adjust scales, axis labels, color palettes and themes
  • Reshape data to long format so ggplot2 can plot it easily

How long does it take to learn ggplot2?

With basic R, most learners make solid exploratory plots within a week or two of practice. Custom themes, annotated publication charts and tricky reshaping usually take 1 to 3 months. Starting R from zero adds a few weeks for data frames and the pipe.

What you need before you start

Basic R: installing packages, reading a CSV, data frames and the pipe. Knowing a little dplyr for filtering and summarizing helps, because you usually prepare data before plotting it.

Common mistakes to avoid

  • Joining layers with the pipe instead of a plus sign. Pipes pass data between dplyr steps, while ggplot2 layers are added with +.
  • Plotting wide data by writing one geom_line per column. Reshape to long format and map the old column names to color instead.
  • Reaching for a dual y axis or a pie chart by habit. Facets or a simple bar chart usually communicate the comparison more honestly.

How Learnr builds your course

Type your topic

Tell Learnr you want to learn ggplot2, or get as specific as you like.

Get a personalized course

Learnr instantly builds a full ggplot2 course tailored to your goal and skill level.

Learn and level up

Move through bite-sized lessons and interactive practice, earning XP and keeping your streak.

ggplot2 questions, answered

Why is it called ggplot2?

It is based on Leland Wilkinson's book The Grammar of Graphics, and it is the second version of the package, rewritten by Hadley Wickham after an earlier ggplot. The name has stuck even though there is no current ggplot1.

Can I use ggplot2 in Python?

The R package itself runs in R, but the plotnine library for Python follows the same grammar closely, so the concepts transfer almost directly.

Is ggplot2 good for interactive charts?

It makes static charts. For interactivity, packages like plotly (via ggplotly) or ggiraph can wrap a ggplot, and Shiny can put it inside a web app.

Explore more courses

More data and ai courses

See all data and ai courses

Ready to learn ggplot2?

Start your free, personalized ggplot2 course in seconds. It will be typed in and ready when you get there.

Start learning ggplot2

Free to start. No credit card.