在ggplot2中绘制lmer()

时间:2016-07-27 18:51:51

标签: r ggplot2

我正在尝试调整图表以使其适合科学报告。请参阅下面的示例(从此处:http://glmm.wikidot.com/faq)。

如何更改ggplot设置以使线条以灰度显示?

removeItem(index) {
        var self = this;

        // Store property paths for easy access
        let todaysPath = this.get('healthData').todaysFood;
        let caloriesPath = 'healthData.calories';

        this.set(caloriesPath, this.get(caloriesPath) - Math.round(todaysPath[index].fields.nf_calories));

        todaysPath.removeObject(todaysPath[index]);

    }

我也不确定为什么在这行代码中使用了sqrt():

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

由于

1 个答案:

答案 0 :(得分: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()