1

I want to overlay 3 geom_bar to make clear an evolution over 3 years. My data is as follows for each year:

Example : PerfDist2021 (my dataframe for 2021)

Districts Perf
1 40
2 30
3 60

On my Yaxis I have the performance (in %) and on the Xaxis I have the number corresponding to the district (from 1 to 25 and there is also a 31th).

I made this :

ggplot(data=NULL, aes(Districts, Perf)) + 
  geom_bar(aes(fill = "2019"), data = PerfDist2019, stat="identity" ,alpha = 0.5, col="red") +
  geom_bar(aes(fill = "2020" ), data = PerfDist2020, stat="identity", alpha = 0.5, col="green") +
  geom_bar(aes(fill = "2021" ), data = PerfDist2021, stat="identity", alpha = 0.5, col="blue") 
  • But first, I can't see all my districts, I don't know how to get them all visible it's like R erase some or is just not precise with my Xaxis (see picture in link).

  • Secondly, I don't know how to change the color of the geom_bar, I can just change the color of the frame of the bar with col=... , and the data is not very readable this way.

  • Third, the colours blend together, it is sometimes hard to distinguish the three. I tried with several combination of colors, it is always the same. Is there a way to avoir this issue of mixing colors ? Thanks you

What I did

Thanks you for your help !

PS : You can ask for any precision !

Léo
  • 11
  • 2
  • 1
    Maybe you can change the type of Districts to factor, e.g. PerfDist2019$Districts <- factor(PerfDist2019$Districts, levels = 1:31). – h45 Nov 02 '22 at 03:33
  • Hello Léo! In SO, if a new question has appeared on top of an old one, you either: (i) edit your original question and add the new one at the bottom; or (ii) post a completely new question. You don't post it as an answer because it is not one. I've edited your question for you. – Ricardo Semião e Castro Nov 02 '22 at 14:21
  • Oh sorry for that, I'm a beginner in stack as well :D . – Léo Nov 02 '22 at 16:29

1 Answers1

0

fill is the aesthetic that controls the color of the bars. color controls colors of lines, in this case, the frames, as you noted. So you want to delete the col = arguments and add scale_fill_manual to associate which color each custom fill name should have.

Also, the three repeated geom_bar isn't too good, if you had 12 years you wouldn't want to have 12 geom_bar. To change that, you can rbind your three datasets, and add a column specifying from which year it comes from, and than saying to ggplot to fill the bars following that column.

Lastly, as @h45 said, you can change the type of Districts to factor to avoid the missing levels.

PerfDist = rbind(
  cbind(PerfDist2019, Year = "2019"),
  cbind(PerfDist2019, Year = "2020"),
  cbind(PerfDist2019, Year = "2021"))

PerfDist$Districts <- factor(PerfDist$Districts, levels = 1:31)
  
ggplot(PerfDist, aes(Districts, Perf, fill = Year)) + 
  geom_bar(stat="identity", position = "identity", alpha = 0.5) +
  scale_fill_manual(c("red", "green", "blue"))

Thats it for the answer, now just a note for you to improve your next answers, please read how to make a great r reproducible example.

  • Okay thanks you I will try and I come back ! – Léo Nov 02 '22 at 08:36
  • Thanks you for your help, I tried your code, it works but it don't produce an overlaying, for each district it plot bars on an additive way. Do you see what I mean ? I really want to make an overlay for each district of the 3 years. – Léo Nov 02 '22 at 16:28
  • Oh i see. That happened because `geom_bar` by default has the `position` argument set to `"stack"` (which is a shorthand for `position_stack()`). If you change it to `position = "identity"` you get your original graph back. As for the mixing colors, that is a side effect of the transparency of the bars. I guess you want the bars to be in a different order for each x-axis tick, which is unusual for ggplot. I opened a [new question](https://stackoverflow.com/questions/74296730/barplot-with-different-factor-order-for-each-x-axis-tick) about how to do it, you should follow it @Léo – Ricardo Semião e Castro Nov 02 '22 at 23:43
  • 1
    Thanks you a lot, I found exactly what I searched in the first plot of Ric Villalba, I cannot comment the other question, but thanks you all ! – Léo Nov 04 '22 at 19:42