2016-07-27 112 views
0

我正在尝试调整我的图形,使其适用于科学报告。看下面的例子(从这里:http://glmm.wikidot.com/faq)。在ggplot2中绘制lmer()

如何更改ggplot设置,使行以灰度显示?

library("lme4") 
library("ggplot2") # Plotting 
data("Orthodont",package="MEMSS") 
fm1 <- lmer(
formula = distance ~ age*Sex + (age|Subject) 
, data = Orthodont 
) 

newdat <- expand.grid(
age=c(8,10,12,14) 
, Sex=c("Female","Male") 
, distance = 0 
) 

mm <- model.matrix(terms(fm1),newdat) 
newdat$distance <- predict(fm1,newdat,re.form=NA) 

pvar1 <- diag(mm %*% tcrossprod(vcov(fm1),mm)) 
tvar1 <- pvar1+VarCorr(fm1)$Subject[1] 
cmult <- 2 ## could use 1.96 
newdat <- data.frame(
    newdat 
    , plo = newdat$distance-cmult*sqrt(pvar1) 
    , phi = newdat$distance+cmult*sqrt(pvar1) 
    , tlo = newdat$distance-cmult*sqrt(tvar1) 
    , thi = newdat$distance+cmult*sqrt(tvar1) 
) 

g0 <- ggplot(newdat, aes(x=age, y=distance, colour=Sex))+geom_point() 
g0 + geom_errorbar(aes(ymin = plo, ymax = phi))+ 
labs(title="CI based on fixed-effects uncertainty ONLY") + theme_bw() 

我也不清楚为什么开方()在这行代码被用于:

plo = newdat$distance-cmult*sqrt(pvar1) 

感谢

+1

请参阅'scale_color_grey'作为一个选项。您也可以考虑使用标准误差(方差的平方根)计算映射到“线型”和/或“形状”而不是“颜色”。 – aosmith

回答

1

@aosmith是正确的 - scale_color_grey可能是你在找什么。

g0 <- ggplot(newdat, aes(x=age, y=distance, colour=Sex))+geom_point() 
g0 + geom_errorbar(aes(ymin = plo, ymax = phi)) + 
labs(title="CI based on fixed-effects uncertainty ONLY") + 
theme_bw() + scale_color_grey(start = 0.2, end = 0.5) 

如果你能(你在这里),它通常最好使用冗余编码,即用两个变量(如颜色和线型)编码性。它更容易察觉两者之间的差异。

g0 <- ggplot(newdat, aes(x=age, y=distance, colour=Sex, linetype = Sex)) + geom_point() 
g0 + geom_errorbar(aes(ymin = plo, ymax = phi)) + 
labs(title="CI based on fixed-effects uncertainty ONLY") + 
theme_bw() + scale_color_grey(start = 0.2, end = 0.5) + scale_linetype() 
+0

谢谢,这个工作!我花了一段时间才得到了scale_linetype()的工作,但它做出了很大的改变! –