Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple titles in facet_wrap (ggplot2)

Tags:

r

ggplot2

I am trying to add multiple titles to a plot using facet_wrap and ggplot2. Say that you e.g. have quarterly data over two years, and wants a graphical comparison of the quarterly data with two major titles; 2014 and 2015, as well as titles for the respective quarter.

I have come this far:

data <- rnorm(10)

A1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2014)
A2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2014)
A3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2014)
A4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2014)

N1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2015)
N2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2015)
N3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2015)
N4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2015)

A <- rbind(A1, A2, A3, A4)
N <- rbind(N1, N2, N3, N4)
tmp <- data.frame(rbind(A, N))

ggplot(data=tmp, aes(x=X, y=Y)) + geom_line() + facet_wrap(~year + Q, scales="free", ncol=4)

which gives me this graph: enter image description here

Instead I would like "2014" and "2015" to be in two separate grey boxes above each sub graph. Is that possible?

Thanks!

like image 305
Pierre Avatar asked Aug 02 '15 20:08

Pierre


1 Answers

Using the code here, as suggested by Sandy Muspratt, i can come up with:

library(ggplot2)
library(gtable)

data <- rnorm(10)

A1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2014)
A2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2014)
A3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2014)
A4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2014)

N1 <- data.frame("Y"=data, "X"=1:10, "Q"=1, "year"=2015)
N2 <- data.frame("Y"=data, "X"=1:10, "Q"=2, "year"=2015)
N3 <- data.frame("Y"=data, "X"=1:10, "Q"=3, "year"=2015)
N4 <- data.frame("Y"=data, "X"=1:10, "Q"=4, "year"=2015)

A <- rbind(A1, A2, A3, A4)
N <- rbind(N1, N2, N3, N4)
tmp <- data.frame(rbind(A, N))

p <- ggplot(data=tmp, aes(x=X, y=Y)) + geom_line() + facet_wrap(~year + Q, scales="free", ncol=4)


z <- ggplotGrob(p)

#  New title strip at the top
z <- gtable_add_rows(z, unit(1, "lines"), pos = 0)  # New row added to top

# Display the layout to select the place for the new strip
gtable_show_layout(z)   # New strip goes into row 2 
# New strip spans columns 4 to 13

z <- gtable_add_grob(z, 
                     list(rectGrob(gp = gpar(col = NA, fill = grey(0.8), size = .5)),
                          textGrob("2014", vjust = .27, 
                                   gp = gpar(cex = .75, fontface = 'bold', col = "black"))), 2, 4, 2, 13, name = c("a", "b"))

# Add small gap between strips - below row 2
z <- gtable_add_rows(z, unit(2/10, "line"), 2)


#  New title strip in the middle
z <- gtable_add_rows(z, unit(1, "lines"), pos = 8)  # New row added to top
# Display the layout to select the place for the new strip
gtable_show_layout(z) 
# New strip goes into row 9 
# New strip spans columns 4 to 13

z <- gtable_add_grob(z, 
                     list(rectGrob(gp = gpar(col = NA, fill = grey(0.8), size = .5)),
                          textGrob("2015", vjust = .27, 
                                   gp = gpar(cex = .75, fontface = 'bold', col = "black"))), 9, 4, 9, 13, name = c("a", "b"))

# Add small gap between strips - below row 2
z <- gtable_add_rows(z, unit(2/10, "line"), 9)


# Draw it
grid.newpage()
grid.draw(z)

Which will give you this graph:enter image description here

like image 50
RHA Avatar answered Oct 11 '22 11:10

RHA