geom_tile和facet_grid / facet_wrap用于相同高度的图块

时间:2013-12-17 15:36:42

标签: r ggplot2 facet-wrap

使用ggplot,我想用面板表示图形图块,但每个面板使用相同的高度图块。 我有这张图:

dataSta <- list(sites=rep(paste("S", 1:31),each=12), month=rep(1:12,31), value=round(runif(31*12, min=0, max=3000)), panel=c(rep("Group 1",16*12),rep("Group 2", 12*12), rep("Group 3", 3*12)))

    library(ggplot2)
    library(grid)
    base_size <- 9

    windows()
    ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
      geom_tile(aes(fill = value), colour = "black")+
      facet_wrap(~panel, scale="free_y", nrow=3)+
      theme_grey(base_size = base_size) +  
      labs(x = "",y = "") + 
      scale_x_discrete(expand = c(0, 0)) +    
      scale_y_discrete(expand = c(0, 0)) +    
      theme(legend.title = element_blank(),
            axis.ticks = element_blank(),     
            axis.text.x = element_text(size = base_size *0.8, hjust = 0),
            panel.margin = unit(0,"lines"),
            strip.text = element_text(colour="red3", size=10, face=2))

enter image description here

但是瓷砖的高度在面板之间是不同的。我尝试使用facet_grid:

windows()
ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
  geom_tile(aes(fill = value), colour = "black")+
  facet_grid(panel~., scales="free_y", space="free")+
  theme_grey(base_size = base_size) +  
  labs(x = "",y = "") + 
  scale_x_discrete(expand = c(0, 0)) +    
  scale_y_discrete(expand = c(0, 0)) +    
  theme(legend.title = element_blank(),
        axis.ticks = element_blank(),     
        axis.text.x = element_text(size = base_size *0.8, hjust = 0),
        panel.margin = unit(0,"lines"),
        strip.text = element_text(colour="red3", size=10, face=2))

enter image description here 解决了瓷砖高度的问题,但面板(组1 ...组3)的标签不在面板的顶部。是否可以使用facet_grid更改面板标签的位置?或者组合facet_grid和facet_wrap? 谢谢你的帮助,对不起我的英文!

3 个答案:

答案 0 :(得分:17)

您可以在绘图之前查看ggplot包含的内容,并相应地重新调整面板。

g <- ggplot_build(p) 
## find out how many y-breaks are in each panel
## to infer the number of tiles
vtiles <- sapply(lapply(g$panel$ranges, "[[", "y.major"), length)

## convert the plot to a gtable object 
gt <- ggplot_gtable(g)
## find out which items in the layout correspond to the panels
## we refer to the "t" (top) index of the layout
panels <- gt$layout$t[grepl("panel", gt$layout$name)]
## replace the default panel heights (1null) with relative sizes 
## null units scale relative to each other, so we scale with the number of tiles
gt$heights[panels] <-lapply(vtiles, unit, "null")
## draw on a clean slate
library(grid)
grid.newpage()
grid.draw(gt)

enter image description here

答案 1 :(得分:0)

我花了一些时间来找到更简单的解决方案,它实际上是facet_grid的一部分,您可以在其中设置space = "free_y"。有关详情,请访问recent question

答案 2 :(得分:0)

<fieldset class='item'> <input class='price' value='$5.00'> </fieldset> <div class='item'> <input class='price' value='2,00'> </div> <section class='item'> <output class='price'>0.6</output> </section> <article class='item'> <p class='price'>8,00</p> </article> <div class='item'> <p class='price'>£3,00</p> </div> <aside class='item'> <div>2999</div> </aside> <nav> <fieldset class='price'>99</fieldset> </nav> 包有一个简洁的小函数,称为 ggforce。它不需要弄乱 facet_col 包!

您所要做的就是用适当的 grid 替代方法替换对 facet_grid 的调用:

facet_col

enter image description here