ggplot2 -使用两种不同的颜色标度作为重叠的区域。

时间:2022-04-20 06:13:00

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 when I add the second color scale, I am given the error

我试图覆盖两个不同的情节。其中一个是“风水”,另一个是“风水”。我希望每个人都有自己的颜色标度。但是当我添加第二个颜色标度时,我得到了误差。

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

I am assuming I am doing something wrong. Any advice would be appreciate

我假设我做错了什么。任何建议都是值得赞赏的。

This is a rough 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 schema, but different values.

dat和dat2具有相同的模式,但有不同的值。

I found several examples addressing overlaying graphs but none that appeared to address this specific concern.

我发现了几个关于覆盖图的例子,但是没有一个能解决这个特别的问题。

1 个解决方案

#1


12  

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.

我假设您不需要使用参数填充=在ge_jitter()中,因为形状=4的颜色也可以设置为颜色=参数。然后,您可以使用scale_color - manual()来设置您的值。而不是cpallete使用的颜色名称。

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"))

ggplot2 -使用两种不同的颜色标度作为重叠的区域。

#1


12  

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.

我假设您不需要使用参数填充=在ge_jitter()中,因为形状=4的颜色也可以设置为颜色=参数。然后,您可以使用scale_color - manual()来设置您的值。而不是cpallete使用的颜色名称。

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"))

ggplot2 -使用两种不同的颜色标度作为重叠的区域。