调整strip.background的大小以匹配ggplot facet

编程入门 行业动态 更新时间:2024-10-14 18:15:41
调整strip.background的大小以匹配ggplot facet_wrap中的strip.text(Resize strip.background to match strip.text in ggplot facet_wrap)

我试图制作许多“小倍数”图。

(旁边/背景)我有太多的方面要在单个图中显示,所以我需要手动将它们子集化为单独的图。 我希望我能将所需的nrow和nrow给facet_wrap ,它会尽可能多地nrow给我。 事实并非如此。

因为我有很多方面,所以空间非常宝贵。 所以,我想减小刻面标签的大小。 很容易减少strip.text字体大小,但遗憾的是strip.background矩形没有字体大小,也没有提供大小调整参数(除了边框大小)。 将它设置为element_blank()实际上并没有删除为它预留的空间,它只是使相同的空间透明。

MWE:

set.seed(1) xcat = rep(1:10, 1000) yvalue1 = rep(runif(10,5,7),1000) yvalue2 = rep(runif(10,4,6),1000) id = rep(1:1000,each=10) df = data.frame(id,xcat,yvalue1,yvalue2) for(i in seq(1, length(unique(df$id)), by=100)){ print( ggplot(subset(df, id %in% unique(df$id)[i:(i+99)]), aes(x=xcat)) + annotate("line", y=yvalue1, x=xcat, colour="cornflowerblue") + annotate("line", y=yvalue2, x=xcat, colour="grey20") + theme(axis.text.x = element_text(size=5), axis.text.y = element_text(size=5), strip.text = element_text(size=5)) + facet_wrap(~id, ncol=10, nrow=10) + expand_limits(y=0)) }

关于如何减少strip.background的高度以使它不占用太多空间的任何想法? 理想情况下,它的大小将与id文本的确切高度相匹配。

作为奖励,如何减少刻面之间的间距? (我缺少facet_wrap中的设置吗?)

