title: ggplot facet_grid panel.spacing w/ axis label position = "right"
body: <p>I'm creating a heatmap/tile plot with faceting on both the <code>x</code> and <code>y</code> axes, and for some reason the spacing of the panel labels is not correctly matching the spacing of the data panels.  </p>
  
  <p>Here is example data and a plot. </p>
  
  <pre><code>library(data.table)
  td = data.table(Sym=rep(paste0("sym",1:14),141),
                  sample=rep(paste0("sample",1:141), 14),
                  value = sample(x=0:100, size=141*14,replace=T)/100,
                  group = rep(c(rep(1,12),rep(2,34),rep(3,28),rep(4,27),rep(5,40)),14)
             )
  td[, category := ifelse(Sym %in% paste0("sym",1:5), "cat1",
                          ifelse(Sym %in% paste0("sym",6:10), "cat2",
                                 ifelse(Sym %in% paste0("sym",11:14), "cat3",NA)))]
  
  p = ggplot(td, 
         aes(x=sample,y=Sym, fill=value))+
    geom_tile()+
    facet_grid(category ~ group,scales = "free",space = "free", switch = "y")+
    scale_y_discrete(position = "right")+
    theme(axis.title = element_blank(),
          axis.ticks = element_blank(),
          axis.text.x = element_blank(),
          axis.line = element_blank(),
          strip.text.y = element_text(angle=180),
          strip.text.x = element_text(color="white"))
  p
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/yT9B7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yT9B7.png" alt="unequal facets"></a></p>
  
  <p>Notice how the y axis facets are not perfectly aligned with the tileplot of the data.  I tried to change the panel spacing in the theme, and the issue still persists:</p>
  
  <pre><code>p+theme(panel.spacing = unit(0,"lines"))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/1TcTY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1TcTY.png" alt="after changing panel spacing"></a></p>
  
  <p>Finally, I can fix it by resetting the y axis scale:</p>
  
  <pre><code>p+scale_y_discrete(position="right", expand = c(0,0))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hT729.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hT729.png" alt="enter image description here"></a></p>
  
  <p>It looks like this is caused by me setting the y axis labels to plot on the right side of the plot using <code>scale_y_discrete(position="right")</code>.  Is there a better way to set the y-axis labels to plot on the right side without it affecting the facet spacing?</p>