如何在ggplot2中减少堆积条形图的条间隙 [英] how to reduce bar gap of stacked bar plot in ggplot2

查看:3321
本文介绍了如何在ggplot2中减少堆积条形图的条间隙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想减少酒吧间隙并保持酒吧宽度的堆积酒吧情节。

堆积棒图:

  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(黄瓜-1,
黄瓜-2,茄子-1,茄子-2,胡椒(1L,2L,3L,4L,5L,
1L),.Label = c(1L,2L,3L,4L,5L,
1L),class =factor [3.19e-39.2],(2,4),(4,6),(6,8),(8,10)
),class = 因子),n = c(14729L,1670L,447L,131L,16L,20206L
),百分比= c(0.866768669452127,0.0982757606073089,0.0263049490966869,
0.00770905667039369,0.000941564173483199,0.9941039493293592)), = 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),largest_group_size = 5L,labels = structure(list(
plant = s (1:2,.Label = c(黄瓜-1,黄瓜-2,
茄子-1,茄子-2,胡椒-1,胡椒-2 ),class =factor)),class =data.frame,row.names = c(NA,
-2L),.Names =plant,vars = list(plant)))


解决方案

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)


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)

and then I want to change bar gaps by 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!

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

解决方案

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.

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)

这篇关于如何在ggplot2中减少堆积条形图的条间隙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