Description Usage Arguments Value Examples
View source: R/Helper_Functions.R
Combines multiple plots generated by ggplot2 into a 1 column (vertical) or 1 row (horizontal) panel with a shared legend.
1 | ggpanel(plots, orientation = "horizontal", legend.position = "bottom")
|
plots |
A list of ggplot2 plots |
orientation |
A character of "horizontal" or "vertical", defaults to 'horizontal' |
legend.position |
A character vector of "top", "bottom", "left", or "right", defaults to 'bottom'; to remove legend use "none" |
A gtable object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | # Create datasets for plotting
data1 <-
data.frame(x = c(1:5, 1:5, 1:5, 1:5),
treatment = rep(c(letters[1:2]),
each = 5,
times = 2),
year = rep(c("2015", "2016"),
each = 10),
power = c(rep(2, times = 5),
rep(2.1, times = 5),
rep(2.5, times = 5),
rep(3, times = 5))) %>%
mutate(y = rnorm(10, mean = 0, sd = 2) + x ^ power)
data2 <-
data.frame(x = c(1:5, 1:5, 1:5, 1:5),
treatment = rep(c(letters[1:2]),
each = 5,
times = 2),
year = rep(c("2015", "2016"),
each = 10),
slope = c(rep(3, times = 5),
rep(4, times = 5),
rep(3, times = 5),
rep(7, times = 5))) %>%
mutate(y = rnorm(20, mean = 0, sd = 2) + x * slope)
# Create two plots
plot1 <-
ggplot(data1,
aes(x, y, color = treatment)) +
geom_point() +
geom_smooth(span = 1, se = F) +
facet_grid(.~year)
plot2 <-
ggplot(data2,
aes(x, y, color = treatment)) +
geom_point() +
geom_smooth(method = "lm", span = 1, se = F) +
facet_grid(.~year)
# Generate a Panel
ggpanel(list(plot1, plot2),
orientation = "vertical",
legend.position = "right")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.