Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Place title of multiplot panel with ggplot2

Tags:

r

ggplot2

From the help found here I've managed to create this multiplot panel: enter image description here with the following code:

library(zoo) library(ggplot2)  datos=read.csv("paterna.dat",sep=";",header=T,na.strings="-99.9")  datos$dia=as.POSIXct(datos[,1], format="%y/%m/%d %H:%M:%S")  datos$Precipitación[is.na(datos$Precipitación)]=0 xlim = as.POSIXct(c("2010-05-12 00:00:00", "2010-05-12 23:50:00")) ylim = trunc(max(datos$Precipitación) + 5) tmax = trunc(max(datos$Temperatura) + 5) tmin = trunc(min(datos$Temperatura) - 5)  tmx = max(datos$Temperatura)  tmxpos=which.max(datos$Temperatura)  tmn = min(datos$Temperatura)  tmnpos=which.min(datos$Temperatura)   tmp=ggplot(data=datos,aes(x=dia, y=Temperatura)) + geom_line(colour="red") + ylab("Temperatura (ºC)") +  xlab(" ") + scale_x_datetime(limits=xlim ,format = "%H",major='hour') + scale_y_continuous(limits = c(tmin,tmax)) + geom_text(data=datos[tmxpos,], label=tmx, vjust=-1.5, colour="red") + geom_text(data=datos[tmnpos,], label=tmn, vjust=1.5, colour="blue")  pre=ggplot(data=datos,aes(x=dia, y=Precipitación)) + geom_bar(colour="blue",stat="identity",fill="blue") + ylab("Precipitación (l)") + xlab("Hora solar") + scale_x_datetime(limits=xlim ,format = "%H",major='hour') + scale_y_continuous(limits=c(0,ylim))  vel=ggplot(data=datos,aes(x=dia, y=Velocidad)) + geom_line(colour="brown") + ylab("Velocidad (km/h)") + xlab(" ")  + scale_x_datetime(limits=xlim ,format = "%H",major='hour') + scale_y_continuous(limits = c(0,100))  dir=ggplot(data=datos,aes(x=dia, y=Dirección)) + geom_line(colour="brown") + ylab("Dirección viento (grados)") + xlab(" ") + scale_x_datetime(limits=xlim ,format = "%H",major='hour') + scale_y_continuous(limits = c(0,360))  hum=ggplot(data=datos,aes(x=dia, y=Humedad.Relativa)) + geom_line(colour="blue") + ylab("Humedad relativa (%)") + xlab(" ") + scale_x_datetime(limits=xlim ,format = "%H",major='hour') + scale_y_continuous(limits = c(0,100))  grid.newpage() pushViewport(viewport(layout = grid.layout(3, 2)))    print(tmp, vp = viewport(layout.pos.row = 1, layout.pos.col = 1))          print(vel, vp = viewport(layout.pos.row = 1, layout.pos.col = 2)) print(dir, vp = viewport(layout.pos.row = 2, layout.pos.col = 2)) print(hum, vp = viewport(layout.pos.row = 2, layout.pos.col = 1)) print(pre, vp = viewport(layout.pos.row = 3, layout.pos.col = 1:2)) 

Now I'm missing the title of the multiplot that I want to be the met. station name. I haven't found how to set main title on grid.newpage or viewport. I've read about grid.arrange but couldn't figure out how to use it in my case.

How can this be done? For sure it's gonna be an easy question for you.

You can find source data in http://ubuntuone.com/4G01ifn7cJ1jMIOKh

Thanks in advance

UPDATE: Thanks to koshke I found the solution. The working code is:

grid.newpage() pushViewport(viewport(layout = grid.layout(4, 2, heights = unit(c(0.5, 5, 5, 5), "null"))))    grid.text("MAIN TITLE", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2)) print(tmp, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))          print(vel, vp = viewport(layout.pos.row = 2, layout.pos.col = 2)) print(dir, vp = viewport(layout.pos.row = 3, layout.pos.col = 2)) print(hum, vp = viewport(layout.pos.row = 3, layout.pos.col = 1)) print(pre, vp = viewport(layout.pos.row = 4, layout.pos.col = 1:2)) 
like image 911
pacomet Avatar asked Dec 23 '11 11:12

pacomet


People also ask

How do I add a title to a Ggplot?

To add a title to your plot, add the code +ggtitle("Your Title Here") to your line of basic ggplot code. Ensure you have quotation marks at the start and end of your title. If you have a particulary long title that would work better on two lines, use \n for a new line.

Does par () work with Ggplot?

One disadvantage for par() is that it cannot work for ggplot, we can see below that the plot should appear on the upper left of the page, but it just happen as if par() isn't written here.

How do I put two Ggplots on top of each other?

Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.

How do I show multiple Ggplots in R?

The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page. marrangeGrob() for arranging multiple ggplots over multiple pages.


2 Answers

library(gridExtra)  p <- ggplot()  grid.arrange(p,p,p,p,p, top = "Title",             layout_matrix = matrix(c(1,2,3,4,5,5), ncol=2, byrow=TRUE)) 

enter image description here

like image 86
baptiste Avatar answered Sep 22 '22 01:09

baptiste


If I understand correctly what you want to do, probably you can use +opts(title = XXX):

p1 <- ggplot(mtcars, aes(factor(cyl))) + geom_bar() p2 <- ggplot(mtcars, aes(wt, mpg)) + geom_point() p3 <- p2 + geom_line()  pushViewport(viewport(layout = grid.layout(2, 2)))   print(p1 + opts(title = "bar"),    vp = viewport(layout.pos.row = 1, layout.pos.col = 1))      print(p2 + opts(title = "point"),    vp = viewport(layout.pos.row = 1, layout.pos.col = 2))      print(p3 + opts(title = "point and line"),    vp = viewport(layout.pos.row = 2, layout.pos.col = 1:2)) 

enter image description here

UPDATED

here is an example:

pushViewport(viewport(layout = grid.layout(3, 2, heights = unit(c(1, 4, 4), "null")))) grid.text("title of this panel", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2)) print(p1, vp = viewport(layout.pos.row = 2, layout.pos.col = 1)) print(p2, vp = viewport(layout.pos.row = 2, layout.pos.col = 2)) print(p3, vp = viewport(layout.pos.row = 3, layout.pos.col = 1:2)) 

what you need to do is:

  1. Make one extra row in grid.layout
  2. Adjust width
  3. Draw textGrob on the extra viewport row.

enter image description here

like image 32
kohske Avatar answered Sep 20 '22 01:09

kohske