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

尤塞贝

facet_grid请允许我根据y轴上的项目数(space参数)来调整每个方面的宽度

df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

在此处输入图片说明

但是我想在其上面添加构面标签,因此我切换到facet_wrap,并丢失了space参数(构面具有相​​同的宽度):

ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

在此处输入图片说明

能否获得两全其美?

预先感谢您的帮助。

桑迪·穆斯普拉特(Sandy Muspratt)

可以手动完成。所需图中三个面板的高度比约为1:3:2。可以通过更改手把来调整三个面板的高度:

library(ggplot2)
library(grid)
df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))

p1 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

g1 = ggplotGrob(p1) 

g1$heights[[7]] = unit(1, "null")
g1$heights[[12]] = unit(3, "null")
g1$heights[[17]] = unit(2, "null")

grid.newpage()
grid.draw(g1)

或者,可以将高度设置为与原始图中的高度相同:

p2 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

g2 = ggplotGrob(p2) 

g1$heights[[7]] = g2$heights[[6]]
g1$heights[[12]] = g2$heights[[8]]
g1$heights[[17]] = g2$heights[[10]]

grid.newpage()
grid.draw(g1)

或者,可以不参考原始图来设置高度。他们可以根据数量来设置items每个labeldf并从@baptiste的答案中借用一些代码,以从与面板相对应的布局中选择项目:

# From 'df', get the number of 'items' for each 'label'.
# That is, the number y-breaks in each panel.
library(plyr)
N = dlply(df, .(label), function(x) length(row.names(x)))

# Get the items in the g1 layout corresponding to the panels.
panels1 <- g1$layout$t[grepl("panel", g1$layout$name)]

# Replace the default panel heights with relative heights
g1$heights[panels1] <- unit(N, "null")

## Draw g1
grid.newpage()
grid.draw(g1)

在此处输入图片说明

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

无法使用facet_wrap获得geom_text标签

来自分类Dev

facet_wrap,facet_grid-在构面中使用日期类型

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

使用facet_wrap()时颜色错误

来自分类Dev

y轴上的一个标签,第二个标签将其留在顶部。将facet_wrap与2个变量一起使用

来自分类Dev

使用facet_grid绘制多个以公共多边形为覆盖的空间多边形

来自分类Dev

使用带有facet_wrap的ggplot2显示不同的轴标签

来自分类Dev

向使用facet_wrap(ggplot2)创建的每个子图添加标签

来自分类Dev

如何在R中的facet_wrap标签中使用上标?

来自分类Dev

在ggplot2中使用labeller更改facet_wrap标签

来自分类Dev

如何使用 facet_grid 在 ggplot 中拟合 X 轴标签?

来自分类Dev

ggplot facet_grid标签上标

来自分类Dev

使用facet_wrap评估geom_text

来自分类Dev

在facet_wrap中使用geom_vline

来自分类Dev

如何正确使用facet_wrap中的labeller

来自分类Dev

将facet_wrap与for循环一起使用

来自分类Dev

在ggplot中使用facet_wrap时缩放y轴

来自分类Dev

通过使用facet_wrap将r平方成ggplot

来自分类Dev

使用facet_wrap时ggplot重新排序更改

来自分类Dev

从功能/数据集中使用facet_wrap?

来自分类Dev

使用facet_wrap时更改多个y比例

来自分类Dev

ggplot在使用`facet_wrap`时添加正态分布

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    无法使用facet_wrap获得geom_text标签

  5. 5

    facet_wrap,facet_grid-在构面中使用日期类型

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

    使用facet_wrap()时颜色错误

  12. 12

    y轴上的一个标签,第二个标签将其留在顶部。将facet_wrap与2个变量一起使用

  13. 13

    使用facet_grid绘制多个以公共多边形为覆盖的空间多边形

  14. 14

    使用带有facet_wrap的ggplot2显示不同的轴标签

  15. 15

    向使用facet_wrap(ggplot2)创建的每个子图添加标签

  16. 16

    如何在R中的facet_wrap标签中使用上标?

  17. 17

    在ggplot2中使用labeller更改facet_wrap标签

  18. 18

    如何使用 facet_grid 在 ggplot 中拟合 X 轴标签?

  19. 19

    ggplot facet_grid标签上标

  20. 20

    使用facet_wrap评估geom_text

  21. 21

    在facet_wrap中使用geom_vline

  22. 22

    如何正确使用facet_wrap中的labeller

  23. 23

    将facet_wrap与for循环一起使用

  24. 24

    在ggplot中使用facet_wrap时缩放y轴

  25. 25

    通过使用facet_wrap将r平方成ggplot

  26. 26

    使用facet_wrap时ggplot重新排序更改

  27. 27

    从功能/数据集中使用facet_wrap?

  28. 28

    使用facet_wrap时更改多个y比例

  29. 29

    ggplot在使用`facet_wrap`时添加正态分布

热门标签

归档