Last active
July 28, 2019 01:24
-
-
Save benmarwick/82776e452d61cfbd44972ae6a4c23c21 to your computer and use it in GitHub Desktop.
Stacked, overlapping time series plots on a common y-axis with ggplot2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# starting with code from | |
# https://gist.github.com/tomhopper/faa24797bb44addeba79?fbclid=IwAR3FFiHpUhTESqi_ylASZP5-zYD0lOrQyyHM2MSEvnVRCIykmjNREGTv4Uk | |
library(ggplot2) | |
library(grid) | |
library(dplyr) | |
#' Create some data to plot | |
n <- 100 | |
df <- data.frame(DateTime = seq(10, 20, length.out = 100), | |
series1 = rnorm(n), | |
series2a = rnorm(n), | |
series2b = rnorm(n), | |
series3 = rnorm(n)) | |
# the plot at the top | |
plot1 <- df %>% | |
select(DateTime, series1) %>% | |
na.omit() %>% | |
ggplot() + | |
geom_line(aes(x = DateTime, y = series1), size = 0.5, alpha = 0.75, colour = "red") + | |
ylab("Red") + | |
theme_minimal() + | |
scale_y_continuous(position = "right", | |
limits = c(-3,3)) + | |
theme(axis.line.y = element_line(color="red", size = 0.5), | |
axis.title.x=element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
panel.grid.minor.y=element_blank(), | |
panel.grid.major.y=element_blank(), | |
plot.margin=unit(c(0, # top | |
0, # right | |
-1, # bottom | |
0), # left | |
"cm")) | |
# the plots in the middle | |
plot2a <- df %>% | |
select(DateTime, series2a) %>% | |
na.omit() %>% | |
ggplot() + | |
geom_line(aes(x = DateTime, y = series2a), size = 0.5, alpha = 0.75, colour = "green") + | |
ylab("Green") + | |
theme_minimal() + | |
scale_y_continuous(limits = c(-3,3)) + | |
theme(axis.line.y = element_line(color="green", size = 0.5), | |
axis.title.x = element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
panel.grid.minor.y=element_blank(), | |
panel.grid.major.y=element_blank(), | |
plot.margin=unit(c(-1, # top | |
0, # right | |
-1, # bottom | |
0), # left | |
"cm")) | |
# another plot in the middle | |
plot2b <- df %>% | |
select(DateTime, series2b) %>% | |
na.omit() %>% | |
ggplot() + | |
geom_line(aes(x = DateTime, y = series2b), size = 0.5, alpha = 0.75, colour = "yellow") + | |
ylab("Yellow") + | |
theme_minimal() + | |
scale_y_continuous(position = "right", | |
limits = c(-3,3)) + | |
theme(axis.line.y = element_line(color="yellow", size = 0.5), | |
axis.title.x = element_blank(), | |
axis.text.x=element_blank(), | |
axis.ticks.x=element_blank(), | |
panel.grid.minor.y=element_blank(), | |
panel.grid.major.y=element_blank(), | |
plot.margin=unit(c(-1, # top | |
0, # right | |
-1, # bottom | |
0), # left | |
"cm")) | |
# the plot at the bottom | |
plot3 <- df %>% | |
select(DateTime, series3) %>% | |
na.omit() %>% | |
ggplot() + | |
geom_line(aes(x = DateTime, y = series3), size = 0.5, alpha = 0.75, colour = "blue") + | |
ylab("Blue") + | |
xlab("Age (cal k BP)") + | |
theme_minimal() + | |
scale_y_continuous(limits = c(-3,3)) + | |
theme(axis.line.y = element_line(color="blue", size = 0.5), | |
axis.title.x = element_blank(), | |
panel.grid.minor.y=element_blank(), | |
panel.grid.major.y=element_blank(), | |
plot.margin=unit(c(0, # top | |
0, # right | |
0, # bottom | |
0), # left | |
"cm")) | |
grid.newpage() | |
grid.draw(rbind(ggplotGrob(plot1), | |
ggplotGrob(plot2a), | |
ggplotGrob(plot2b), | |
ggplotGrob(plot3), | |
size = "max")) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Justin, I'm not sure about that error, but my guess is that maybe some of your pkgs are not the most recent releases? I made this where you can run this code in your browser and explore it further: https://rstudio.cloud/project/424276