Creating ggplot2 charts that are facetwrapped by plot type

Viewed 216

I am trying to create a ggplot2 facet wrapped by plot type. I've used grid.arrange but I'm trying to maximise the plot area, using the grid.arrange I loose area due to the repeating of the y axis and legends.

Specifically, I am trying to create a plot that has a BoxPlot on the Left with a timeseries of the same data (so same Y axis) on the Right. Then I'd have a single Y Axis and a Single Legend - is this possible?

The code:

library(ggplot2)
library(gridExtra)
Time = c("19/12/2013 10:00","19/12/2013 10:01", "19/12/2013 10:02", "19/12/2013 10:03",    "19/12/2013 10:04",
     "19/12/2013 10:05", "19/12/2013 10:06", "19/12/2013 10:07", "19/12/2013 10:08", "19/12/2013 10:09",
     "19/12/2013 10:10", "19/12/2013 10:11", "19/12/2013 10:12", "19/12/2013 10:13", "19/12/2013 10:14")

test <- data.frame(Time)

test$Factor <- c("t1", "t1", "t1", "t1", "t1", "t1", "t1", "t1", "t1", "t2", "t2", "t2", "t2", "t2", "t2")
test$Values <- c(4,7,2,9,6,9,1,1,5,8,3,4,3,6,7)
test$PROD <- test$PROD <- c("one", "two", "one", "two", "one", "one", "two", "one", "one", 
           "two", "one", "two", "two", "two", "one")

p1 <-ggplot(data=test,aes(Factor,Values )) +
    geom_boxplot(outlier.colour = "red", outlier.size = 3, outlier.shape = 15, fill = "white", colour = "blue") +
    theme(panel.grid.minor = element_line(colour = "grey"), plot.title = element_text(size = rel(2)),axis.text.x = element_text(angle=90, vjust=1), strip.text.x = element_text(size = 8, colour = "black", face = "bold")) +  
    geom_point(alpha = 0.6, position = position_jitter(w = 0.05, h = 0.0), aes(colour=factor(Factor)), size =3) +
    facet_wrap(~PROD, scales = "free") +
    ggtitle("MyTitle") + 
    scale_size_area() + 
    xlab("Tools") + 
    ylab("Values")

p2<-ggplot(data = test, aes(Time,Values )) + 
    ggtitle("MyTitle") + 
    theme(axis.text.x = element_text(angle=90, vjust=1),plot.title = element_text(size = rel(2))) + 
    geom_point(aes(colour=factor(Factor)), size = 3) + 
    facet_wrap(~PROD, scales = "free") + 
    xlab("TIME") + 
    ylab("Values")

grid.arrange(p1,p2,ncol=2)

enter image description here

2 Answers
Related