ggplot2: Fix colors to factor levels

前端 未结 4 2005
借酒劲吻你
借酒劲吻你 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: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)
    

提交回复
热议问题