如何减少ggplot2中堆积条形图的条形间隙

时间:2021-03-12 14:56:01

I would like to reduce bar gap and keep bar width of stacked bar plot.

我想减少条间隙并保持堆积条形图的条宽。

Stacked bar plot:

堆积条形图:

p <- ggplot(dat, aes(x = plant, y = percentage*100, fill = group)) + 
  geom_bar(stat = "identity", width =0.20)

如何减少ggplot2中堆积条形图的条形间隙

and then I want to change bar gaps by position=position_dodge(0.9):

然后我想通过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)

This solution can change bar gaps, but bars were unstacked. So how to change bar gap and keep bar width and stacked? Thank you in advance!

这个解决方案可以改变条形间隙,但是条形图被拆开了。那么如何改变条形间隙并保持条形宽度和堆叠?先感谢您!

如何减少ggplot2中堆积条形图的条形间隙

My data:

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)))

1 个解决方案

#1


position = position_dodge is used to show the fill part of bar graphs side by side. I cannot particularly find a solution to this. However, when I face such problem, I adjust the width of the entire graph to adjust width of the bars. Consider the following example and see the graphs that are saved when you adjust the width. Hope this helps. The point is if you make the bar width narrower and decrease the space between the bars, ultimately your graph width decreases.

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

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
ggsave(filename = "trial1.png",plot = P,width=15,height = 10)

如何减少ggplot2中堆积条形图的条形间隙

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

如何减少ggplot2中堆积条形图的条形间隙

#1


position = position_dodge is used to show the fill part of bar graphs side by side. I cannot particularly find a solution to this. However, when I face such problem, I adjust the width of the entire graph to adjust width of the bars. Consider the following example and see the graphs that are saved when you adjust the width. Hope this helps. The point is if you make the bar width narrower and decrease the space between the bars, ultimately your graph width decreases.

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

ggplot(diamonds, aes(clarity, fill=cut)) + geom_bar()
ggsave(filename = "trial1.png",plot = P,width=15,height = 10)

如何减少ggplot2中堆积条形图的条形间隙

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

如何减少ggplot2中堆积条形图的条形间隙