我希望减少条形图的间距,并保持堆叠条形图的条形宽度。
堆叠条形图:
p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) +
geom_bar(stat = "identity", width =0.20)

然后我想通过position=position_dodge(0.9)更改条形图间距
p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) +
geom_bar(stat = "identity", position=position_dodge(0.9),width =0.20)此解决方案可以更改条形间隙,但条形未堆叠。那么如何改变条间距并保持条的宽度和堆叠呢?提前谢谢你!

我的数据:
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)))发布于 2015-05-11 13:52:47
position = position_dodge用于并排显示条形图的填充部分。我找不到一个特别的解决方案。然而,当我遇到这样的问题时,我会调整整个图形的宽度来调整条形图的宽度。考虑以下示例,并查看在调整宽度时保存的图形。希望这能有所帮助。关键是,如果您使条形宽度变窄,并减小条形之间的间距,最终您的图形宽度会减小。
ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
ggsave(filename = "trial1.png",plot = P,width=15,height = 10)

ggsave(filename = "trial2.png",plot = P,width=5,height = 10)

https://stackoverflow.com/questions/30159962
复制相似问题