1

I want to combine to ggplots. I tried the following code, but the result is not very good. In the combine plot I have problems with the second y-axis and the errorbars of plot 1.

Plot 1

pdf(file=paste("./plots/", "Cog1rt.pdf", sep=""), width=16, height=11)
p1 <- ggplot(data=datcom, aes(x=group, y=value, fill=group)) +
  geom_bar(position="dodge", size=.3,stat="identity") +  
  geom_errorbar( aes(ymax=value+1*value2, ymin=value, width=0.1,colour=group)) +
  labs(x="\n Gruppe", y="Reaktionszeit\n") + 
  facet_wrap(~rt) +
  theme_bw() %+replace%  theme(panel.background = element_rect(fill = NA))
print(p1) 
dev.off()

Plot 2

pdf(file="./plots/Cog1errl.pdf", width=4, height=3.5)
p2 <- ggplot(data=datcom, aes(x=group, y=value3,fill=group)) +

  geom_errorbar(aes(ymax=value3+1*value4, ymin=value3-1*value4,width=0.6)) +
  geom_point() +
  facet_wrap(~rt) +
  ylab("Fehler") +
  theme_bw() %+replace%  theme(panel.background = element_rect(fill = NA))
print(p2)

dev.off()

Combine

g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

pp <- c(subset(g1$layout, grepl("panel",name) , se = t:r))

g <- gtable_add_grob(g1, g2$grobs[grep("panel",g2$layout$name)], pp$t, 
                     pp$l, pp$b, pp$l)
ia <- which(grepl("axis_l",g2$layout$name) |  grepl("axis-l",g2$layout$name)     )
ga <- g2$grobs[ia]

axis_idx <- as.numeric(which(sapply(ga,function(x) !is.null(x$children$axis))))

i <- length(axis_idx)
ax <- ga[[axis_idx[i]]]$children$axis
ax$widths <- rev(ax$widths)
ax$grobs <- rev(ax$grobs)
ax$grobs[[1]]$x <- ax$grobs[[1]]$x - unit(-0.8, "npc") + unit(-0.1, "cm")
g <- gtable_add_cols(g, g2$widths[12], 12)
g <- gtable_add_grob(g, ax, pp$t[axis_idx[i]], length(g$widths) - 3, pp$b[axis_idx[i]])
grid.newpage()
grid.draw(g)
poplitea
  • 3,585
  • 1
  • 25
  • 39
  • Please format the code for readability. Code-blocks are formatted with four leading spaces for each line. – poplitea Jan 10 '17 at 20:42
  • A [minimal reproducible example](http://stackoverflow.com/questions/5963269) would be helpful... – bastistician Jan 10 '17 at 21:39
  • Maybe something like [this](http://stackoverflow.com/questions/37984000/how-to-manage-the-t-b-l-r-coordinates-of-gtable-to-plot-the-secondary-y-axi/38007170#38007170) – Sandy Muspratt Jan 10 '17 at 22:00
  • Or [this](http://stackoverflow.com/questions/26917689/how-to-use-facets-with-a-dual-y-axis-ggplot/37336658#37336658) – Sandy Muspratt Jan 10 '17 at 22:09
  • But [this](http://stackoverflow.com/questions/26917689/how-to-use-facets-with-a-dual-y-axis-ggplot/40746716#40746716) might be easier. – Sandy Muspratt Jan 10 '17 at 22:13
  • 1
    Thank you @poplitea for editing my post (first time at this forum). Thank you also @SandyMuspratt. I tried your last proposal but I get the following error:`d2 <- gather(d1, 'var', 'val', value3:value) %>% mutate(val = if_else(var == 'value', as.double(val), val / (max_Fehler / max_RZ)))` Error in mutate_impl(.data, dots) : non-numeric argument to binary operator In addition: Warning messages: 1: attributes are not identical across measure variables; they will be dropped 2: In if_else(c("value3", "value3", "value3", "value3", "value3", "value3", : NAs introduced by coercion – Daniel Groß Jan 11 '17 at 20:32
  • Without access to your data, it is difficult to tell where the problem lies. – Sandy Muspratt Jan 12 '17 at 00:42
  • Thank you @Sandy Muspratt for helping me. – Daniel Groß Jan 12 '17 at 15:09
  • I create an example plot: `group <- rep(c(1,2,3,4),3) task <- rep(c("flex", "wm","go"),4) rt <- rep(c(600,784,784,403,439,605),2) error_rt <- rep(c(323,354,293,243,235,245),2) mis <- rep(c(3,4,2,1,2,4),2) error_miss <-rep(c(1,2,1,1,2,2),2) data <- data.frame(group,task,rt,error_rt,mis,error_miss)` .I would like to create an plot with two y-axis. The x-axis should be the group with facet_wrap (~task) The first y-axis should be rt present in a barplot with error_rt as errorbar. And the second y-axis should be mis presented as point with the errorbar erorr_mis. – Daniel Groß Jan 12 '17 at 15:15
  • I can generate a plot, bu I would advise that you reconsider the style of plot. You will have a plot with bars, points, and two sets of error bars, making it difficult to read. Also, as your code stands, there are overlapping grid lines, overlapping error bars, grid lines overlapping one set of error bars so they are not visible, and a legend in the right margin appearing as though it has something to do with the rights scale but in fact it relates to the bars; though these are fairly easily fixed. – Sandy Muspratt Jan 13 '17 at 03:30

0 Answers0