title: How to use facets with a dual y-axis ggplot
body: <p>I have been trying to extend my scenario from <a href="https://stackoverflow.com/questions/26727741/how-to-show-a-legend-on-dual-y-axis-ggplot/26742226?noredirect=1#comment42381239_26742226">here</a> to make use of facets (specifically <code>facet_grid()</code>).</p>
  
  <p>I have seen this <a href="https://stackoverflow.com/questions/21981416/dual-y-axis-in-ggplot2-for-multiple-panel-figure">example</a>, however I can't seem to get it to work for my <code>geom_bar()</code> and <code>geom_point()</code> combo. I attempted to use the code from the example just changing from <code>facet_wrap</code> to <code>facet_grid</code> which also seemed to make the first layer not show.</p>
  
  <p>I am very much a novice when it comes to grid and grobs so if someone can give some guidance on how to make P1 show up with the left y axis and P2 show up on the right y axis that would be great.</p>
  
  <p><strong>Data</strong></p>
  
  <pre><code>library(ggplot2)
  library(gtable)
  library(grid)
  library(data.table)
  library(scales)
  
  grid.newpage()
  
  dt.diamonds &lt;- as.data.table(diamonds)
  
  d1 &lt;- dt.diamonds[,list(revenue = sum(price),
                          stones = length(price)),
                    by=c("clarity","cut")]
  
  setkey(d1, clarity,cut)
  </code></pre>
  
  <p><strong>p1 &amp; p2</strong></p>
  
  <pre><code>p1 &lt;- ggplot(d1, aes(x=clarity,y=revenue, fill=cut)) +
    geom_bar(stat="identity") +
    labs(x="clarity", y="revenue") +
    facet_grid(. ~ cut) +
    scale_y_continuous(labels=dollar, expand=c(0,0)) + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1),
          axis.text.y = element_text(colour="#4B92DB"), 
          legend.position="bottom")
  
  p2 &lt;- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
    geom_point(size=6) + 
    labs(x="", y="number of stones") + expand_limits(y=0) +
    scale_y_continuous(labels=comma, expand=c(0,0)) +
    scale_colour_manual(name = '',values =c("red","green"), labels = c("Number of Stones"))+
    facet_grid(. ~ cut) +
    theme(axis.text.y = element_text(colour = "red")) +
    theme(panel.background = element_rect(fill = NA),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_rect(fill=NA,colour="grey50"),
          legend.position="bottom")
  </code></pre>
  
  <p><strong>Attempt to combine (based on example linked above)</strong>
  This fails in the first for loop, I suspect to the hard coding of geom_point.points, however I don't know how to make it suit my charts (or fluid enough to suit a variety of charts)</p>
  
  <pre><code># extract gtable
  g1 &lt;- ggplot_gtable(ggplot_build(p1))
  g2 &lt;- ggplot_gtable(ggplot_build(p2))
  
  combo_grob &lt;- g2
  pos &lt;- length(combo_grob) - 1
  combo_grob$grobs[[pos]] &lt;- cbind(g1$grobs[[pos]],
                                   g2$grobs[[pos]], size = 'first')
  
  panel_num &lt;- length(unique(d1$cut))
  for (i in seq(panel_num))
  {
     grid.ls(g1$grobs[[i + 1]])
    panel_grob &lt;- getGrob(g1$grobs[[i + 1]], 'geom_point.points',
                          grep = TRUE, global = TRUE)
    combo_grob$grobs[[i + 1]] &lt;- addGrob(combo_grob$grobs[[i + 1]], 
                                         panel_grob)
  }       
  
  
  pos_a &lt;- grep('axis_l', names(g1$grobs))
  axis &lt;- g1$grobs[pos_a]
  for (i in seq(along = axis))
  {
    if (i %in% c(2, 4))
    {
      pp &lt;- c(subset(g1$layout, name == paste0('panel-', i), se = t:r))
  
      ax &lt;- axis[[1]]$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.5, "cm")
      ax$grobs[[2]]$x &lt;- ax$grobs[[2]]$x - unit(1, "npc") + unit(0.8, "cm")
      combo_grob &lt;- gtable_add_cols(combo_grob, g2$widths[g2$layout[pos_a[i],]$l], length(combo_grob$widths) - 1)
      combo_grob &lt;- gtable_add_grob(combo_grob, ax,  pp$t, length(combo_grob$widths) - 1, pp$b)
    }
  }
  
  pp &lt;- c(subset(g1$layout, name == 'ylab', se = t:r))
  
  ia &lt;- which(g1$layout$name == "ylab")
  ga &lt;- g1$grobs[[ia]]
  ga$rot &lt;- 270
  ga$x &lt;- ga$x - unit(1, "npc") + unit(1.5, "cm")
  
  combo_grob &lt;- gtable_add_cols(combo_grob, g2$widths[g2$layout[ia,]$l], length(combo_grob$widths) - 1)
  combo_grob &lt;- gtable_add_grob(combo_grob, ga, pp$t, length(combo_grob$widths) - 1, pp$b)
  combo_grob$layout$clip &lt;- "off"
  
  grid.draw(combo_grob)
  </code></pre>
  
  <p><strong>EDIT to attempt to make workable for <code>facet_wrap</code></strong></p>
  
  <p>The following code still works with <code>facet_grid</code> using <code>ggplot2 2.0.0</code></p>
  
  <pre><code>g1 &lt;- ggplot_gtable(ggplot_build(p1))
  g2 &lt;- ggplot_gtable(ggplot_build(p2))
  
  pp &lt;- c(subset(g1$layout, name == "panel", se = t:r))
  g &lt;- gtable_add_grob(g1, g2$grobs[which(g2$layout$name == "panel")], pp$t,
                       pp$l, pp$b, pp$l)
  # axis tweaks
  ia &lt;- which(g2$layout$name == "axis-l")
  ga &lt;- g2$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")
  g &lt;- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
  g &lt;- gtable_add_grob(g, ax, unique(pp$t), length(g$widths) - 1)
  
  # Add second y-axis title
  ia &lt;- which(g2$layout$name == "ylab")
  ax &lt;- g2$grobs[[ia]]
  # str(ax) # you can change features (size, colour etc for these -
  # change rotation below
  ax$rot &lt;- 90
  g &lt;- gtable_add_cols(g, g2$widths[g2$layout[ia, ]$l], length(g$widths) - 1)
  g &lt;- gtable_add_grob(g, ax, unique(pp$t), length(g$widths) - 1)
  
  # Add legend to the code
  leg1 &lt;- g1$grobs[[which(g1$layout$name == "guide-box")]]
  leg2 &lt;- g2$grobs[[which(g2$layout$name == "guide-box")]]
  
  g$grobs[[which(g$layout$name == "guide-box")]] &lt;-
    gtable:::cbind_gtable(leg1, leg2, "first")
  
  grid.draw(g)
  </code></pre>