ggplot2 Fundamentals

ggplot2 is R's most popular plotting package, built on the "grammar of graphics" — you describe a chart as data, aesthetic mappings, and geometric layers, then build it up with +.

Learn ggplot2 Fundamentals in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll create scatter plots and bar charts, map columns to aesthetics with aes(), add layers like trend lines and labels, and apply a clean theme.

What You'll Learn in This Lesson

1️⃣ Your First Plot: Data + aes + geom

Every plot starts with ggplot(data, aes(...)) to set the data and mappings, then adds at least one geom_ layer to draw it. geom_point() makes a scatter plot.

2️⃣ Adding Layers

Plots grow by adding layers with + . Style a geom directly (colour, size), overlay a trend line with geom_smooth() , label with labs() , and clean up with a theme.

3️⃣ Bar Charts: geom_bar vs geom_col

Use geom_bar() to count how many rows fall in each category, and geom_col() when you already have the value to plot per category.

Your turn. Fill in the # TODO blank, run it, and check the Plots pane.

Write it from the outline using the built-in mtcars data, run it in RStudio, and check the plot. Mapping colour to a factor is how you reveal groups in a scatter.

📋 Quick Reference — ggplot2

Practice quiz

Which three parts make up every ggplot?

  • Rows, columns, cells
  • Title, axes, legend
  • Mean, median, mode
  • Data, aesthetic mappings, and a geom layer

Answer: Data, aesthetic mappings, and a geom layer. A ggplot is built from data, aes() mappings, and one or more geom layers.

What does aes() do?

  • Maps data columns to visual properties like x, y, colour
  • Saves the plot
  • Loads the package
  • Sets the file size

Answer: Maps data columns to visual properties like x, y, colour. aes() connects data columns to aesthetics such as x, y, and colour.

Which geom draws a scatter plot?

  • geom_bar()
  • geom_point()
  • geom_line()
  • geom_col()

Answer: geom_point(). geom_point() plots individual points for a scatter plot.

How do you add layers to a ggplot?

  • With the | operator
  • With commas
  • With the + operator
  • With %>%

Answer: With the + operator. ggplot2 stacks layers with +, each on the end of the previous line.

What is the difference between geom_bar() and geom_col()?

  • They are identical
  • geom_bar is for lines, geom_col for points
  • geom_bar counts rows per category; geom_col uses a supplied y value
  • geom_col counts rows; geom_bar uses a y value

Answer: geom_bar counts rows per category; geom_col uses a supplied y value. geom_bar() counts frequencies; geom_col() draws bars from a y value you provide.

Which function loads ggplot2 for the session?

  • library(ggplot2)
  • import ggplot2
  • load(ggplot2)
  • use(ggplot2)

Answer: library(ggplot2). library(ggplot2) attaches the package; install once with install.packages.

To set ONE fixed colour for all points, where does colour go?

  • Inside aes()
  • Outside aes(), in the geom, e.g. geom_point(color = "blue")
  • In labs()
  • In a theme() call

Answer: Outside aes(), in the geom, e.g. geom_point(color = "blue"). A constant style goes outside aes(); only data-driven colour goes inside aes().

Which layer adds a linear best-fit trend line?

  • geom_trend()
  • geom_fit()
  • geom_smooth(method = "lm")
  • geom_line(lm = TRUE)

Answer: geom_smooth(method = "lm"). geom_smooth(method = "lm") overlays a straight-line fit.

A common beginner mistake with + is:

  • Putting + at the START of the next line instead of the end of the previous one
  • Using + only once
  • Using + with geoms only
  • Never needing +

Answer: Putting + at the START of the next line instead of the end of the previous one. The + must END the previous line, or R thinks the plot is finished.

Which function sets the plot title and axis labels?

  • title()
  • theme()
  • aes()
  • labs()

Answer: labs(). labs(title = ..., x = ..., y = ...) sets the plot's text labels.