R:回归线在 ggplot 中中断,而预期是连续线

发布于 2025-01-16 02:38:52 字数 1684 浏览 4 评论 0原文

我使用 nlme 包创建了一个多级回归模型,现在我想说明为某些患者获得的回归线(不幸的是我不能将 geom_smooth 与 nlme 一起使用)。

因此,使用该模型,我获得了两个患者(ID1 和 ID2)在不同时间 (date_day) 的以下预测值 (predicted_value)。

df <- data.frame (ID = c (rep (1, 10), rep(2, 10)),
                  date_day = c (7:16, 7:16),
                  predicted_value = c (33, 33, 33, 33, 33, NA, 34, NA, NA, NA, 
                               55, NA, NA, 53.3, NA, NA, 51.6, NA, 50.5, NA))


 ID date_day predicted_value
1   1        7            33.0
2   1        8            33.0
3   1        9            33.0
4   1       10            33.0
5   1       11            33.0
6   1       12              NA
7   1       13            34.0
8   1       14              NA
9   1       15              NA
10  1       16              NA
11  2        7            55.0
12  2        8              NA
13  2        9              NA
14  2       10            53.3
15  2       11              NA
16  2       12              NA
17  2       13            51.6
18  2       14              NA
19  2       15            50.5
20  2       16              NA

现在我想为每个患者绘制回归线。所以我尝试了以下

ggplot(df%>% filter(ID %in% c("1", "2")))+ 
  aes(x =  date_day, y = predicted_value) +
  geom_point(shape = "circle", size = 1.5, colour = "#112446", na.rm = T) +
  geom_line(aes(y = predicted_value), na.rm = T, size = 1) +
  theme_minimal() +
  facet_wrap(vars(ID)) +
  scale_x_continuous(name="days", limits=c(7, 16)) +
  scale_y_continuous(name="predicted values", limits=c(0, 60))

但我以以下情节结束: 患者 1:线路中断,而患者 2 根本没有线路。我该如何解决这个问题?

非常感谢

I created a multilevel regression model with nlme package and now I would like to illustrate the regression line obtained for some patients (unfortunately I cannot use geom_smooth with nlme).

So using the model I obtained the following predicted values (predicted_value) at different times (date_day) and here for two patients (ID1 and ID2).

df <- data.frame (ID = c (rep (1, 10), rep(2, 10)),
                  date_day = c (7:16, 7:16),
                  predicted_value = c (33, 33, 33, 33, 33, NA, 34, NA, NA, NA, 
                               55, NA, NA, 53.3, NA, NA, 51.6, NA, 50.5, NA))


 ID date_day predicted_value
1   1        7            33.0
2   1        8            33.0
3   1        9            33.0
4   1       10            33.0
5   1       11            33.0
6   1       12              NA
7   1       13            34.0
8   1       14              NA
9   1       15              NA
10  1       16              NA
11  2        7            55.0
12  2        8              NA
13  2        9              NA
14  2       10            53.3
15  2       11              NA
16  2       12              NA
17  2       13            51.6
18  2       14              NA
19  2       15            50.5
20  2       16              NA

Now I would like to draw the regression line for each of these patients. So I tried the following

ggplot(df%>% filter(ID %in% c("1", "2")))+ 
  aes(x =  date_day, y = predicted_value) +
  geom_point(shape = "circle", size = 1.5, colour = "#112446", na.rm = T) +
  geom_line(aes(y = predicted_value), na.rm = T, size = 1) +
  theme_minimal() +
  facet_wrap(vars(ID)) +
  scale_x_continuous(name="days", limits=c(7, 16)) +
  scale_y_continuous(name="predicted values", limits=c(0, 60))

But I end with the following plots: patient 1 : the line is interrupted, and patient 2 no line at all. How can I fix that ?

enter image description here

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓦然回首 2025-01-23 02:38:52

谢谢@BenBolker,确实将第一行更改

ggplot(df%>% filter(ID %in% c("1", "2"))) 

ggplot(na.omit(df)%>% filter(ID %in% c("1", "2")))

允许解决工作

Thank you @BenBolker , indeed changing the first line

ggplot(df%>% filter(ID %in% c("1", "2"))) 

to

ggplot(na.omit(df)%>% filter(ID %in% c("1", "2")))

allowed to solve the job

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文