如何手动调整切面大小 [英] How to adjust facet size manually

查看:148
本文介绍了如何手动调整切面大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含非常多样化数据的多面图.因此,某些构面只有1个x值,而另一些构面只有13个x值.我知道有一个参数space='free'通过它代表的数据调整每个构面的宽度.

I have a faceted plot with very diverse data. So some facets have only 1 x value, but some others have 13 x values. I know there is the parameter space='free' which adjusts the width of each facet by the data it represents.

我的问题是,可以手动调整此空间吗?由于某些方面太小,因此无法再读取方面中的标签.我做了一个可重复的例子来说明我的意思.

My question, is there a possibility to adjust this space manually? Since some of my facets are so small, it is no longer possible to read the labels in the facets. I made a little reproducible example to show what I mean.

df <- data.frame(labelx=rep(c('my long label','short'), c(2,26)),
                 labely=rep(c('a','b'), each=14),
                 x=c(letters[1:2],letters[1:26]),
                 y=LETTERS[6:7],
                 i=rnorm(28))
ggplot(df, aes(x,y,color=i)) +
  geom_point() +
  facet_grid(labely~labelx, scales='free_x', space='free_x')

因此,根据屏幕的不同,my long label构面将被压缩,因此您将无法再读取标签.

So depending on your screen, the my long label facet gets compressed and you can no longer read the label.

我在互联网上发现了一条帖子,该帖子似乎完全符合我的意愿,但是在ggplot2中似乎不再起作用.该帖子来自2010年.

I found a post on the internet which seems to do exactly what I want to do, but this seems to no longer work in ggplot2. The post is from 2010.

https://kohske.wordpress.com/2010/12/25/adjusting-the-relative-space-of-a-facet-grid/

他建议使用facet_grid(fac1 + fac2 ~ fac3 + fac4, widths = 1:4, heights = 4:1),因此widthsheights可以手动调整每个构面的大小.

He suggests to use facet_grid(fac1 + fac2 ~ fac3 + fac4, widths = 1:4, heights = 4:1), so widths and heights to adjust each facet size manually.

推荐答案

您可以使用网格图形调整ggplot对象的宽度

You can adjust the widths of a ggplot object using grid graphics

g = ggplot(df, aes(x,y,color=i)) +
  geom_point() +
  facet_grid(labely~labelx, scales='free_x', space='free_x')

library(grid)
gt = ggplot_gtable(ggplot_build(g))
gt$widths[4] = 4*gt$widths[4]
grid.draw(gt)

对于包含许多元素的复杂图形,确定要更改的宽度可能会有些麻烦.在这种情况下,需要扩展的是网格第4列,但这将因不同的图而异.确定更改哪个方法的方法有很多种,但是一种相当简单有效的方法是使用gtable软件包中的gtable_show_layout.

With complex graphs with many elements, it can be slightly cumbersome to determine which width it is that you want to alter. In this instance it was grid column 4 that needed to be expanded, but this will vary for different plots. There are several ways to determine which one to change, but a fairly simple and good way is to use gtable_show_layout from the gtable package.

gtable_show_layout(gt)

产生以下图像:

,其中我们可以看到左侧小平面位于第4列中.前3列为页边距,轴标题和轴标签+标记提供了空间.第5列是刻面之间的空间,第6列是右侧刻面.第7列至第12列用于右侧小平面标签,空格,图例和右页边距.

in which we can see that the left hand facet is in column number 4. The first 3 columns provide room for the margin, the axis title and the axis labels+ticks. Column 5 is the space between the facets, column 6 is the right hand facet. Columns 7 through 12 are for the right hand facet labels, spaces, the legend, and the right margin.

检查gtable的图形表示的另一种方法是简单地检查表本身.实际上,如果您需要使该过程自动化,那么这就是这样做的方法.因此,让我们看一下TableGrob:

An alternative to inspecting a graphical representation of the gtable is to simply inspect the table itself. In fact if you need to automate the process, this would be the way to do it. So lets have a look at the TableGrob:

