2022-06-18

How to show selected legend but show all the colors in stacked geom_bar

I'm creating a stacked bar plot of relative abundance data, but I only want to display selected few interesting taxa in the legend. I have tried using scale_fill_manual(values = sample(col_vector), breaks = legend_list). The result only shows selected legend in my legend_list as I wish, but all other factors shows no color. How do I show all the colors as stacked bar plot, but only show legend for factors in legend_list?

My code:

ggplot(df, aes_string(x = x, y = y, fill = fill)) +
        geom_bar(stat="identity", position="stack") +
        scale_fill_manual(values = sample(col_vector), 
                          breaks = legend_list) +
        theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

enter image description here



No comments:

Post a Comment