Home > Back-end >  ggplot2 barplot bar colour can't be changed properly
ggplot2 barplot bar colour can't be changed properly

Time:09-22

I created the following flipped bar plot in ggplot2 (a coordinate flipped grouped bar plot in bright green) - which worked out just fine (Code 1).

However I tried to recreate the same plot structure with different data, basically copying and adapting the code from the first plot. Everything is fine, exept for the colour being grey instead of the desired green in the scale fill manual. I honestly can't figure out why it works in the first plot but not in the second. (Code 2)

CODE 1 (the working original plot)


#LIBRARIES
library (ggplot2)
library (dplyr)
library (data.table)
library (rdrop2)
library(tableHTML)
library (forcats)
  
  df<- data.frame(
    Komp = rep(c("Geschlecht", "Alter", "Niveau"), 6), 
    
    groupW = c(
      rep("1", 3),
      rep("2", 3),
      rep("3", 3),
      NA, NA, "4",
      NA, NA, "5",
      NA, NA, "6"
      
      
      
    ),
    
    
    valuesW = c (45, 14.9, 4.6, 54, 64.9, 5.3, 1, 19.8, 22.2, NA, NA, 15.4, NA, NA, 38.2, NA, NA, 14.2)
    
  ) 



df%>%
  ggplot( aes(x = fct_rev(Komp), y = valuesW, fill = fct_rev(groupW)))   
    geom_bar(position = "dodge", stat = "identity",color = "#404040", show.legend = FALSE, 
             #width = 1
    ) 
    #scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", paste("Durchschnitt", (Daten[toupper(input$Code), "SP01_01"]-1),"-",(Daten[toupper(input$Code), "SP01_01"] 1), "Jahre" ),paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"]), Geschlecht)) 
    xlab("") 
    ylab("Prozent") 
    scale_fill_manual(values=rep( "#8DAE10", 6)) 
    coord_flip() 
    #geom_hline(yintercept = 10, lty = 8, col = "Red")
    geom_text(
      aes (label = ifelse(is.na(valuesW),"" ,paste(valuesW,"%"))),
      color = "black", hjust =-0.35 , size = 4, position = position_dodge(width= 0.9)) 
    
    #scale_x_discrete(limits= rev(levels(groupW))) 
    geom_text(aes(y=0, label = c(
      "Weiblich",  "10-13 Jahre","Keine Ebene",
      "Mxnnlich", "14-17 Jahre","Bezirksebene", 
      "Div.",  "18-23 Jahre","Landesebene",
      "", "",  "landesxbergreifende Ebene" , 
      "", "",  "nationale Ebene", 
      "","", "internationale Ebene")
    ),  color = "white", hjust = 0, size = 5, position = position_dodge(width= 0.9)) 
    scale_y_continuous(
      expand= c(0.04,0), label = scales::comma, position = "right"
    )

CODE 2 (a grey plot that is supposed to be just as green as the first one)

#LIBRARIES
library (ggplot2)
library (dplyr)
library (data.table)
library (rdrop2)
library(tableHTML)
library (forcats)


df<- data.frame(

  
  
  
    
  Komp = rep(c("Smartphone", "PC/Laptop", "Tablet", "TV", "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere"), 2), 
  
  groupW = c(
    rep("ja", 8),
    rep("nein", 8)
    
    
    
  ),
  
  
  valuesW = c (100-0, 100-23.9, 100-41.3, 100-30.9, 100-51.5, 100-73.2, 100-89.8, 100-98.9 ,0, 23.9, 41.3, 30.9, 51.5, 73.2, 89.8, 98.9)
  
) 

df%>%
  ggplot( aes(x = fct_rev(Komp), y = valuesW, fill = fct_rev(groupW)))   
    geom_bar(position = "dodge", stat = "identity",color = "#8DAE10", show.legend = FALSE, 
             #width = 1
    ) 
    #scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", paste("Durchschnitt", (Daten[toupper(input$Code), "SP01_01"]-1),"-",(Daten[toupper(input$Code), "SP01_01"] 1), "Jahre" ),paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"]), Geschlecht)) 
    xlab("") 
    ylab("Prozent") 
    scale_fill_manual(values=rep( "#8DAE10", 16), limits = c("Smartphone", "PC/Laptop", "Tablet", "TV", "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere")) 
    coord_flip() 
    #geom_hline(yintercept = 10, lty = 8, col = "Red")
    geom_text(
      aes (label = round(valuesW,1)),
      color = "black", hjust =-0.35 , size = 4, position = position_dodge(width= 0.9)) 
    
    #scale_x_discrete(limits= rev(levels(groupW))) 
    geom_text(aes(y=0, label = groupW
    ),  color = "white", hjust = 0, size = 5, position = position_dodge(width= 0.9)) 
    scale_y_continuous(
      expand= c(0.04,0), label = scales::comma, position = "right"
    )

