简体   繁体   English

在 ggplot2 中绘制 nlme 对象

[英]Plotting an nlme object in ggplot2

I calculated a linear-mixed model using the nlme package.我使用 nlme 包计算了一个线性混合模型。 I was evaluating a psychological treatment and used treatment condition and measurement point as predictors.我正在评估心理治疗并使用治疗条件和测量点作为预测指标。 I did post-hoc comparisons using the emmans package.我使用 emmans 包进行了事后比较。 So far so good, everything worked out well and I am looking forward to finish my thesis.到目前为止一切顺利,一切顺利,我期待着完成我的论文。 There is only one problem left.只剩下一个问题了。 I am really really bad in plotting.我在策划方面真的很糟糕。 I want to plot the emmeans for the four measurement points for each group.我想为每组的四个测量点绘制emmeans。 The emmip function in emmeans does this, but I am not that happy with the result. emmeans 中的 emmip 函数就是这样做的,但我对结果并不满意。 I used the following code to generate the result:我使用以下代码生成结果:

emmip(HLM_IPANAT_pos, Gruppe~TP, CIs=TRUE) + theme_bw() + labs(x = "Zeit", y = "IPANAT-PA")

在此处输入图片说明

I don't like the way the confidence intervals are presented.我不喜欢置信区间的呈现方式。 I would prefer a line bar with "normal" confidence bars, like the one below, which is taken from Ireland et al.我更喜欢带有“正常”置信条的线条,如下所示,取自爱尔兰等人。 (2017). (2017)。 I tried to do it in excel, but did not find out how to integrate seperate confidence intervals for each line.我试图在 excel 中做到这一点,但没有找到如何为每条线整合单独的置信区间。 So I was wondering if there was the possibility to do it using ggplot2.所以我想知道是否有可能使用 ggplot2 来做到这一点。 However, I do not know how to integrate the values I obtained using emmeans in ggplot.但是,我不知道如何整合我在 ggplot 中使用 emmeans 获得的值。 As I said, I really have no idea about plotting.正如我所说,我真的不知道绘图。 Does someone know how to do it?有人知道怎么做吗? 该图取自爱尔兰等人 (2017)。我想创建一个这样的情节

I think it is possible.我认为这是可能的。 Rather than using emmip to create the plot, you could use emmeans to get the values for ggplot2 .而不是使用emmip创建的情节,你可以使用emmeans得到的值ggplot2 With ggplot2 and the data, you might be able to better control the format of the plot.使用ggplot2和数据,您可能能够更好地控制绘图的格式。 Since I do not have your data, I can only suggest a few steps.由于我没有你的数据,我只能建议几个步骤。
First, after fitting the model HLM_IPANAT_pos , get values using emmeans .首先,在拟合模型HLM_IPANAT_pos ,使用emmeans获取值。 Second, broom::tidy this object.其次, broom::tidy这个对象。 Third, ggplot the above broom::tidy object.三、 ggplot上面的broom::tidy对象。

Using mtcars data as an example:mtcars数据为例:

library(emmeans)
# mtcars data
mtcars$cyl = as.factor(mtcars$cyl)
# Model
mymodel <- lm(mpg ~ cyl * am, data = mtcars)

# using ggplot2 
library(tidyverse)
broom::tidy(emmeans(mymodel,  ~ am | cyl)) %>% 
  mutate(cyl_x = as.numeric(as.character(cyl)) + 0.1*am) %>% 
  ggplot(aes(x = cyl_x, y = estimate, color = as.factor(am))) + 
  geom_point() + 
  geom_line() +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.1)

Created on 2019-12-29 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2019 年 12 月 29 日创建

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

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