ggplot2: Fix colors to factor levels

前端 未结 4 2000
借酒劲吻你
借酒劲吻你 2020-12-10 12:50

I\'m working on a larger project for which I am creating several plots in ggplot2. The plots are concerned with plotting several different outcomes across several different

相关标签:
4条回答
  • 2020-12-10 13:05

    You could define your own custom scale, if you like. If you look at the source for scale_fill_manual,

    scale_fill_manual
    #> function (..., values) 
    #> {
    #>     manual_scale("fill", values, ...)
    #> }
    #> <environment: namespace:ggplot2>
    

    it's actually quite simple:

    library(ggplot2)
    
    scale_fill_chris <- function(...){
        ggplot2:::manual_scale(
            'fill', 
            values = setNames(c('green', 'blue', 'red', 'orange'), LETTERS[1:4]), 
            ...
        )
    }
    
    df1 <- data.frame(Value = c(40, 20, 10, 60), 
                      Type = c("A", "B", "C", "D"))
    
    ggplot(df1, aes(x = Type, y = Value, fill = Type)) + 
        geom_col() + 
        scale_fill_chris()
    

    df2 <- data.frame(Value = c(40, 20, 60), 
                      Type = c("A", "B", "D"))
    
    ggplot(df2, aes(x = Type, y = Value, fill = Type)) + 
        geom_col() + 
        scale_fill_chris()
    

    df3 <- data.frame(Value = c(40, 20, 10, 60), 
                      Type = c("A", "B", "C", "D"))
    df3$Type <- factor(df3$Type, levels = c("D", "C", "B", "A"), ordered = TRUE)
    
    ggplot(df3, aes(x = Type, y = Value, fill = Type)) + 
        geom_col() + 
        scale_fill_chris()
    

    0 讨论(0)
  • 2020-12-10 13:07

    Another options is to make drop = F the default by defining the default colour scales as follows:

    scale_colour_discrete <- function(...)
      scale_colour_manual(..., drop = F)
    scale_fill_discrete <- function(...)
      scale_fill_manual(..., drop = F)
    

    That way colours are always consistent for different factors.

    0 讨论(0)
  • 2020-12-10 13:22

    make sure you convert that column into Factor first and then create a variable to store the color value for each factor...

    df$color <- as.factor(df$color, levels = c(1, 0))
    cbPallete <- c("1"= "green", "0"="red")
    
    ggplot(data = df) + geom_bar(x = df$x, 
                                 y = df$y,
                                 fill = df$color) +
    scale_fill_manual(values = cbPallete)
    
    0 讨论(0)
  • 2020-12-10 13:26

    You could make a custom plot function (including scale_fill_manual and reasonable default colours) in order to avoid repeating code:

    library(ggplot2)
    custom_plot <- function(.data,
      colours = c("A" = "green", "B" = "blue", "C" = "red", "D" = "grey"))  {
      ggplot(.data, aes(x=Type, y=Value, fill= Type)) + geom_bar(stat="identity") +
       scale_fill_manual(values = colours)
    }
    
    df1 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D"))
    df2 <- data.frame(Value=c(40, 20, 60), Type=c("A", "B", "D"))
    df3 <- data.frame(Value=c(40, 20, 10, 60), Type=c("A", "B", "C", "D"))
    df3$Type <- factor(df3$Type, levels=c("D", "C", "B", "A"), ordered=TRUE)
    
    custom_plot(df1)
    custom_plot(df2)
    custom_plot(df3)
    
    0 讨论(0)
提交回复
热议问题