首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ggplot2中减小叠加条形图的条距

如何在ggplot2中减小叠加条形图的条距
EN

Stack Overflow用户
提问于 2015-05-11 13:02:56
回答 1查看 1.8K关注 0票数 1

我希望减少条形图的间距,并保持堆叠条形图的条形宽度。

堆叠条形图:

代码语言:javascript
复制
p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) + 
  geom_bar(stat = "identity", width =0.20)

然后我想通过position=position_dodge(0.9)更改条形图间距

代码语言:javascript
复制
p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) + 
  geom_bar(stat = "identity", position=position_dodge(0.9),width =0.20)

此解决方案可以更改条形间隙,但条形未堆叠。那么如何改变条间距并保持条的宽度和堆叠呢?提前谢谢你!

我的数据:

代码语言:javascript
复制
structure(list(plant = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("Cucumber-1", 
"Cucumber-2", "Eggplant-1", "Eggplant-2", "Pepper-1", "Pepper-2"
), class = "factor"), group = structure(c(1L, 2L, 3L, 4L, 5L, 
1L), .Label = c("[3.19e-39,2]", "(2,4]", "(4,6]", "(6,8]", "(8,10]"
), class = "factor"), n = c(14729L, 1670L, 447L, 131L, 16L, 20206L
), percentage = c(0.866768669452127, 0.0982757606073089, 0.0263049490966869, 
0.00770905667039369, 0.000941564173483199, 0.941039493293592)), .Names = c("plant", 
"group", "n", "percentage"), class = c("grouped_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L), vars = list(plant), drop = TRUE, indices = list(
    0:4, 5L), group_sizes = c(5L, 1L), biggest_group_size = 5L, labels = structure(list(
    plant = structure(1:2, .Label = c("Cucumber-1", "Cucumber-2", 
    "Eggplant-1", "Eggplant-2", "Pepper-1", "Pepper-2"), class = "factor")), class = "data.frame", row.names = c(NA, 
-2L), .Names = "plant", vars = list(plant)))
EN

回答 1

Stack Overflow用户

发布于 2015-05-11 13:52:47

position = position_dodge用于并排显示条形图的填充部分。我找不到一个特别的解决方案。然而,当我遇到这样的问题时,我会调整整个图形的宽度来调整条形图的宽度。考虑以下示例,并查看在调整宽度时保存的图形。希望这能有所帮助。关键是,如果您使条形宽度变窄,并减小条形之间的间距,最终您的图形宽度会减小。

代码语言:javascript
复制
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
ggsave(filename = "trial1.png",plot = P,width=15,height = 10)

代码语言:javascript
复制
ggsave(filename = "trial2.png",plot = P,width=5,height = 10)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30159962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档