help with lay-out multiple ggplot2 plots

86 views
Skip to first unread message

Ellen Pape

unread,
Jan 27, 2016, 3:19:51 PM1/27/16
to ggplot2
Hi all,

I would really like some help with the following plot - consisting of multiple ggplot2 plots. I have tried several approaches, using package cowplot and gridExtra, but I have not succeeded in solving all issues at once

I would like to have 6 ggplot2 plots in 1 "plot window".

These are the issues I am experiencing:

- I would like to place the x axis on top of each graph, instead of at the bottom. 
- I would like to have 1 vertical axis label (Depth), shown on the left of the graph window, as this is the same for all graphs
- I would like to label each plot with letters A till F (for ease of reference)
- I would like to have 1 legend (as it is common for all plots), shown at the bottom of the plot window, with the labels placed next to each other (on 1 horizontal line)
- I would like to increase the distance between the two rows of graphs

Thanks!!
Ellen


Here is an example dataframe that can be solved with

Data <- data.frame(
  Depth = rep(1:5,3),
  Station = rep(c("A","B","C"),each=5),
  VarA=runif(15, 5.0, 7.5),
  VarB=runif(15, 3.0, 20.5),
  VarC=runif(15,1,15),
  VarD=runif(15,0.5,20),
  VarE=runif(15,2,42),
  VarF=runif(15,1,6)
)

require(ggplot2)
require(gridExtra)
plotA<-ggplot(Data,aes(x=Depth,y=VarA,colour=Station))+geom_point()+geom_line()+coord_flip()
plotA
plotB<-ggplot(Data,aes(x=Depth,y=VarB,colour=Station))+geom_point()+geom_line()+coord_flip()
plotB
plotC<-ggplot(Data,aes(x=Depth,y=VarC,colour=Station))+geom_point()+geom_line()+coord_flip()
plotC
plotD<-ggplot(Data,aes(x=Depth,y=VarD,colour=Station))+geom_point()+geom_line()+coord_flip()
plotD
plotE<-ggplot(Data,aes(x=Depth,y=VarE,colour=Station))+geom_point()+geom_line()+coord_flip()
plotE
plotF<-ggplot(Data,aes(x=Depth,y=VarF,colour=Station))+geom_point()+geom_line()+coord_flip()
plotF

grid.arrange(plotA,plotB,plotC,plotD,plotE,plotF,nrow=2,ncol=3)

Wouter van der Bijl

unread,
Jan 27, 2016, 4:56:36 PM1/27/16
to ggplot2

One other solution that can tick at least a lot of those boxes is to use facets.:

Data2 <- tidyr::gather(Data, 'var', 'val', -Depth, -Station)
ggplot(Data2,aes(x=val,y=Depth,colour=Station))+
  geom_point()+
  geom_path()+
  facet_wrap(~var, scales = "free") + 
  xlim(0, 42) + 
  theme(panel.margin = unit(2, "lines"), legend.position = 'top')

It doesn't have separate x axis labels though. A to F labels could be added with annotate().

W.

Ellen Pape

unread,
Jan 28, 2016, 7:33:02 AM1/28/16
to Wouter van der Bijl, ggplot2
Hi Wouter,

Thank you for this solution. Howevever, when I try your approach on my real dataset, and I want to plot 3 graphs, 1 for each variable (TOC,TN,TOC.TN), I get a different problem, i.e.  I cannot get the scales of the x axis to vary (range of values are diff for the 3 variables) with the below code (based on your code).

- If I use facet_wrap, I got an error message ("Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting") --> don't really understand what this means
- If I use facet_grid, the scale remains the same for each plot.

Do you happen to know why this is?

Thanks,
ellen


limits.errorbars<-aes(ymax = avg.value + sd.value, ymin=avg.value - sd.value,colour=Station_ID)

plots<-ggplot(dataset,aes(x=slice_upper_cm,y=avg.value,colour=Station_ID))+
    geom_point(size=5)+
    geom_line()+
    coord_flip()+
    geom_errorbar(limits.errorbars,width=0.2)+
    scale_x_continuous(breaks=-9:0,"Sediment depth (cm)\n",labels=c("9-10","8-9","7-8","6-7","5-6","4-5","3-4","2-3","1-2","0-1"))+
    scale_y_continuous("")+
    facet_wrap(.~variable,scales="free_y")

plots



dataset<-structure(list(Station_ID = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                                 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 
                                                 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 
                                                 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
                                                 3L, 3L, 3L, 3L), .Label = c("B6S02", "B4S03", "B4N01"), class = "factor"), 
                        slice_upper_cm = c(-9L, -8L, -7L, -6L, -5L, -4L, -3L, -2L, 
                                           -1L, 0L, -9L, -8L, -7L, -6L, -5L, -4L, -3L, -2L, -1L, 0L, 
                                           -9L, -8L, -7L, -6L, -5L, -4L, -3L, -2L, -1L, 0L, -9L, -8L, 
                                           -7L, -6L, -5L, -4L, -3L, -2L, -1L, 0L, -9L, -8L, -7L, -6L, 
                                           -5L, -4L, -3L, -2L, -1L, 0L, -9L, -8L, -7L, -6L, -5L, -4L, 
                                           -3L, -2L, -1L, 0L, -9L, -8L, -7L, -6L, -5L, -4L, -3L, -2L, 
                                           -1L, 0L, -9L, -8L, -7L, -6L, -5L, -4L, -3L, -2L, -1L, 0L, 
                                           -9L, -8L, -7L, -6L, -5L, -4L, -3L, -2L, -1L, 0L), variable = c("TOC", 
                                                                                                          "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", 
                                                                                                          "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", 
                                                                                                          "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", "TOC", 
                                                                                                          "TOC", "TOC", "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", 
                                                                                                          "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", 
                                                                                                          "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", "TN", 
                                                                                                          "TN", "TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", 
                                                                                                          "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", 
                                                                                                          "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", 
                                                                                                          "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", 
                                                                                                          "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", "TOC.TN", 
                                                                                                          "TOC.TN"), avg.value = c(0.356666666666667, 0.366666666666667, 
                                                                                                                                   0.45, 0.433333333333333, 0.5, 0.566666666666667, 0.586666666666667, 
                                                                                                                                   0.563333333333333, 0.583333333333333, 0.536666666666667, 
                                                                                                                                   0.346666666666667, 0.363333333333333, 0.346666666666667, 
                                                                                                                                   0.366666666666667, 0.396666666666667, 0.386666666666667, 
                                                                                                                                   0.45, 0.493333333333333, 0.493333333333333, 0.49, 0.45, 0.503333333333333, 
                                                                                                                                   0.463333333333333, 0.516666666666667, 0.516666666666667, 
                                                                                                                                   0.536666666666667, 0.556666666666667, 0.606666666666667, 
                                                                                                                                   0.586666666666667, 0.66, 0.12, 0.176666666666667, 0.206666666666667, 
                                                                                                                                   0.163333333333333, 0.186666666666667, 0.16, 0.193333333333333, 
                                                                                                                                   0.166666666666667, 0.17, 0.133333333333333, 0.123333333333333, 
                                                                                                                                   0.133333333333333, 0.166666666666667, 0.146666666666667, 
                                                                                                                                   0.14, 0.133333333333333, 0.15, 0.156666666666667, 0.166666666666667, 
                                                                                                                                   0.163333333333333, 0.17, 0.21, 0.186666666666667, 0.21, 0.203333333333333, 
                                                                                                                                   0.206666666666667, 0.226666666666667, 0.236666666666667, 
                                                                                                                                   0.216666666666667, 0.223333333333333, 3.45689800133333, 2.508788344, 
                                                                                                                                   2.599500864, 3.156817167, 3.16560748066667, 4.119647827, 
                                                                                                                                   3.55330822033333, 3.91909069166667, 4.03024704566667, 4.79755673466667, 
                                                                                                                                   3.251946721, 3.07435306733333, 2.63445202633333, 2.94084695933333, 
                                                                                                                                   3.23032396766667, 3.3763, 3.51182927033333, 3.69732725533333, 
                                                                                                                                   3.52748928733333, 3.63916427866667, 3.14138333733333, 2.80537980633333, 
                                                                                                                                   2.852736917, 2.86524508566667, 2.99788226366667, 3.034018571, 
                                                                                                                                   2.89037871966667, 3.00988641066667, 3.14028874766667, 3.53395680033333
                                                                                                          ), sd.value = c(0.0901849950564579, 0.087368949480541, 0.11, 
                                                                                                                          0.0945163125250522, 0.02, 0.0416333199893226, 0.0416333199893226, 
                                                                                                                          0.0750555349946514, 0.0493288286231625, 0.00577350269189626, 
                                                                                                                          0.0493288286231625, 0.0971253485622231, 0.0404145188432738, 
                                                                                                                          0.0251661147842358, 0.0503322295684717, 0.0351188458428425, 
                                                                                                                          0.03, 0.00577350269189626, 0.0230940107675851, 0.03, 0.04, 
                                                                                                                          0.066583281184794, 0.0321455025366432, 0.0723417813807024, 
                                                                                                                          0.0602771377334171, 0.0351188458428424, 0.0404145188432738, 
                                                                                                                          0.0832666399786453, 0.0208166599946613, 0.113578166916006, 
                                                                                                                          0.01, 0.0635085296108588, 0.0832666399786453, 0.0251661147842358, 
                                                                                                                          0.0321455025366432, 0, 0.0351188458428425, 0.0115470053837925, 
                                                                                                                          0.02, 0.0208166599946613, 0.0115470053837925, 0.0208166599946613, 
                                                                                                                          0.055075705472861, 0.023094010767585, 0, 0.0152752523165195, 
                                                                                                                          0.01, 0.0115470053837925, 0.0378593889720018, 0.0493288286231625, 
                                                                                                                          0.0264575131106459, 0.0360555127546399, 0.0115470053837925, 
                                                                                                                          0.0264575131106459, 0.0321455025366432, 0.0251661147842358, 
                                                                                                                          0.0251661147842358, 0.0568624070307733, 0.00577350269189626, 
                                                                                                                          0.0665832811847939, 0.878801896010041, 0.616182039660263, 
                                                                                                                          0.398388202276427, 0.43237871510877, 0.422353666901443, 0.232349437821054, 
                                                                                                                          0.510298691340651, 0.588985400477535, 0.773395663582113, 
                                                                                                                          0.678851141619681, 0.143337235928456, 0.483484223768652, 
                                                                                                                          0.786173954224227, 0.269545815320824, 0.360528565114919, 
                                                                                                                          0.570916689628269, 0.344370964224575, 0.343409916819879, 
                                                                                                                          0.602124926553554, 0.822921218879401, 0.30083618732301, 0.145437897623324, 
                                                                                                                          0.312997982946254, 0.104491015676895, 0.097539255986646, 
                                                                                                                          0.303851161908953, 0.150698262148946, 0.282379431915581, 
                                                                                                                          0.180229439769592, 0.430387474339244), id = c(1L, 2L, 3L, 
                                                                                                                                                                        4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
                                                                                                                                                                        17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
                                                                                                                                                                        29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
                                                                                                                                                                        13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 
                                                                                                                                                                        25L, 26L, 27L, 28L, 29L, 30L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
                                                                                                                                                                        8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
                                                                                                                                                                        20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L)), row.names = c("1.TOC", 
                                                                                                                                                                                                                                               "2.TOC", "3.TOC", "4.TOC", "5.TOC", "6.TOC", "7.TOC", "8.TOC", 
                                                                                                                                                                                                                                               "9.TOC", "10.TOC", "11.TOC", "12.TOC", "13.TOC", "14.TOC", "15.TOC", 
                                                                                                                                                                                                                                               "16.TOC", "17.TOC", "18.TOC", "19.TOC", "20.TOC", "21.TOC", "22.TOC", 
                                                                                                                                                                                                                                               "23.TOC", "24.TOC", "25.TOC", "26.TOC", "27.TOC", "28.TOC", "29.TOC", 
                                                                                                                                                                                                                                               "30.TOC", "1.TN", "2.TN", "3.TN", "4.TN", "5.TN", "6.TN", "7.TN", 
                                                                                                                                                                                                                                               "8.TN", "9.TN", "10.TN", "11.TN", "12.TN", "13.TN", "14.TN", 
                                                                                                                                                                                                                                               "15.TN", "16.TN", "17.TN", "18.TN", "19.TN", "20.TN", "21.TN", 
                                                                                                                                                                                                                                               "22.TN", "23.TN", "24.TN", "25.TN", "26.TN", "27.TN", "28.TN", 
                                                                                                                                                                                                                                               "29.TN", "30.TN", "1.TOC.TN", "2.TOC.TN", "3.TOC.TN", "4.TOC.TN", 
                                                                                                                                                                                                                                               "5.TOC.TN", "6.TOC.TN", "7.TOC.TN", "8.TOC.TN", "9.TOC.TN", "10.TOC.TN", 
                                                                                                                                                                                                                                               "11.TOC.TN", "12.TOC.TN", "13.TOC.TN", "14.TOC.TN", "15.TOC.TN", 
                                                                                                                                                                                                                                               "16.TOC.TN", "17.TOC.TN", "18.TOC.TN", "19.TOC.TN", "20.TOC.TN", 
                                                                                                                                                                                                                                               "21.TOC.TN", "22.TOC.TN", "23.TOC.TN", "24.TOC.TN", "25.TOC.TN", 
                                                                                                                                                                                                                                               "26.TOC.TN", "27.TOC.TN", "28.TOC.TN", "29.TOC.TN", "30.TOC.TN"
                                                                                                                                                                        ), .Names = c("Station_ID", "slice_upper_cm", "variable", "avg.value", 
                                                                                                                                                                                      "sd.value", "id"), class = "data.frame", reshapeLong = structure(list(
                                                                                                                                                                                        varying = list(c("avgTOC.station.depth", "avgTN.station.depth", 
                                                                                                                                                                                                         "avgTOC.TN.station.depth"), c("sdTOC.station.depth", "sdTN.station.depth", 
                                                                                                                                                                                                                                       "sdTOC.TN.station.depth")), v.names = c("avg.value", "sd.value"
                                                                                                                                                                                                                                       ), idvar = "id", timevar = "variable"), .Names = c("varying", 
                                                                                                                                                                                                                                                                                          "v.names", "idvar", "timevar")))

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to a topic in the Google Groups "ggplot2" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ggplot2/alViqolKDMw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wouter van der Bijl

unread,
Jan 28, 2016, 8:55:22 AM1/28/16
to ggplot2, wouter...@hotmail.com

Hi Ellen,

Facetting and coord_flip() generally don't play nice, so we'll have to make a few changes to do without that (as I did in my example before). We switch x and y in the aes(), we use geom_path() instead of geom_line() (be aware that the order of rows matters now), and we replace geom_errorbar() with geom_errorbarh(). Also, I had to take out the . (dot) argument from the formula in facet_wrap(), which was giving you the error.

Additionally, I moved the legend to the bottom and increased the spacing between the facets (as you had mentioned before). I then realized we can move the facet strips to the bottom, and remove the grey background so they now actually look like axis labels!

I also added another theme, and some panel labels (A, B and C). See code below.



limits.errorbars <- aes(xmax = avg.value + sd.value, xmin=avg.value - sd.value)
labels <- data.frame(variable = c('TN', 'TOC', 'TOC.TN'),
                     x = c(0.05, 0.2, 1.8),
                     y = 0,
                     label = LETTERS[1:3])

ggplot(dataset,aes(x = avg.value, y = slice_upper_cm, colour = Station_ID)) +
  geom_point(size = 5) +
  geom_path() +
  geom_errorbarh(limits.errorbars, height = 0.2) +
  scale_y_continuous(breaks=-9:0,"Sediment depth (cm)\n",labels=c("9-10","8-9","7-8","6-7","5-6","4-5","3-4","2-3","1-2","0-1"))+
  scale_x_continuous("")+
  facet_wrap(~variable, scales = "free", switch = 'x') +
  theme_bw() +
  theme(panel.margin = unit(2, "lines"), 
        legend.position = 'bottom',
        strip.background = element_blank(),
        legend.margin = unit(0, "lines")) +
  geom_text(data = labels, aes(x, y, label = label), size = 10,
            inherit.aes = FALSE, hjust = "inward")

Ellen Pape

unread,
Jan 31, 2016, 9:35:19 AM1/31/16
to Wouter van der Bijl, ggplot2
Thanks Wouter!
Reply all
Reply to author
Forward
0 new messages