Search code examples
rggplot2stacked-bar-chart

How to change colours in multiple stacked bar charts in R?


I am trying to construct the stacked bar chart plots. Below you may find the sample data set and a code.

This is the data set:

group;answer;count;proportion
first;1;67;19
first;2;119;33,7
first;3;6;1,7
first;4;116;32,9
first;5;45;12,7
second;1;102;17,1
second;2;197;33,1
second;3;10;1,7
second;4;232;38,9
second;5;55;9,2
third;1;49;12,9
third;2;143;37,7
third;3;1;0,3
third;4;142;37,5
third;5;44;11,6
fourth;1;45;14,9
fourth;2;93;30,7
fourth;3;3;1
fourth;4;118;38,9
fourth;5;44;14,5

This is the code:

p <- ggplot(sample1, aes(y = proportion, x = group, fill = proportion)) + 
geom_bar(position = "stack", stat = "identity") + 
facet_grid(~ "") + 
theme_minimal() +
 
p1 <-  ggpar(p, xlab = F, ylab = F, legend = "", ticks = F)

p1 + geom_text(aes(label = proportion), 
               position = position_stack(vjust = 0.5), 
               check_overlap = T, 
               colour = "white")

This generates the plot well, but I need to manually change the colours of the five categories (in the data set denoted to as "answer").

enter image description here

However, if I add:

scale_fill_manual(values = c("#E7344E", "#0097BF", "#E7344E", "#0097BF", "#E7344E") )

I get the error: Continuous value supplied to discrete scale.


Solution

  • You are mapping a numeric on the fill aes. Hence you get a continuous fill color scale. If you want to fill your bars by answer map this column on the fill aes. But as this column is a numeric too, convert it to factor to make scale_fill_manual work:

    library(ggplot2)
    
    p <- ggplot(sample1, aes(y = proportion, x = group, fill = factor(answer))) +
      geom_bar(position = "stack", stat = "identity") +
      scale_fill_manual(values = c("#E7344E", "#0097BF", "#E7344E", "#0097BF", "#E7344E")) +
      facet_grid(~"") +
      theme_minimal()
    
    p1 <- ggpubr::ggpar(p, xlab = F, ylab = F, legend = "", ticks = F)
    
    p1 + geom_text(aes(label = proportion),
      position = position_stack(vjust = 0.5),
      check_overlap = T,
      colour = "white"
    )
    

    DATA

    sample1 <- structure(list(group = c(
      "first", "first", "first", "first",
      "first", "second", "second", "second", "second", "second", "third",
      "third", "third", "third", "third", "fourth", "fourth", "fourth",
      "fourth", "fourth"
    ), answer = c(
      1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L,
      4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L
    ), count = c(
      67L,
      119L, 6L, 116L, 45L, 102L, 197L, 10L, 232L, 55L, 49L, 143L, 1L,
      142L, 44L, 45L, 93L, 3L, 118L, 44L
    ), proportion = c(
      19, 33.7,
      1.7, 32.9, 12.7, 17.1, 33.1, 1.7, 38.9, 9.2, 12.9, 37.7, 0.3,
      37.5, 11.6, 14.9, 30.7, 1, 38.9, 14.5
    )), class = "data.frame", row.names = c(
      NA,
      -20L
    ))