Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stacked, overlapping time series plots on a common y-axis with ggplot2
# 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"))
@benmarwick

This comment has been minimized.

Copy link
Owner Author

@benmarwick benmarwick commented Jul 20, 2019

image

@mrpargeter

This comment has been minimized.

Copy link

@mrpargeter mrpargeter commented Jul 26, 2019

Thanks for this Ben.

I keep encountering the following error after running your code:

Error in mmm < each : comparison of these types is not implemented

Any ideas what is causing this?

@benmarwick

This comment has been minimized.

Copy link
Owner Author

@benmarwick benmarwick commented Jul 27, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment