【问题标题】:ggplot facet_wrap bar chart and column widthggplot facet_wrap 条形图和列宽
【发布时间】:2021-07-08 10:10:34
【问题描述】:

我使用ggplotfacet_wrap() 创建了以下数据框和条形图。

group <- c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f')
condition_a <- c('occasionally', 'often', 'often', 'often', 'occasionally', 'often', 'often', 'occasionally', 'often', 'often', 'occasionally', 'occasionally', 'often', 'often', 'occasionally')
condition_c <- c('yes', 'yes', 'no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'no')

data.frame <- data_frame (group, condition_a, condition_c)

tiff("test.tiff", units="in", width=15, height=5, res=300)
ggplot(data.frame, aes(x = condition_a, fill = condition_c)) +
  scale_y_continuous(labels = scales::percent_format()) +
  geom_bar(position = position_fill()) +
  facet_wrap(~ group)
dev.off()

问题是列太宽了,我想并排放置所有这些图表。我尝试过 geom_col 和 geom_bar,但它们不起作用。

感谢您的帮助!

【问题讨论】:

  • 您是否尝试过在对tiff 的调用中减小宽度参数:ggplot 图像正在填充您的 tiff 图像区域。试试说width = 6。或者,如果您需要图像宽度为 15 英寸,请增加 tiff 高度以适应绘图的相对条宽和高度。
  • 感谢您的回答!如果我改变 tiff 宽度,柱子会变窄。但是仍然有并排的三个图表,其余的在下面?
  • 试试facet_wrap(~ group, nrow = 1)
  • 行得通!非常感谢!

标签: r ggplot2


【解决方案1】:

或者,您可以使用facet_grid( ~ group)。当使用facet_grid(group1 ~ group2) 时,这对于按两个组进行分面也很有用。

【讨论】:

    猜你喜欢
    • 2018-06-23
    • 2012-09-18
    • 2019-03-14
    • 1970-01-01
    • 2015-04-13
    • 2013-08-28
    • 2020-10-03
    • 2022-12-21
    • 1970-01-01
    相关资源
    最近更新 更多