In this post, we will show how to use cowplot, but you can explore the features of patchwork here. When you are creating multiple plots and they do not share axes or do not fit into the facet framework, you could use the packages cowplot or patchwork (very new!), or the grid.arrange function from gridExtra. The Cookbook for R facet examples have even more to explore! Using cowplot to create multiple plots in one figure There are still other things you can do with facets, such as using space = "free". Switch = "y", # flip the facet labels along the y axis from the right side to the left labeller = as_labeller( # redefine the text that shows up for the facets c(Flow = "Flow, cfs", InorganicN = "Inorganic N, mg/L", TSS = "TSS, mg/L"))) + ylab( NULL) + # remove the word "values" theme(strip.background = element_blank(), # remove the background acement = "outside") # put labels to the left of the axis text ggplot2 with facet labels as the y axis labels ![]() if you do not want to divide the plot in the other direction. So, if you want to divide the figure along the y axis, you put variable in the data that you want to use to decide which plot data goes into as the first entry in the formula. We can do this using facet_grid and a formula syntax, y ~ x. ![]() Since the resulting three plots that we want will all share an x axis (Date), we can imagine slicing up the figure in the vertical direction so that the x axis remains in-tact but we end up with three different y axes. We could have written code to filter the data frame to the appropriate values and make a plot for each of them, but we can also take advantage of facet_grid. Now, we know that we can’t keep these different parameters on the same plot. # Now, we can look at the plot and see how it looks before we facet # Obviously, the scales are off because we are plotting flow with concentrations p # Setup plot without facets p <- ggplot(data = wi_daily_wq, aes(x = Date, y = value)) + geom_point( aes(color = site_no)) + theme_bw() # Get the data by giving site numbers and parameter codes # 00060 = stream flow, 00530 = total suspended solids, 00631 = concentration of inorganic nitrogen wi_daily_wq % select( - ends_with( "_cd")) %>% gather(key = "parameter", value = "value", -site_no, -Date) ![]() Library(dplyr) # for `rename` & `select` library(tidyr) # for `gather` library(ggplot2) Three USGS gage sites in Wisconsin were chosen because they have data for all three water quality parameters (flow, total suspended solids, and inorganic nitrogen) we are using in this example. We will download USGS water data for use in this example from the USGS National Water Information System (NWIS) using the dataRetrieval package (you can learn more about dataRetrieval in this curriculum). First, setup your ggplot code as if you aren’t faceting. Sounds like a lot, but facets can make this very simple. You want three different plots in the same figure - a timeseries for each of the parameters with different colored symbols for the different sites. You have a ame with four columns: Date, site_no, parameter, and value. Let’s start by considering a set of graphs with a common x axis. You write your ggplot2 code as if you were putting all of the data onto one plot, and then you use one of the faceting functions to specify how to slice up the graph. When you are creating multiple plots and they share axes, you should consider using facet functions from ggplot2 ( facet_grid, facet_wrap). Multiple plots in one figure using ggplot2 and facets ![]() However, there are other methods to do this that are optimized for ggplot2 plots. You may have already heard of ways to put multiple R plots into a single figure - specifying mfrow or mfcol arguments to par, split.screen, and layout are all ways to do this. While ggplot2 has many useful features, this post will explore how to create figures with multiple ggplot2 plots. In the Introduction to R class, we have switched to teaching ggplot2 because it works nicely with other tidyverse packages (dplyr, tidyr), and can create interesting and powerful graphics with little code.
0 Comments
Leave a Reply. |