简体   繁体   English

在ggplot2中绘制lmer()

[英]Plotting lmer() in ggplot2

I'm trying to adjust my graph to make it suitable for a scientific report. 我正在尝试调整图表以使其适合科学报告。 See example below (from here: http://glmm.wikidot.com/faq ). 请参见下面的示例(从此处: http : //glmm.wikidot.com/faq )。

How do I change the ggplot settings so the lines are displayed in greyscale? 如何更改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()

I'm also unsure why sqrt() is used in this line of code: 我也不确定为什么在此代码行中使用sqrt():

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

Thanks 谢谢

@aosmith is right - scale_color_grey is probably what you're looking for. @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)

If you're able to (which you are here), it's generally best to use redundant encoding, ie, encoding sex with two variables (such as color and linetype). 如果能够(您在此处),通常最好使用冗余编码,即用两个变量(例如颜色和线型)编码性别。 It makes it easier to perceive the difference between the two. 这使得更容易理解两者之间的差异。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM