Windstorm1981
Windstorm1981

Reputation: 2680

Getting Rid of Internal Bar Lines in a Bar Plot

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:

enter image description here

Questions:

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

  2. 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:

enter image description here

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!

enter image description here

Upvotes: 2

Views: 4497

Answers (1)

Didzis Elferts
Didzis Elferts

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

Related Questions