【问题标题】:geom_tile and facet_grid/facet_wrap for same height of tilesgeom_tile 和 facet_grid/facet_wrap 用于相同高度的瓷砖
【发布时间】:2014-01-05 11:01:47
【问题描述】:

使用 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))

但面板之间的瓷砖高度不同。我尝试使用 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))

瓷砖高度的问题已解决,但面板(第 1 组 ... 第 3 组)的标签不在面板顶部。是否可以使用 facet_grid 更改面板标签的位置?或结合 facet_grid 和 facet_wrap ? 感谢您的帮助,对不起我的英语!

【问题讨论】:

标签: r ggplot2 facet-wrap


【解决方案1】:

ggforce 包有一个简洁的小功能,称为facet_col。它不需要搞乱grid 包!

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

library(ggplot2)
library(ggforce)

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)))

base_size <- 9

ggplot(data.frame(dataSta), aes(factor(month), sites)) + 
  geom_tile(aes(fill = value), colour = "black") +

  # Here's the line to alter:
  facet_col(vars(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.spacing = unit(0,"lines"),
        strip.text = element_text(colour="red3", size=10, face=2))

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 如果这应该作为一个答案,请发布一个完整的解决方案,引用该问题并在此处使用它的代表,而不是仅仅链接到另一个问题。后者最好在评论中完成。
    • OP 想要每个面板顶部的构面标签,facet_grid 不允许您这样做。
    【解决方案3】:

    您可以在绘图前查看 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)
    

    【讨论】:

    • 非常感谢,这正是我要找的!效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2018-05-28
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    相关资源
    最近更新 更多