DR15
DR15

Reputation: 693

How to fill each bar in geom_bar using "dodge"

I'm struggling on how can I fill the bars in geom_bar, using other variable that I used in "fill" variable.

Here is my code:

ggplot() +
  geom_bar(data=df ,width = .9, aes(x = SEM_PRI, y = acum, fill = CLASSI_FIN, group=CLASSI_FIN), color="transparent",position = "dodge",stat="identity") +
  scale_x_continuous(breaks = 1:25,labels = labelss,expand = expansion(add = c(.1, .1))) +
  scale_fill_manual("",c("SRAG-COVID","SRAG-não especificada"), values = c("dodgerblue3","gold"))+
  theme(legend.position = "bottom", legend.direction = "horizontal")

enter image description here I'd like to fill each bar above with the value of the variable acumobito with the color black and insert that color in the legend too.

Here is a manual example of what I want (I jus painted the last bar, but I'd like to have this in all bars, the value painted in black is the variable acumobito.

enter image description here Any hint on how can I do that?

Here is my data:

df = data.frame(                SEM_PRI = c(9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,
                                                                                   21,22,23,24,25,1,2,3,4,5,6,7,8,10),
                CLASSI_FIN = c("SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID",           
                               "SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID" ,          
                               "SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID" ,          
                               "SRAG-COVID","SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada",
                               "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada",
                               "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada",
                               "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada",
                               "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada", "SRAG-não especificada",
                               "SRAG-não especificada", "SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID",           
                               "SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID","SRAG-COVID"),
                acum = c(1,10,29,62,80,105,129,155,183,237,297,380,477,594,665,679,20,40,57,83,99,119,139,160,
                            197,241,355,667,968,1240,1466,1715,1975,2229,2423,2583,2714,2841,2953,3012,3026,0,0,0,0,0,0,0,
                            0,1),
                acumobito = c(1,2,4,8,11,20,27,29,37,47,53,67,80,89,95,96,1,1,3,4,7,7,9,9,10,11,16,47,86,119,
                              142,186,231,265,289,307,330,354,367,377,379,0,0,0,0,0,0,0,0,1))

colnames(df) = c("SEM_PRI","CLASSI_FIN","acum","acumobito")

labelss = c("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21",
"22","23","24","25")

Upvotes: 0

Views: 142

Answers (1)

chemdork123
chemdork123

Reputation: 13843

In your case, one simple way that seems to work would be to overlay the black bars on top of the geom_bar. Overplotting is not always recommended, but it does seem to work in this case.

ggplot() +
  geom_bar(data=df ,width = .9,
      aes(x = SEM_PRI, y = acum, fill = CLASSI_FIN, group=CLASSI_FIN),
      color="transparent",position = "dodge",stat="identity") +
  geom_bar(data=df, width = .9,
      aes(x = SEM_PRI, y=acumobito, fill='blahblah', group=CLASSI_FIN),
      color='transparent', position='dodge', stat='identity') +
  scale_x_continuous(breaks = 1:25,labels = labels,expand = expansion(add = c(.1, .1))) +
  scale_fill_manual("",c("acumobito", "SRAG-COVID","SRAG-não especificada"),
    values = c("black","dodgerblue3", "gold"))+
  theme(legend.position = "bottom", legend.direction = "horizontal")

enter image description here

Notably, the new geom_bar needs to be after your original one to ensure the black bars are drawn on top of the original. Additionally, you need to include fill="some_text" within aes() in order to make sure the new fill value gets added to the legend. Note that you don't need to include the actual label text within the geom_bar call, since that is defined within scale_fill_manual. Finally, you need to alter scale_fill_manual to make sure you select the new bars to be black and also indicate how that should be labeled in the legend. I should note that the ordering of the legend is not always intuitive, and in this case, the new bar item is before the others. You can always explicitly define that by using a named vector or a list to specifically assign a particular color or label to a particular legend key.

Upvotes: 2

Related Questions