2

I am using a working to plot on Dual Axis using ggplot2. I am using the code published below. I am trying to save it to JPEG but it only save the last output line and not the two lines of the plot even if axis are correct. How to save the two axis with their relative line at the same time? I am trying ggsave but it complains it is not a ggplot2 format.

## Putting plots together ##################
# extract gtable
g1 <- ggplot_gtable(ggplot_build(p1))
g2 <- ggplot_gtable(ggplot_build(p2))

# overlap the panel of 2nd plot on that of 1st plot
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)



# axis tweaks
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))))

for(i in 1: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(1, "npc") + unit(0.15, "cm")
  g <- gtable_add_cols(g, g2$widths[g2$layout[ia[axis_idx[i]], ]$l], length(g$widths) - 1)
  g <- gtable_add_grob(g, ax, pp$t[axis_idx[i]], length(g$widths) - i, pp$b[axis_idx[i]])
}



# Plot!
grid.newpage()
grid.draw(g)
4
  • maybe your second plot is not transparent? try g2 <- ggplot_gtable(ggplot_build(p2 + theme(plot.background = element_blank(), panel.background = element_blank()))) Commented Oct 7, 2015 at 12:31
  • No it always seems to save only one of the two plot. Not both. I have tried your suggestion but result is the same. Commented Oct 7, 2015 at 12:47
  • you also need to open graphics device with allowed transparency like jpeg(bg = "transparent"), by default it has bg = "white" Commented Oct 7, 2015 at 13:06
  • It solved the problem i think. Thanks a lot Commented Oct 7, 2015 at 13:24

1 Answer 1

1

Here we overlap one plot with the second, so the second plot should be transparent not to hide the first one. we can get this by deleting panel.background and plot.background from the plot.

g2 <- ggplot_gtable(ggplot_build(p2 + theme(plot.background = element_blank(), 
                                            panel.background = element_blank())))

Another option is setting background color to transparent in the output device.

jpeg(bg = "transparent")

After that we can get dual-axis plot (did it on the data from Seatbelts data.frame):

enter image description here

We also should keep in mind that gridlines of the second plot are still visible and we have to do something with them in case of need.

Sign up to request clarification or add additional context in comments.

2 Comments

You see your histogram is behind the AXIS and it becomes less clear. How to get the AXIS to plot behind both?
As a quick solution it's ok to remove them from the second plot, modifying the theme again: p2 + theme(panel.grid = element_blank()) If you really want to plot them, but behind both plot layers, it's possible, but there are at least two ways of doing it: 1) modifying the order of grobs in resulting gtable 2) rebuilding the first plot with custom y-scale breaks and labels. Both ways are not so simple to give an answer in comments. You can post it as a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.