使用facet_wrap在RStudio中渲染也需要很长时间(没有facet_wrap,它会更快。

I am attempting to make lots of "small multiples" plots.

(Aside/Background) I have too many facets to display in a single plot so I need to manually subset them up into separate plots. I was hoping I could just pass the desired ncol and nrow to facet_wrap and it would make as many as I needed. That is not the case.

Because I have so many facets, space is at a premium. So, I wanted to reduce the size of the facet labels. Easy enough to reduce the strip.text font size, but sadly the strip.background rectangle does not size with the font nor does it provide sizing parameters (apart from border size). Setting it to element_blank() doesn't actually remove the space that is set aside for it, it just makes the same space transparent.

MWE:

set.seed(1) xcat = rep(1:10, 1000) yvalue1 = rep(runif(10,5,7),1000) yvalue2 = rep(runif(10,4,6),1000) id = rep(1:1000,each=10) df = data.frame(id,xcat,yvalue1,yvalue2) for(i in seq(1, length(unique(df$id)), by=100)){ print( ggplot(subset(df, id %in% unique(df$id)[i:(i+99)]), aes(x=xcat)) + annotate("line", y=yvalue1, x=xcat, colour="cornflowerblue") + annotate("line", y=yvalue2, x=xcat, colour="grey20") + theme(axis.text.x = element_text(size=5), axis.text.y = element_text(size=5), strip.text = element_text(size=5)) + facet_wrap(~id, ncol=10, nrow=10) + expand_limits(y=0)) }

Any ideas on how I could reduce the height of that strip.background so that it doesn't take up so much space? Ideally it would be sized to the exact height of the id text.

As a bonus, how to reduce the spacing between facets? (Is there a setting in facet_wrap that I'm missing?)

It also takes a REALLY long time to render in RStudio with facet_wrap (without facet_wrap it is much faster.

最满意答案

您想要的结果是可以修改grobs创建的ggplot 。 这个过程有些混乱,需要进行一些手动微调:

抛开循环,这对问题没有影响,我们可以从减少方面之间的间距开始。 通过将panel.spacing参数添加到theme可以轻松完成。

library(ggplot2) d <- ggplot(subset(df, id %in% unique(df$id)[1:(100)]), aes(x=xcat)) + annotate("line", y=yvalue1, x=xcat, colour="cornflowerblue") + annotate("line", y=yvalue2, x=xcat, colour="grey20") + theme(axis.text.x = element_text(size=5), axis.text.y = element_text(size=5), strip.text = element_text(size=5), panel.spacing = unit(.5, 'pt')) + facet_wrap(~id, ncol=10, nrow=10) + expand_limits(y=0)

然后我们必须生成一个ggplot2图表grob。 这将创建构成绘图的各个元素的列表。

g <- ggplotGrob(d) head(g, 5) # TableGrob (5 x 43) "layout": 13 grobs # z cells name grob # 1 3 ( 5- 5, 4- 4) axis-t-1-1 zeroGrob[NULL] # 2 3 ( 5- 5, 8- 8) axis-t-2-1 zeroGrob[NULL] # 3 3 ( 5- 5,12-12) axis-t-3-1 zeroGrob[NULL] # 4 3 ( 5- 5,16-16) axis-t-4-1 zeroGrob[NULL] # 5 3 ( 5- 5,20-20) axis-t-5-1 zeroGrob[NULL] # 6 3 ( 5- 5,24-24) axis-t-6-1 zeroGrob[NULL] # 7 3 ( 5- 5,28-28) axis-t-7-1 zeroGrob[NULL] # 8 3 ( 5- 5,32-32) axis-t-8-1 zeroGrob[NULL] # 9 3 ( 5- 5,36-36) axis-t-9-1 zeroGrob[NULL] # 10 3 ( 5- 5,40-40) axis-t-10-1 zeroGrob[NULL] # 11 4 ( 4- 4, 4-40) xlab-t zeroGrob[NULL] # 12 8 ( 3- 3, 4-40) subtitle zeroGrob[plot.subtitle..zeroGrob.77669] # 13 9 ( 2- 2, 4-40) title zeroGrob[plot.title..zeroGrob.77668]

我们的目标是找到并固定相对于条带的高度。

for(i in 1:length(g$grobs)) { if(g$grobs[[i]]$name == 'strip') g$grobs[[i]]$heights <- unit(5, 'points') }

不幸的是, strip grobs也受到主要图形划分的不同heights的影响,因此我们也必须减少那些。 我没有找到一种方法来以编程方式识别哪个高度指的是条带,但是检查g$heights很快就能找出我们需要的东西。

g$heights # [1] 5.5pt 0cm 0cm 0cm 0cm # [6] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [11] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [16] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [21] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [26] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [31] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [36] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [41] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [46] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [51] 0.518897450532725cm 1null 0.306264269406393cm 1grobheight 0cm # [56] 5.5pt strip_grob_position <- seq(6, length(g$heights) - 5, 5) for(i in strip_grob_position) { g$heights[[i]] = unit(.05, 'cm') }

我们终于可以得出结果了:

library(grid) grid.newpage() grid.draw(g)

在此处输入图像描述

我不是这个主题的专家,所以我可能错误地或不准确地说或解释了一些东西。 此外,可能有更好的(更简单和更ggplot-ish)方法来获得结果。

The result you want is achievable modifying the grobs created by ggplot. The process is somewhat messy and bound to need some manual fine tuning:

Setting aside the loop, that is uninfluential on the problem, we can start by reducing the spacing between facet. That is easily done by adding the panel.spacing argument to the theme.

library(ggplot2) d <- ggplot(subset(df, id %in% unique(df$id)[1:(100)]), aes(x=xcat)) + annotate("line", y=yvalue1, x=xcat, colour="cornflowerblue") + annotate("line", y=yvalue2, x=xcat, colour="grey20") + theme(axis.text.x = element_text(size=5), axis.text.y = element_text(size=5), strip.text = element_text(size=5), panel.spacing = unit(.5, 'pt')) + facet_wrap(~id, ncol=10, nrow=10) + expand_limits(y=0)

We then have to generate a ggplot2 plot grob. This create a list of individual elements composing the plot.

g <- ggplotGrob(d) head(g, 5) # TableGrob (5 x 43) "layout": 13 grobs # z cells name grob # 1 3 ( 5- 5, 4- 4) axis-t-1-1 zeroGrob[NULL] # 2 3 ( 5- 5, 8- 8) axis-t-2-1 zeroGrob[NULL] # 3 3 ( 5- 5,12-12) axis-t-3-1 zeroGrob[NULL] # 4 3 ( 5- 5,16-16) axis-t-4-1 zeroGrob[NULL] # 5 3 ( 5- 5,20-20) axis-t-5-1 zeroGrob[NULL] # 6 3 ( 5- 5,24-24) axis-t-6-1 zeroGrob[NULL] # 7 3 ( 5- 5,28-28) axis-t-7-1 zeroGrob[NULL] # 8 3 ( 5- 5,32-32) axis-t-8-1 zeroGrob[NULL] # 9 3 ( 5- 5,36-36) axis-t-9-1 zeroGrob[NULL] # 10 3 ( 5- 5,40-40) axis-t-10-1 zeroGrob[NULL] # 11 4 ( 4- 4, 4-40) xlab-t zeroGrob[NULL] # 12 8 ( 3- 3, 4-40) subtitle zeroGrob[plot.subtitle..zeroGrob.77669] # 13 9 ( 2- 2, 4-40) title zeroGrob[plot.title..zeroGrob.77668]

Our objective is then to find and fix the height of the ones relative to the strips.

for(i in 1:length(g$grobs)) { if(g$grobs[[i]]$name == 'strip') g$grobs[[i]]$heights <- unit(5, 'points') }

Unfortunately the strip grobs are also affected by the different heights the the main plot is divided into, so we have to reduce that ones also. I haven't found a way to identify programmatically which heights refers to the strip, but inspecting g$heights is quite fast to figure out which we need.

g$heights # [1] 5.5pt 0cm 0cm 0cm 0cm # [6] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [11] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [16] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [21] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [26] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [31] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [36] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [41] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [46] 0.518897450532725cm 1null 0cm 0.5pt 0cm # [51] 0.518897450532725cm 1null 0.306264269406393cm 1grobheight 0cm # [56] 5.5pt strip_grob_position <- seq(6, length(g$heights) - 5, 5) for(i in strip_grob_position) { g$heights[[i]] = unit(.05, 'cm') }

We can finally draw the result:

library(grid) grid.newpage() grid.draw(g)

enter image description here

I am not an expert at all in the subject, so I may have said or explained something wrongly or inaccurately. Moreover there may be better (and simpler and more ggplot-ish) ways to obtain the result.

更多推荐

本文发布于:2023-07-07 21:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1068445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   background   strip   ggplot   facet

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!