Skip to content

Instantly share code, notes, and snippets.

@karthik
Created June 5, 2018 15:19
Show Gist options
  • Save karthik/2ab29cb9bf64761f4a50228748ae39ff to your computer and use it in GitHub Desktop.
Save karthik/2ab29cb9bf64761f4a50228748ae39ff to your computer and use it in GitHub Desktop.
library(ggplot2)
library(patchwork)
d1 <- runif(500)
d2 <- rep(c("Treatment", "Control"), each = 250)
d3 <- rbeta(500, shape1 = 100, shape2 = 3)
d4 <- d3 + rnorm(500, mean = 0, sd = 0.1)
plotData <- data.frame(d1, d2, d3, d4)
str(plotData)
p1 <- ggplot(data = plotData) + geom_point(aes(x = d3, y = d4))
p2 <- ggplot(data = plotData) + geom_boxplot(aes(x = d2, y = d1, fill =
d2)) +
theme(legend.position = "none")
p3 <- ggplot(data = plotData) +
geom_histogram(aes(
x = d1,
color = I("black"),
fill = I("orchid")
))
p4 <- ggplot(data = plotData) +
geom_histogram(aes(
x = d3,
color = I("black"),
fill = I("goldenrod")
))
# side by side
p1 + p2
# side by side with space
p1 + plot_spacer() + p2
# vertical
p1 / p2
# or
p1 + p2 + plot_layout(ncol = 1)
# Resize heights
p1 + p2 + plot_layout(ncol = 1, heights = c(2, 3))
# two on top, two on bottom
(p1 + p2) / (p3 + p4)
# Complex layouts
p1 + (
p2 + ( p3 + p4 + plot_layout(ncol = 1))
) + plot_layout(ncol = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment