对于相同高度的瓷砖,geom_tile和facet_grid / 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))

在此处输入图片说明

但是面板之间的瓷砖高度不同。我尝试使用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吗?感谢您的帮助,对不起我的英语!

浸礼会

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

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

对于相同高度的瓷砖,geom_tile和facet_grid / facet_wrap

来自分类Dev

ggplot中的facet_wrap for sf

来自分类Dev

防止在facet_wrap()中重新排序

来自分类Dev

facet_wrap的配对数据

来自分类Dev

R:没有因素的facet_wrap()

来自分类Dev

使用facet_wrap()时颜色错误

来自分类Dev

ggplot2中的facet_wrap()和facet_grid()有什么区别?

来自分类Dev

`geom_abline`和`facet_wrap`似乎不兼容

来自分类Dev

为什么facet_grid有效,而facet_wrap不起作用?

来自分类Dev

使用facet_grid的“顶部标签”,或者使用facet_wrap的“空间选项”

来自分类Dev

Scales =“ free”适用于facet_wrap,但不适用于facet_grid

来自分类Dev

使用facet_wrap评估geom_text

来自分类Dev

在facet_wrap中使用geom_vline

来自分类Dev

在facet_wrap的某些面板上的geom_rect

来自分类Dev

r facet_wrap无法与geom_point正确分组

来自分类Dev

无法使用facet_wrap获得geom_text标签

来自分类Dev

ggplot2:如何获取facet_grid()的labeller = label_both和facet_wrap()的ncol选项的合并功能?

来自分类Dev

自动更改facet_wrap()的行和列

来自分类Dev

GGplot_annotate 和 facet_wrap 函数

来自分类Dev

使用 facet_wrap 和 gather 时重新排序图

来自分类Dev

position_dodge2 和 facet_wrap

来自分类Dev

ggplot2中的嵌套facet_wrap()

来自分类Dev

如何使用facet_wrap产生的空白空间?

来自分类Dev

ggparcoord中带有facet_wrap的多条色线

来自分类Dev

将facet_wrap分割成多页PDF

来自分类Dev

相当于Lattice中的facet_wrap

来自分类Dev

如何正确使用facet_wrap中的labeller

来自分类Dev

如何在facet_wrap中更改构面标签

来自分类Dev

将facet_wrap与for循环一起使用

Related 相关文章

热门标签

归档