摆脱条形图中的内部条形线 [英] Getting Rid of Internal Bar Lines in a Bar Plot

查看:74
本文介绍了摆脱条形图中的内部条形线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中的ggplot2中进行基本的条形图绘制.

I'm doing a basic bar plot in ggplot2 in R.

代码是:

  ggplot(shootings,aes(x=Year,y= Injured))+geom_bar(stat="identity",fill="#CC6666",colour="black")+ggtitle("Injured")

它呈现如下:

问题:

  1. 条形图是否由内部水平线隔开或重叠?

  1. Are the bars separated by an internal horizontal line stacked or overlapping?

假定它们是堆叠在一起的(例如,1984年的酒吧代表2起事件-一起总共19人受伤,一起总共1人受伤,总共20起),我怎么知道摆脱了y = 19处的水平线,所以我可以显示一个值为20的条形图?

Assuming they are stacked (for example, the bar at year 1984 represents 2 incidents - one with a total of 19 injured and one with a total of 1 injured for a grand total of 20), how do I get rid of the horizontal line at value y = 19 so I can display a single bar with value of 20?

谢谢.

@Didzis Elferts奇怪的是,当我运行您的代码时(除了删除图例外),我得到了:

@Didzis Elferts Strangely when I run your code (with an addition to remove the legend) I get this:

2个奇怪的东西:它具有奇怪的美学(我想我当然可以修改),但是总和似乎没有用(1984年现在总共显示为19,而不是20.我尝试输入x ,y值,但没有用).是否需要将它们放进去?谢谢.

2 weird things: It has strange aesthetics (which I suppose I can certainly modify) but the sum doesn't appear to be working (1984 now shows only as a total of 19 - not 20. I tried to put in x, y values but it didn't work). Do they need to be put in? Thanks.

@Didzis Elferts使用我的代码修改后的答案(带有stat_summary):

@Didzis Elferts Your modified answer (with stat_summary) with my code:

ggplot(shootings,aes(Year,Injured))+ stat_summary(fun.y=sum,geom="bar",colour="black",fill="#CC6666",show.legend = FALSE)+ ggtitle("Injured")

工作完美!

推荐答案

如果需要每年对Injured的值求和,可以将stat_summary()geom="bar"结合使用并设置fun.y=sum

If you need to sum the values of Injured for each year, you can use stat_summary() with geom="bar" and set fun.y=sum

ggplot(shootings,aes(Year,Injured))+
  stat_summary(fun.y=sum,geom="bar",fill="#CC6666",colour="black")+
  ggtitle("Injured")

这篇关于摆脱条形图中的内部条形线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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