Lesson 5
Crafting Multi-Plot Narratives in R
Lesson Overview: Crafting Your Data Storybook

Welcome to our storytelling session, where data comes alive on the pages of ggplot2! Today, you'll develop the ability to weave multiple data narratives together onto a single canvas. This process is much akin to assembling a scrapbook, where each photo or plot, in this case, adds depth to the story. By the end of this lesson, you'll be able to create a multi-plot narrative by using layers on the same axis and within a single figure using R syntax.

Understanding Subplots and Axes

Imagine you're building a scrapbook. Each page can hold multiple pictures, and you have the freedom to decide where each photo goes. Subplots function similarly, assisting us in positioning multiple charts within a plot grid. We'll learn how to organize our data tales neatly on a page using subplots in R, specifically a R package called cowplot.

Here's a detailed example of creating subplots:

R
1# install and load necessary libraries 2install.packages("cowplot") 3library(ggplot2) 4library(cowplot) 5 6# Create some data 7df1 <- data.frame(x = c(1, 2, 3), y = c(2, 3, 4)) 8df2 <- data.frame(x = c(1, 2, 3), y = c(1, 3, 3)) 9 10# Define two line plots 11p1 <- ggplot(df1, aes(x = x, y = y)) + 12 geom_line(colour = "blue") + 13 ggtitle("Plot 1") 14p2 <- ggplot(df2, aes(x = x, y = y)) + 15 geom_line(colour = "red") + 16 ggtitle("Plot 2") 17 18# Arrange the plots with grid arrange 19plot <- plot_grid(p1, p2, ncol = 2)

plot_grid organizes the plots within a grid. By setting ncol = 2, we arrange plots like photos on a scrapbook page, telling parts of the larger story side by side.

Plotting Students' Performance

Let's consider a more meaningful dataset. Imagine we have the data for two students' average marks and we want to compare their performance using plots:

R
1first_student_marks <- c(3.8, 3.9, 3.8, 4.1, 4.4, 4.2, 4.5, 4.5, 4.7) 2second_student_marks <- c(4, 3.9, 4.1, 4.1, 4.1, 3.9, 3.8, 3.7, 3.5) 3semesters <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)

Here's how we plot these graphs:

R
1studentA <- data.frame(semester = semesters, marks = first_student_marks) 2studentB <- data.frame(semester = semesters, marks = second_student_marks) 3 4p1 <- ggplot(studentA, aes(x = semester, y = marks)) + 5 geom_line() + 6 ylim(c(3, 5)) + 7 ggtitle("Student A") 8p2 <- ggplot(studentB, aes(x = semester, y = marks)) + 9 geom_line() + 10 ylim(c(3, 5)) + 11 ggtitle("Student B") 12 13plot <- plot_grid(p1, p2, ncol = 2)

By using a common y-axis limit with ylim, we make the comparison more apparent.

Layering Plots on the Same Axis

There is another way to compare these plots.

We can overlay plots on the same axis in ggplot2, directly comparing different datasets. To put two plots on the same axis, we simply add a second geom_line() call to the plot!

R
1student_data <- data.frame(semester = rep(semesters, 2), 2 marks = c(first_student_marks, second_student_marks), 3 student = rep(c('studentA', 'studentB'), each = length(semesters))) 4 5ggplot(student_data, aes(x = semester, y = marks, color = student)) + 6 geom_line() + 7 scale_color_manual(values = c('purple', 'orange')) + 8 ggtitle("Students' Performances")

Let's break this down:

First, we combine the data into a single data.frame. This is done to make it easier to plot both students' performances on the same graph.

  • semester = rep(semesters, 2) repeats the semesters vector twice, once for each student.
  • marks = c(first_student_marks, second_student_marks) concatenates the marks of both students into a single vector.
  • student = rep(c('studentA', 'studentB'), each = length(semesters)) creates a vector indicating which marks belong to which student.

Next, we create a plot using ggplot(). Important things to note:

  • x = semester and y = marks map the semesters to the x-axis and the marks to the y-axis.
  • color = student differentiates the lines by color based on the student.
  • The scale_color_manual(values = c('purple', 'orange')) function manually sets the colors for the lines

The result allows us to compare the performances of two students on the same plot.

Wrapping Up: Your Data Storybook Complete

Bravo! You're now equipped to craft a multi-layered data storybook with plots on the same axis and diverse visuals collected in a single figure. Just as a scrapbook contains different pictures and embellishments on a page, you've learned to use plot_grid and ggplot() to arrange different data plots, creating multifaceted visual narratives in R with ggplot2 and cowplot.

As we close this chapter, prepare to apply your skills in the next round of practice exercises. By creating your own data stories, you'll solidify your understanding of these techniques and begin to comprehend the bigger picture in your data. Your journey into eloquent data storytelling has only just begun!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.