How do I manually set geom_bar fill color in ggplot

前端 未结 2 2042
清歌不尽
清歌不尽 2021-01-04 10:27

I\'m trying to create several graphs using ggplot. The graphs are a series of bar graphs that together describe a line as well EXAMPLE (BTW, yes I realize the color palett

2条回答
  •  一整个雨季
    2021-01-04 11:01

    I'm guessing that you've looked at the ggplot color blind example shown here? Without your data, I can only speculate that your geom_bar calls create ambiguity regarding which layer to apply the fill changes to since your initial call to ggplot doesn't have an aes argument. Try moving all of your data into a single dataframe and reference it in the initial call to ggplot, e.g.,

    ggplot(df, aes(x=cond, y=yval)) +
        geom_bar() + 
        scale_fill_manual(values=cbbPalette)
    

    where df is the dataframe containing your data and aes is the mapping between your variables. This makes it clear to ggplot that you want the fill colors of geom_bar to correspond to the data in df. There are ways to make this work with your current code, but they're unconventional for creating standard bar plots.

提交回复
热议问题