Home > Enterprise >  ggplot2 bar plot color changing
ggplot2 bar plot color changing

Time:03-10

I wanted to make a bar graph with ggplot. I specify the order and colors for "fill" but one bar's order and color do not change. Here are my codes:

ggplot(data)  
  geom_col(mapping = aes(x = Groups, y = Ratio, fill = Domains), position = "dodge")  
  scale_color_manual(values = c(C1 = "pink", V1 = "blue", V2 = "maroon", C2 = "orange", 
                                V3 = "violet", C3 = "green", V4 = "brown", C4 = "sky blue", 
                                V5 = "red", C5 = "yellow", GP41 = "tan"), aesthetics = c("colour", "fill"))
data$Domains <- factor(data$Domains, levels = c("C1", "V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41"))

Although C5's color in the label is marked by yellow but it does not get changed in the graph and it comes after GP41. Only this bar has an issue.

enter image description here

CodePudding user response:

While the order of the named vector supplied to values within scale_fill_manual() determines the order in the legend, it doesn't dictate the order of the bars. You show code to set the order of the levels for data$Domains but it looks like you're running it after the plotting. Try doing it before and see if that helps.

Here's an example of what I mean about the order of the legend vs the order of the bars in the plot.

library(tidyverse)

d <- tibble(x = rep(letters[1:3], each = 3),
            y = runif(1:9),
            grp = rep(LETTERS[1:3], times = 3))

colours <- setNames(c("cadetblue", "hotpink", "black"), 
                    c("C", "B", "A"))

d %>% 
  ggplot(aes(x, y, fill = grp))  
  geom_col(position = "dodge")  
  scale_fill_manual(values = colours)

Created on 2022-03-10 by the enter image description here

Sample code:

data$Groups <-factor(data$Groups, levels = c("First", "Second", "Third", "Fourth"))

library (ggplot2)

 ggplot(data)  
  geom_col(mapping = aes(x = Groups, y = Ratio, fill = Domains), position = "dodge")  
  scale_color_manual(values = c(C1 = "pink", V1 = "blue", V2 = "maroon", C2 = "orange", 
                                V3 = "violet", C3 = "green", V4 = "brown", C4 = "sky blue", 
                                V5 = "red", C5 = "yellow", GP41 = "tan"), aesthetics = c("colour", "fill")) 
  theme_minimal())

data$Domains <- factor(data$Domains, levels = c("C1", "V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41")) 

#after the ggplot. If you look at the plot, you'll notice that the order is alphabetical: C1,C2,C3,C4,C5,GP41, V1,V2,V3,V4 and V5
    #data$Domains <- factor(data$Domains, levels=unique(data$Domains))

Plot:

enter image description here

Sample data:

    data<-structure(list(Groups = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), .Label = c("First", "Second", "Third", "Fourth"
), class = "factor"), Ratio = c(0.53, 0.32, 0.67, 0.8, 0.57, 
0.69, 0.07, 0.05, 0.47, 0.49, 0.41, 0.12, 0.21, 0.46, 0.49, 0.36, 
0.77, 0.79, 0.17, 0.43, 0.85, 0.9, 0.99, 0.44, 0.23, 0.24, 0.36, 
0.93, 0.07, 0.05, 0.61, 0.07, 0.64, 0.78, 0.21, 0.58, 0.18, 0.09, 
0.15, 0.33, 0.75, 0.73, 0.71, 0.91), Domains = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), .Label = c("C1", 
"V1", "V2", "C2", "V3", "C3", "V4", "C4", "V5", "C5", "GP41"), class = "factor")), spec = structure(list(
    cols = list(Groups = structure(list(), class = c("collector_character", 
    "collector")), Ratio = structure(list(), class = c("collector_double", 
    "collector")), Domains = structure(list(), class = c("collector_character", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x112f2a90>, row.names = c(NA, 
-44L), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
))
  • Related