Question about multiple plots

I was doing a guided project: Analyzing forest fire data ®. In Rstudio, I used purrrr and ggplot to plot multiple plots. However, it only showed the last graph (not the entire plots). I can use [] to find within the list of plots. But is there any way I can show all the plots at once?

Thanks.

Do you mean viewing plots within the Plots panel in RStudio? I don’t see a way to show more than one plot at a time. I thought I once used the Zoom function in the Plots pane to open one plot, then switch to a second one and zoom again (to open more than one), but now I can’t seem to reproduce that behavior, as it renders the second plot in the same pop-up window.

Alternatively, if you mean you want to render more than one plot at a time, using the layout() function might be what you’re looking for - it lets you create a grid of multiple plots in a single view. See https://www.statmethods.net/advgraphs/layout.html.

You could also code your plots in R Markdown and render them that way.

Hope this helps!
Chris

1 Like

Hi @hcho0529. It is possible to save each ggplot to a variable, and then use a package such as cowplot to save many plots to a grid. Here’s the link to documentation on cowplot. I have not tried using cowplot with output from purrr to generate many plots programatically. But doing it manually for four plots would looks something like this:

create_boxplots <- function(x, y) {
  ggplot(data = forest_fires) + 
    aes_string(x = x, y = y) +
    geom_boxplot() +
    theme(panel.background = element_rect(fill = "white"))
}

x_var_day <- names(forest_fires)[4] ## day

p1 <- create_boxplots(x = x_var_day, y = "FFMC")
p2 <- create_boxplots(x = x_var_day, y = "DMC")
p3 <- create_boxplots(x = x_var_day, y = "DC")
p4 <- create_boxplots(x = x_var_day, y = "ISI")

library(cowplot)
plot_grid(p1, p2, p3, p4, ncol = 2)
ggsave("forest_fire_month_boxplot.jpeg", height = 8, width = 8)

And this would give you a jpeg image with four plots on it like this:

I generally find the facet_wrap() function in ggplot2 to be a great way to view many plots at once in a grid. But in the case of this Guided Project, we can’t do that only using what we’ve learned up to this point in the Dataquest curriculum. The challenge we have is that the forest_fires dataframe is in a “wide” format where each data type is recorded in it’s own column. If we want to use facet_wrap() we need to make the dataframe “taller” using the gather() or pivot_longer() function from the tidyr package.

The gather() function is covered in this mission.

We can see the wide structure of the forest_fires dataframe here:

> head(forest_fires)
# A tibble: 6 x 13
      X     Y month day    FFMC   DMC    DC   ISI  temp    RH  wind  rain  area
  <dbl> <dbl> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1     7     5 mar   fri    86.2  26.2  94.3   5.1   8.2    51   6.7   0       0
2     7     4 oct   tue    90.6  35.4 669.    6.7  18      33   0.9   0       0
3     7     4 oct   sat    90.6  43.7 687.    6.7  14.6    33   1.3   0       0
4     8     6 mar   fri    91.7  33.3  77.5   9     8.3    97   4     0.2     0
5     8     6 mar   sun    89.3  51.3 102.    9.6  11.4    99   1.8   0       0
6     8     6 aug   sun    92.3  85.3 488    14.7  22.2    29   5.4   0       0

What we want to do is change the structure of the dataframe to this:

> head(forest_fires_tall)
# A tibble: 6 x 6
      X     Y month day   data_type value
  <dbl> <dbl> <chr> <chr> <chr>     <dbl>
1     7     5 mar   fri   FFMC       86.2
2     7     4 oct   tue   FFMC       90.6
3     7     4 oct   sat   FFMC       90.6
4     8     6 mar   fri   FFMC       91.7
5     8     6 mar   sun   FFMC       89.3
6     8     6 aug   sun   FFMC       92.3

What happened here is that the former column titles are now entries in the data_type column. And the values associated with each data type have been pulled into the values column into the appropriate row that corresponds with each particular entry.

The code for transforming the dataframe with the gather() function from the tidyr package looks like this:

library(tidyr)
forest_fires_tall <- forest_fires %>% 
  gather(FFMC:area, key = "data_type", value = "value")

What we are doing here is “gathering” the columns from FFMC through area, and each column header is now listed in the column we will call data_type. This is known as the key. The measurements for each entry are recorded in the column value.

This reshaping concept can seem confusing at first, but it is very helpful to use with ggplot2 because having data in a “tall” format makes plotting easier. We’ll see why in a moment. But first let’s look at the pivot_longer() function from tidyr. This function is now being used instead of gather(). You can read more about it here.

The syntax for pivot_longer() looks like this:

forest_fires_tall <- forest_fires %>% 
  pivot_longer(cols = FFMC:area, names_to = "data_type", values_to = "value")

This function is very similar to gather() but perhaps a bit more intuitive. Okay, so why is all of this data preparation useful for ggplot? It allows us to use facet_wrap() to create an individual plot for each data type (that we have called data_type here).

Adapting the ggplot syntax we used in the Guided Project, we can make a facetted plot with our taller dataset like this:

  ggplot(data = forest_fires_tall) + 
    aes(x = month, y = value) +
    geom_boxplot() +
    theme(panel.background = element_rect(fill = "white")) +
    facet_wrap(~data_type, ncol = 2)

Which returns the following grid plot:

We’re getting there, but notice that the y-axis is on the same scale for all data types, which is not very useful. We need to “free the y-axis scale” so that it is optimized for each individual plot. We do this by setting the scales = "free_y" argument within the facet_wrap() call. Finally, we can use ggsave() to save our image as a jpeg file. The syntax looks like this:

  ggplot(data = forest_fires_tall) + 
    aes(x = month, y = value) +
    geom_boxplot() +
    theme(panel.background = element_rect(fill = "white")) +
    facet_wrap(~data_type, ncol = 2, scales = "free_y") +
    ggsave("grid_plot.jpeg", height = 12, width = 9)

I played around with the height and width arguments until I found a grid that looked good. And here’s the result:

I hope this helps! Please let me know if you have any questions or would like clarification on anything. Best,
-Casey

5 Likes

Thanks @casey this was really helpful.

1 Like