How can I force ggplot's geom_tile to fill every facet?

前端 未结 1 1367
被撕碎了的回忆
被撕碎了的回忆 2020-12-18 01:13

I am using ggplot\'s geom_tile to do 2-D density plots faceted by a factor. Every facet\'s scale goes from the minimum of all the data to the maximum of all the data, but th

相关标签:
1条回答
  • 2020-12-18 02:03

    I think you're looking for a combination of scales = "free" and expand = c(0,0):

    ggplot(mydata) +
      aes(x=x,y=y) +
      stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
      facet_wrap(~ groupvar,scales = "free") + 
      scale_x_continuous(expand = c(0,0)) + 
      scale_y_continuous(expand = c(0,0))
    

    enter image description here

    EDIT

    Given the OP's clarification, here's one option via simply setting the panel background manually:

    ggplot(mydata) +
      aes(x=x,y=y) +
      stat_density2d(geom="tile", aes(fill = ..density..), contour = FALSE) +
      facet_wrap(~ groupvar) + 
      scale_fill_gradient(low = "blue", high = "red") + 
      opts(panel.background = theme_rect(fill = "blue"),panel.grid.major = theme_blank(),
           panel.grid.minor = theme_blank())
    

    enter image description here

    0 讨论(0)
提交回复
热议问题