ggplot2 - using two different color scales for overlayed plots

后端 未结 1 1916
半阙折子戏
半阙折子戏 2020-12-03 18:13

I am attempting to overlay two different plots. One is geom_boxplot, the other geom_jitter. I would like each to have its own color scale. But w

相关标签:
1条回答
  • 2020-12-03 18:56

    First, made two sample data frames with the same names as in example.

    dat<-data.frame(ve=rep(c("FF","GG"),times=50),
                    metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25),
                    atd=rep(c("HH","GG"),times=50))
    dat2<-data.frame(ve=rep(c("FF","GG"),times=50),
                    metValue=rnorm(100),metric=rep(c("A","B","D","C"),each=25),
                    atd=rep(c("HH","GG"),times=50))
    

    I assume that you do not need to use argument fill= in the geom_jitter() because color for shape=4 can be set also with colour= argument. Then you can use scale_colour_manual() to set your values. Instead of cpallete just used names of colors.

    P <-  ggplot(dat) + 
      geom_boxplot(aes(x=ve, y=metValue, fill=metric), alpha=.35, w=0.6, notch=FALSE, na.rm = TRUE) +  
      geom_hline(yintercept=0, colour="#DD4466", linetype = "longdash") +
      scale_fill_manual(values=c("red","blue","green","yellow"))+
      theme(legend.position="none")
    
    P + geom_jitter(data=dat2, aes(x=ve, y=metValue, colour=atd), 
                    size=2, shape=4, alpha = 0.4, 
                    position = position_jitter(width = .03, height=0.03), na.rm = TRUE) + 
                    scale_colour_manual(values=c("red","blue"))
    

    enter image description here

    0 讨论(0)
提交回复
热议问题