Ggplot2 - using two different color scales for overlaid graphs

I am trying to overlay two different graphs. One of them is geom_boxplot , the other is geom_jitter . I would like everyone to have their own color scheme. But when I add a second color scale, I get an error

  "Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale." 

I guess I'm doing something wrong. Any advice would be appreciated

This is an example of my working code:

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

dat and dat2 have the same scheme, but different values.

I found several examples regarding charting, but none of them seemed to address this particular issue.

+8
r color-scheme ggplot2
source share
1 answer

First, create two data samples with the same names as in the 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 the fill= argument in geom_jitter() , because the color for shape=4 can also be set with the colour= argument. Then you can use scale_colour_manual() to set your values. Instead of cpallete , only color names are used.

 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

+12
source share

All Articles