Skip to Content
Course content

23: Introduction to ggplot2

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're an artist preparing to paint a landscape. You don't just start throwing paint at a wall. First, you get your canvas ready. Then, you sketch out where the horizon line goes and where the mountains will sit—this is your layout, but there's no "art" on it yet. Finally, you start layering: a wash of blue for the sky, some green jagged shapes for the hills, and maybe some tiny white dots for distant sheep. Each layer adds a new dimension to the image without erasing what you did before.

This is exactly how ggplot2 works. It's based on the "Grammar of Graphics," which is a fancy way of saying that every plot is just a series of layers stacked on top of each other.

Setting the Stage with ggplot()

In the R world, the ggplot() function is your blank canvas. By itself, it does absolutely nothing. If you run ggplot(data = my_data), R will just give you a grey void. That's because you've told it which dataset to use, but you haven't told it where the "horizon" is.

To fix that, we use aes(), short for aesthetics. This is where you map your data variables to visual properties. Think of it as the sketch phase: "I want the X-axis to represent time and the Y-axis to represent revenue."

# Let's imagine we have a dataset of coffee sales
coffee_sales <- data.frame(
  day = 1:10,
  revenue = c(120, 150, 110, 180, 200, 170, 210, 250, 230, 280)
)

# This sets the canvas and the sketch, but still shows nothing!
ggplot(data = coffee_sales, aes(x = day, y = revenue))

Choosing Your Visual Layer

Now that the canvas is prepped, we need to actually "paint" the data. We do this using geom functions (short for geometric objects). Depending on what you want to see, you'll pick a different geom. If you want a scatter plot, you use geom_point(). If you want a line chart, you use geom_line().

I've seen a lot of beginners try to put the aes() inside the geom function. You can do that, but it's usually better to keep the global mappings in the main ggplot() call so all your layers share the same coordinate system. It just keeps your code cleaner.

# Adding the 'paint' to our canvas
ggplot(data = coffee_sales, aes(x = day, y = revenue)) + 
  geom_point() + 
  geom_line()

The Magic of the Plus Sign

You probably noticed that + symbol. In most of R, + is for math. In ggplot2, it's the "layering" operator. It tells R, "Take the plot I just defined, and then add this next thing on top of it."

This is where ggplot2 becomes incredibly powerful. You can keep stacking. Want to change the labels? Add labs(). Want to change the theme to something professional? Add theme_minimal(). It's like adding a frame to your painting and signing your name at the bottom.

ggplot(data = coffee_sales, aes(x = day, y = revenue)) + 
  geom_point(color = "steelblue", size = 3) + 
  geom_line(color = "darkgrey") + 
  labs(title = "Daily Coffee Revenue", 
       x = "Day of Month", 
       y = "Total Revenue ($)") + 
  theme_minimal()

One quick tip: if your plot looks "off," check your parentheses. A common frustration is putting the + at the start of a new line instead of the end of the previous one. R reads code line-by-line; if the line ends without a +, R thinks the plot is finished and will ignore everything that follows.




📋 Practical Task

Visualizing Monthly Temperature Fluctuations in London

You've been given a small dataset representing the average monthly temperatures in London. Your goal is to create a professional-looking visualization that shows the trend over the year.

The Data:

london_temps <- data.frame(
  month = factor(month = 1:12, levels = 1:12),
  temp = c(5, 6, 9, 12, 15, 18, 21, 20, 17, 13, 9, 6)
)

Your Requirements:

  • Initialize the plot using london_temps, mapping month to the x-axis and temp to the y-axis.
  • Add a layer of points to show the individual monthly data.
  • Add a layer of lines to connect those points.
  • Change the color of the points to "red" and the line to "blue".
  • Add a clear title: "Average Monthly Temperature in London" and label the axes as "Month" and "Temperature (°C)".
  • Apply theme_light() to finish the look.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.