Reputation: 2680
I'm doing a basic bar plot in ggplot2 in R.
The code is:
ggplot(shootings,aes(x=Year,y= Injured))+geom_bar(stat="identity",fill="#CC6666",colour="black")+ggtitle("Injured")
It renders like this:
Questions:
Are the bars separated by an internal horizontal line stacked or overlapping?
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?
Thank you in advance.
EDIT:
@Didzis Elferts Strangely when I run your code (with an addition to remove the legend) I get this:
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.
Edit 2:
@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")
Worked perfectly!
Upvotes: 2
Views: 4497
Reputation: 98449
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")
Upvotes: 7