r - 在 ggplot2 中绘制 nlme 对象

标签 r ggplot2 plot mixed-models nlme

我使用 nlme 包计算了线性混合模型。我正在评估心理治疗,并使用治疗条件和测量点作为预测因素。我使用 emmans 包进行了事后比较。到目前为止,一切顺利,我期待着完成我的论文。只剩下一个问题了。我真的很不擅长策划。我想绘制每组四个测量点的 emmean。 emmeans 中的 emmip 函数可以做到这一点,但我对结果不太满意。我使用以下代码生成结果:

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

enter image description here

我不喜欢置信区间的呈现方式。我更喜欢带有“正常”置信条的线形条,如下所示,取自 Ireland 等人。 (2017)。我尝试在 Excel 中执行此操作,但没有找到如何集成每条线的单独置信区间。所以我想知道是否有可能使用 ggplot2 来做到这一点。但是,我不知道如何在 ggplot 中整合使用 emmeans 获得的值。正如我所说,我真的对情节一窍不通。有人知道该怎么做吗? This graphic is taken from Ireland et al (2017). I would like to create a plot like this

最佳答案

我认为这是可能的。您可以使用 emmeans 来获取 ggplot2 的值,而不是使用 emmip 创建绘图。使用 ggplot2 和数据,您也许能够更好地控制绘图的格式。由于我没有您的数据,我只能建议几个步骤。
首先,拟合模型 HLM_IPANAT_pos 后,使用 emmeans 获取值。其次,broom::tidy 这个对象。第三,ggplot上面的broom::tidy对象。

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)

reprex package于2019年12月29日创建(v0.3.0)

关于r - 在 ggplot2 中绘制 nlme 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59518624/

相关文章:

r - 使用 'poly' : 'degree' must be less than number of unique points while using 'effects' package 时出错

r - 分类/二进制数据总和的直方图

r - 对齐ggplot2中的文本注释

r - 将文本添加到绘图轴而不删除 r 中现有的轴标签

python - 根据信号所在位置调整 xrange

Rplotly - 绘制分组线

在 R 中使用环绕重新排序向量

r - 在 dplyr 中,是否可以使用 mutate 指定在何处添加新列?

r - 如何在R中检查条件后如何向控制台发送消息,但不返回任何内容,而返回NULL

r - ggplot 带虚线的箭头