ggplot2: How to use same colors in different plots for same factor

前端 未结 3 831
春和景丽
春和景丽 2020-11-30 09:45

How can I pin the same color to a value in diffent plots?

Say I have two data.frames df1 and df2:

library(ggplot2)
library(gridExtra)

set.seed(1)
d         


        
3条回答
  •  清歌不尽
    2020-11-30 10:34

    To make this kind of composite plots, ggplot2 has facets:

    df1$id = 'A'
    df2$id = 'B'
    df_all = rbind(df1, df2)
    ggplot(df_all, aes(x=x, y=y, fill=c)) + 
        geom_bar(stat="identity") + 
        facet_wrap(~id)
    

    enter image description here

    When using facets, ggplot2 treats both plots as one whole, keeping the color-value mapping the same.

提交回复
热议问题