styler
packagescale()
Sometimes it is convenient to plot multiple sets of data at once. This can be accomplished in two ways: facet wrapping to separate out different subsets of a dataset into plots with shared axes, and adding multiple plots to the same figure.
Facet wrapping separates subsets of a dataset into plots with identical axes. This can be useful if different groups of data points overlap and occlude one another. Consider the following dataset with three groups of normally distributed random samples:
library(mvtnorm) # package implementing multivariate normal distributions
<- 100
nsamp <- rbind(
data rmvnorm(nsamp,c(1,1),sigma=matrix(c(1,0.8,0.8,1),nrow=2)),
rmvnorm(nsamp,c(1,1),sigma=matrix(c(1,-0.8,-0.8,1),nrow=2)),
rmvnorm(nsamp,c(1,1),sigma=matrix(c(1,0,0,1),nrow=2))
)colnames(data) <- c('x','y')
<- as_tibble(data) %>%
g_oneplot mutate(
sample_name=c(rep('A',nsamp),rep('B',nsamp),rep('C',nsamp))
%>%
) ggplot(aes(x=x,y=y,color=sample_name)) +
geom_point()
g_oneplot
The three sample data points are plotted on top of each other, and although they
are colored differently, it is difficult to discern the patterns of each group
clearly. This same plot may be split into three, one for each sample group,
using the facet_wrap()
function:
+ facet_wrap(vars(sample_name)) g_oneplot
Now the trends of each group are clearly visible. The facet_wrap()
function
accepts one required argument, which is the variable name in the data that
should be used to separate the groups decorated by the vars()
function. Any
geometry may be used with a facet wrap. For example, we could add a
geom_smooth()
to the plot to estimate a smoothing function within each facet:
+ facet_wrap(vars(sample_name)) +
g_oneplot geom_smooth(method="loess", formula=y ~ x)
By default, facet wrapping fixes all the axes of each plot to be identical, which enables the most accurate estimates of comparisons between plots according to the Elementary Perceptual Tasks hierarchy. This and many other aspects of the faceting may be adjusted using arguments as described in the documentation.
Facet wrapping is useful when all of the data for the separate plots are on the
same scale, and comaprisons of the facet groups is meaningful. However, faceting
is less convenient when we desire to place plots with unrelated data in the same
figure. The patchwork
package allows
us to compose multiple plots together using an intuitive set of operators. For
example, we may put two plots next to each other by separating them with the |
operator:
<- tibble(
data a=rnorm(100,0,1),
b=rnorm(100,3,2)
)<- ggplot(data, aes(x=a, y=b)) +
g_scatter geom_point()
<- pivot_longer(data, c(a,b)) %>% ggplot(aes(x=name,y=value,fill=name)) +
g_violin geom_violin()
| g_violin g_scatter
Note that each plot was saved to its own variable and then composed with the
patchwork operator |
, which puts the plots side-by-side. We could also place
one plot on top of the other with the /
operator:
/ g_violin g_scatter
Plots may be composed into arbitrarily complex multipanel figures in this way
using ()
to group plots together:
/ ( g_scatter | g_violin) g_scatter
Another possible composition:
/ g_scatter ) | g_violin (g_scatter
The patchwork library is very flexible, and allows the designer to compose plots in many different ways. These multipanel plots make excellent starting points for producing publication-quality figures, as described in the next section.