Any ideas where the mistake is?

CodePudding user response:

If you remove the limits = c("Smartphone", "PC/Laptop", "Tablet", "TV", "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere")

It should make all the bars the wanted green colour.

library (ggplot2)
library (dplyr)
library (data.table)
library (rdrop2)
library(tableHTML)
library (forcats)


df<- data.frame(

  
  
  
    
  Komp = rep(c("Smartphone", "PC/Laptop", "Tablet", "TV", "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere"), 2), 
  
  groupW = c(
    rep("ja", 8),
    rep("nein", 8)
    
    
    
  ),
  
  
  valuesW = c (100-0, 100-23.9, 100-41.3, 100-30.9, 100-51.5, 100-73.2, 100-89.8, 100-98.9 ,0, 23.9, 41.3, 30.9, 51.5, 73.2, 89.8, 98.9)
  
) 

df%>%
  ggplot( aes(x = fct_rev(Komp), y = valuesW, fill = fct_rev(groupW)))   
    geom_bar(position = "dodge", stat = "identity",color = "#8DAE10", show.legend = FALSE, 
             #width = 1
    ) 
    #scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", paste("Durchschnitt", (Daten[toupper(input$Code), "SP01_01"]-1),"-",(Daten[toupper(input$Code), "SP01_01"] 1), "Jahre" ),paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"]), Geschlecht)) 
    xlab("") 
    ylab("Prozent") 
    scale_fill_manual(values=rep( "#8DAE10", 16), ) 
    coord_flip() 
    #geom_hline(yintercept = 10, lty = 8, col = "Red")
    geom_text(
      aes (label = round(valuesW,1)),
      color = "black", hjust =-0.35 , size = 4, position = position_dodge(width= 0.9)) 
    
    #scale_x_discrete(limits= rev(levels(groupW))) 
    geom_text(aes(y=0, label = groupW
    ),  color = "white", hjust = 0, size = 5, position = position_dodge(width= 0.9)) 
    scale_y_continuous(
      expand= c(0.04,0), label = scales::comma, position = "right"
    )

CodePudding user response:

You can try these adjustments to a) achieve the green bar colour and b) simplify the code

  1. use geom_col rather than geom_bar with stat = "identity"
  2. swap x and y values and associated scales etc to avoid using coord_flip
  3. remove the limits argument in the call to scale_fill_manual
  4. convert Komp to a factor to control order of values in ggplot.
library(ggplot2)
library(forcats)
library(dplyr)


komp_cat <- c("Smartphone", "PC/Laptop", "Tablet", "TV", "Spielkonsole", "Smart-Watch", "E-Book-Reader", "Andere")

df1<- data.frame(
  Komp = rep(komp_cat, 2), 
  
  groupW = c(
    rep("ja", 8),
    rep("nein", 8)),
  
  valuesW = c (100-0, 100-23.9, 100-41.3, 100-30.9, 100-51.5, 100-73.2, 100-89.8, 100-98.9 ,0, 23.9, 41.3, 30.9, 51.5, 73.2, 89.8, 98.9)
  
) 



df1 %>% 
  mutate(Komp = factor(Komp, levels = komp_cat)) %>% 
  ggplot( aes(x = valuesW, y = fct_rev(Komp), fill = rev(groupW)))   
  geom_col(position = "dodge", color = "#8DAE10", show.legend = FALSE) 
  xlab("Prozent") 
  ylab("") 
  scale_fill_manual(values = rep("#8DAE10", 2)) 
  geom_text(aes (label = round(valuesW,1)),
            color = "black", 
            hjust = -0.35, 
            size = 4, 
            position = position_dodge(width = 0.9)) 
  geom_text(aes(x=0, label = groupW),
            color = "white", 
            hjust = 0, 
            size = 5, 
            position = position_dodge(width = 0.9)) 
  scale_x_continuous(expand = c(0.04, 0), label = scales::comma, position = "bottom")

Created on 2021-09-22 by the reprex package (v2.0.0)

  • Related