Skip to content

Instantly share code, notes, and snippets.

@johnDorian
Created November 7, 2014 10:27
Show Gist options
  • Save johnDorian/b385ae8edfdf344c6a87 to your computer and use it in GitHub Desktop.
Save johnDorian/b385ae8edfdf344c6a87 to your computer and use it in GitHub Desktop.
An example of using rbind_ggplot
library(ggplot2)
library(lubridate)
libarry(ggthemes)
# Create a series of dates (hourly)
hourly_date <- seq.POSIXt(dmy("01012012"), dmy("31122012"), by = "1 hour")
# Create some fictional fdom data.
fdom <- data.frame(date = hourly_date, fdom = sin(1:length(hourly_date)/300)+10)
# Create a series of dates (daily)
daily_date <- seq.POSIXt(dmy("01012012"), dmy("31122012"), by = "1 day")
# Sample the daily dates (take 30 samples for the year)
samples <- sample(daily_date, 30)
# order the dates
sampels <- sort(samples)
doc <- data.frame(date = samples, doc = rnorm(30, 10, 3))
library(scales)
# Create the fdom plot (Need to scale the x axis sto ensure the start and end are the same on both plots.)
plot1 <- ggplot() + geom_line(aes(x=date, y=fdom), fdom) + theme_few(20) +
ylab("FDOM (QSU)") +
scale_x_datetime(limits = c(ymd("2012-01-01"),ymd("2012-12-31")))
plot1
# Create the doc plot
plot2 <- ggplot() + geom_point(aes(x=date, y= doc), doc) + theme_few(20) +
ylab(expression(paste("DOC (mg",L^{-1},")"))) +
scale_x_datetime(limits = c(ymd("2012-01-01"),ymd("2012-12-31")))
plot2
# Now to combine the two plots together.
library(gridExtra)
library(gtable)
grid.arrange(plot1, plot2, ncol=1)
# The problem is that the plots are not aligned.
# The following function will take care of that.
rbind_ggplot <- function(...){
## Taken and modified from http://stackoverflow.com/questions/13294952/
ggplots <- list(...)
if(!all(sapply(ggplots, is.ggplot))){
stop("All objects must be of class ggplot (see ?is.ggplot)")
}
gtl <- lapply(ggplots, ggplotGrob)
bind2 <- function (x, y)
{
if(ncol(x) != ncol(y)){
stop("plots have incorrect dimensions")
}
if (nrow(x) == 0)
return(y)
if (nrow(y) == 0)
return(x)
y$layout$t <- y$layout$t + nrow(x)
y$layout$b <- y$layout$b + nrow(x)
x$layout <- rbind(x$layout, y$layout)
x$heights <- gtable:::insert.unit(x$heights, y$heights)
x$rownames <- c(x$rownames, y$rownames)
x$widths <- grid::unit.pmax(x$widths, y$widths)
x$grobs <- append(x$grobs, y$grobs)
x
}
Reduce(bind2, gtl)
}
## The rbind_ggplot function is now done within the grid.arrange function.
grid.arrange(rbind_ggplot(plot1, plot2), ncol=1)
## The next step is to remove the x axis labels on the fdom plot
plot1 <- plot1 + theme(axis.ticks.x = element_blank(), axis.text.x = element_blank(), axis.title.x = element_blank())
plot1
# And putting it back into the grid.arrange
grid.arrange(rbind_ggplot(plot1, plot2), ncol=1)
### Now if you want to have a legend for your plots.
# Crate a data.frame with 3 sites of fdom
fdom <- data.frame(date = hourly_date,
fdom_bog = sin(1:length(hourly_date)/300)+10,
fdom_outlet = sin(1:length(hourly_date)/300)+5,
fdom_girnock = sin(1:length(hourly_date)/300)+2
)
# Reshape the data frame to make only 3 columns
library(reshape2)
fdom <- melt(fdom, id.var="date")
# Now there is a date column, a variable column (fdom for each site) and the assoicated fdom value
head(fdom)
# Now make the same plot as before but use the variable to do the colouring of the line.
plot1 <- ggplot() + geom_line(aes(date, value, colour=variable), fdom) + theme_few(20) +
scale_colour_discrete("Legend title", labels = c("Bog", "Outlet", "Girnock")) +
ylab("FDOM (QSU)") +
scale_x_datetime(limits = c(ymd("2012-01-01"),ymd("2012-12-31")))
plot1
# And now for the DOC.
doc <- data.frame(date = samples,
doc_bog = rnorm(30, 10, 3),
doc_outlet = rnorm(30, 10, 2),
doc_girnock = rnorm(30, 10, 1)
)
doc <- melt(doc, id.var="date")
# I added the dashed line to make it a little easier to see the trend.
plot2 <- ggplot() + geom_point(aes(date, value, colour=variable), size = 2, doc) +
geom_line(aes(date, value, colour=variable), linetype = 2, doc) +
theme_few(20) +
scale_colour_discrete("Legend title", labels = c("Bog", "Outlet", "Girnock")) +
ylab(expression(paste("DOC (mg",L^{-1},")"))) +
scale_x_datetime(limits = c(ymd("2012-01-01"),ymd("2012-12-31")))
plot2
grid.arrange(rbind_ggplot(plot1, plot2), ncol=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment