Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 更改ggplot2中的图例颜色_R_Ggplot2 - Fatal编程技术网

R 更改ggplot2中的图例颜色

R 更改ggplot2中的图例颜色,r,ggplot2,R,Ggplot2,我在更改ggplot2中传奇的外观时遇到了一些问题。这可能是一个一般性的问题,但以防万一,这里是我的示例数据和我正在绘制的lmer模型,以便可以复制 tempEf <- data.frame( N = rep(c("1", "2","1", "2","1", "2","1"), each=5, times=11), Myc = rep(c("1", "2", "3", "4", "5"), each=1, times=77), TRTYEAR = runif(385, 1, 1

我在更改ggplot2中传奇的外观时遇到了一些问题。这可能是一个一般性的问题,但以防万一,这里是我的示例数据和我正在绘制的lmer模型,以便可以复制

tempEf <- data.frame(
  N = rep(c("1", "2","1", "2","1", "2","1"), each=5, times=11),
  Myc = rep(c("1", "2", "3", "4", "5"), each=1, times=77),
  TRTYEAR = runif(385, 1, 15),
  site = rep(c(1:77), each=5, times=1),#77 sites
  Asp = runif(385, 1, 5))

# Make up some response data
tempEf$r <- 2*tempEf$TRTYEAR +                   
  -8*as.numeric(tempEf$Myc=="1") +
  4*as.numeric(tempEf$N=="1") +
  0.1*tempEf$TRTYEAR * as.numeric(tempEf$N=="1") +
  0.2*tempEf$TRTYEAR*as.numeric(tempEf$Myc=="1") +
  -11*as.numeric(tempEf$Myc=="1")*as.numeric(tempEf$N=="1")+ 
  0.5*tempEf$TRTYEAR*as.numeric(tempEf$Myc=="1")*as.numeric(tempEf$N=="1")+ 
  as.numeric(tempEf$site) +  #Random intercepts; intercepts will increase by 1
  tempEf$TRTYEAR/10*rnorm(385, mean=0, sd=2)    #Add some noise
#fit model
library(lme4)
model <- lmer(r ~ Myc * N + TRTYEAR + Asp + (1|site), data=tempEf)
tempEf$fit <- predict(model)   #Add model fits to dataframe
在图例中,哪种颜色适用于不同的灰色,但只适用于所有5种颜色,如果我使用“白色”或“透明”则不适用

有简单的解决办法吗


非常感谢

它不是特别整洁,我相信有更好的方法,但是如果你使用“大小”你可以给积木上色:

myplot + 
 guides(color=guide_legend(override.aes=list(fill=pred$Myc, size=5)))

我希望这有帮助

检查。你必须使用
guides(color=guide\u legend(override.aes=list(fill=NA))
。至于线条颜色,我认为可以,谢谢!有没有办法用正确的颜色把积木遮住?或者它们必须保持白色?对不起,我找不到办法来获得颜色。
myplot<-myplot+
  scale_color_npg(labels = c("A", "B", "C", "D", "E")) + scale_fill_npg() + 
  labs(x="\nTreatment Year",y="Ratings\n")
  theme(legend.key = element_rect(fill =  'pink'))
myplot + 
 guides(color=guide_legend(override.aes=list(fill=pred$Myc, size=5)))