title: R - Secondary y-axis line not showing up when using gtable_add_grob
body: <p>I'm trying to overlay precipitation data over water quality data I've been collecting. I've made the water quality data and precipitation plots separately and am now trying to combine them using gtable_add_grob (a la <a href="http://rpubs.com/kohske/dual_axis_in_ggplot2" rel="nofollow noreferrer">http://rpubs.com/kohske/dual_axis_in_ggplot2</a>). I've got the plot almost finished and looking good, but am running into a problem with the secondary y-axis not displaying. My code is as follows (for example):</p>
  
  <pre><code>y=(1:12)
  y2=(12:1)
  x=seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by="months")
  df=data.frame(x,y)
  df2=data.frame(x,y2)
  
  #plot1
  g&lt;-ggplot(df,aes(x,y))
  g&lt;-g+geom_bar(stat="identity",alpha=0.4)
  g&lt;-g+scale_y_reverse()
  g&lt;-g+theme(panel.grid = element_blank())
  g&lt;-g+theme(panel.background = element_blank())
  g&lt;-g+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))
  g&lt;-g+theme(axis.text.x = element_text(angle=45,hjust=1,color="black"))
  g&lt;-g+theme(axis.text.y = element_text(color="black"))
  g&lt;-g+theme(panel.grid = element_blank())
  g&lt;-g+theme(axis.line=element_line(colour="black"))
  #print(g) #looks fine with axes lines
  
  #plot2
  g2&lt;-ggplot(df,aes(x,y))
  g2&lt;-g2+geom_line()
  g2&lt;-g2+theme(panel.grid = element_blank())
  g2&lt;-g2+theme(panel.background = element_blank())
  g2&lt;-g2+scale_x_date(labels = date_format("%b-%y"),breaks = date_breaks("months"))
  g2&lt;-g2+theme(axis.text.x = element_text(angle=45,hjust=1,color="black"))
  g2&lt;-g2+theme(axis.text.y = element_text(color="black"))
  g2&lt;-g2+theme(panel.grid = element_blank())
  g2&lt;-g2+theme(axis.line=element_line(colour="black"))
  #print(g2) #looks fine with axes lines
  
  #combining them
  gnew1&lt;-ggplot_gtable(ggplot_build(g))
  gnew2&lt;-ggplot_gtable(ggplot_build(g2))
  gg&lt;-c(subset(gnew1$layout,name=="panel",se=t:r))
  gnew&lt;-gtable_add_grob(gnew2,gnew1$grobs[[which(gnew1$layout$name=="panel")]],pp$t,pp$l,pp$b,pp$l)
  #attempted secondary axis    
  ia&lt;-which(gnew1$layout$name=="axis-l")
  ga&lt;-gnew1$grobs[[ia]]
  ax&lt;-ga$children[[2]]
  ax$widths&lt;-rev(ax$widths)
  ax$grobs&lt;-rev(ax$grobs)
  ax$grobs[[1]]$x&lt;-ax$grobs[[1]]$x - unit(1,"npc") + unit(0.15, "cm")
  gnew&lt;-gtable_add_cols(gnew,p1$widths[p1$layout[ia, ]$l], length(g1$widths) - 1)
  gnew&lt;-gtable_add_grob(gnew, ax, pp$t,length(gnew$widths)-1)
  grid.draw(gnew)
  </code></pre>
  
  <p>This gives me a plot that looks like so:
  <img src="https://i.stack.imgur.com/ZN8F5.png" alt="enter image description here"></p>
  
  <p>My problem is that I want the secondary y-axis line to show up as well - you can see it's missing here. My original suspicion was that it had something to do with my making the panels grids and backgrounds blank for both graphs, but the axes lines on the independent graphs plot fine after use of</p>
  
  <pre><code>axis.line=element_line(colour="black")
  </code></pre>
  
  <p>Additionally, I need the clear backgrounds for the way this data will be displayed (so if it is this, is there a work around?). I went through the combining graphs portion of the code step by step and it seems to be working as intended. My output for the combined graph is</p>
  
  <pre><code>&gt; gnew
  TableGrob (6 x 6) "layout": 10 grobs
     z     cells       name                                 grob
  1  0 (1-6,1-6) background      rect[plot.background.rect.1102]
  2  3 (3-3,3-3)     axis-l absoluteGrob[GRID.absoluteGrob.1094]
  3  1 (4-4,3-3)     spacer                       zeroGrob[NULL]
  4  2 (3-3,4-4)      panel               gTree[GRID.gTree.1080]
  5  4 (4-4,4-4)     axis-b absoluteGrob[GRID.absoluteGrob.1087]
  6  5 (5-5,4-4)       xlab         text[axis.title.x.text.1096]
  7  6 (3-3,2-2)       ylab         text[axis.title.y.text.1098]
  8  7 (2-2,4-4)      title           text[plot.title.text.1100]
  9  8 (3-3,4-4)     layout               gTree[GRID.gTree.1048]
  10 9 (3-3,5-5)     layout                         gtable[axis]
  </code></pre>
  
  <p>This is similar to the output of the combined graph from my own data. Any thoughts on why the secondary y-axis line will not display? </p>