gt
# TableGrob (13 x 12) "layout": 25 grobs
#     z         cells       name                                   grob
# 1   0 ( 1-13, 1-12) background        rect[plot.background..rect.399]
# 2   1 ( 7- 7, 4- 4)  panel-1-1               gTree[panel-1.gTree.283]
# 3   1 ( 9- 9, 4- 4)  panel-2-1               gTree[panel-3.gTree.305]
# 4   1 ( 7- 7, 6- 6)  panel-1-2               gTree[panel-2.gTree.294]
# 5   1 ( 9- 9, 6- 6)  panel-2-2               gTree[panel-4.gTree.316]
# 6   3 ( 5- 5, 4- 4)   axis-t-1                         zeroGrob[NULL]
# 7   3 ( 5- 5, 6- 6)   axis-t-2                         zeroGrob[NULL]
# 8   3 (10-10, 4- 4)   axis-b-1    absoluteGrob[GRID.absoluteGrob.329]
# 9   3 (10-10, 6- 6)   axis-b-2    absoluteGrob[GRID.absoluteGrob.336]
# 10  3 ( 7- 7, 3- 3)   axis-l-1    absoluteGrob[GRID.absoluteGrob.343]
# 11  3 ( 9- 9, 3- 3)   axis-l-2    absoluteGrob[GRID.absoluteGrob.350]
# 12  3 ( 7- 7, 8- 8)   axis-r-1                         zeroGrob[NULL]
# 13  3 ( 9- 9, 8- 8)   axis-r-2                         zeroGrob[NULL]
# 14  2 ( 6- 6, 4- 4)  strip-t-1                          gtable[strip]
# 15  2 ( 6- 6, 6- 6)  strip-t-2                          gtable[strip]
# 16  2 ( 7- 7, 7- 7)  strip-r-1                          gtable[strip]
# 17  2 ( 9- 9, 7- 7)  strip-r-2                          gtable[strip]
# 18  4 ( 4- 4, 4- 6)     xlab-t                         zeroGrob[NULL]
# 19  5 (11-11, 4- 6)     xlab-b titleGrob[axis.title.x..titleGrob.319]
# 20  6 ( 7- 9, 2- 2)     ylab-l titleGrob[axis.title.y..titleGrob.322]
# 21  7 ( 7- 9, 9- 9)     ylab-r                         zeroGrob[NULL]
# 22  8 ( 7- 9,11-11)  guide-box                      gtable[guide-box]
# 23  9 ( 3- 3, 4- 6)   subtitle  zeroGrob[plot.subtitle..zeroGrob.396]
# 24 10 ( 2- 2, 4- 6)      title     zeroGrob[plot.title..zeroGrob.395]
# 25 11 (12-12, 4- 6)    caption   zeroGrob[plot.caption..zeroGrob.397]

相关的位是

#         cells       name  
# ( 7- 7, 4- 4)  panel-1-1      
# ( 9- 9, 4- 4)  panel-2-1              
# ( 6- 6, 4- 4)  strip-t-1

,其中名称panel-x-y指的是x,y坐标中的面板,并且单元格给出了表中该命名面板的坐标(作为范围).因此,例如,左上方和左下方的面板均位于列单元格为4- 4的表单元格中. (仅在第四列中).左上方的条也位于单元格第4列中.

in which the names panel-x-y refer to panels in x, y coordinates, and the cells give the coordinates (as ranges) of that named panel in the table. So, for example, the top and bottom left-hand panels both are located in table cells with the column ranges 4- 4. (only in column four, that is). The left-hand top strip is also in cell column 4.

如果要使用此表以编程方式而不是手动方式找到相关宽度(以左上角,即"panel-1-1"为例),可以使用

If you wanted to use this table to find the relevant width programmatically, rather than manually, (using the top left facet, ie "panel-1-1" as an example) you could use

gt$layout$l[grep('panel-1-1', gt$layout$name)]
# [1] 4

这篇关于如何手动调整切面大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