如何使用ggplot在R中创建意大利面图?

发布于 2025-02-08 23:42:20 字数 904 浏览 1 评论 0原文

我有一个看起来像这样的数据集:

  Study_ID time_point value
1      100      Time1    15
2      100      Time2    50
3      100      Time3   120
4      200      Time1    20
5      200      Time2    35
6      200      Time3   150
7      300      Time1    35
8      300      Time2    67
9      300      Time3    95

每个患者(study_id)在3个时间点(时间1,时间2和时间3)中有3行,每个患者的值为每个。

我想在x轴上创建一个带有time_point的意大利面图,以及y轴上的值,每个患者都有一条线。我所需的输出看起来像这样:

“所需图形”

我该怎么做?

可重复的数据:

data<-data.frame(Study_ID=c("100","100","100","200","200","200","300","300","300"),time_point=c("Time1","Time2","Time3","Time1","Time2","Time3","Time1","Time2","Time3"),value=c("15","50","120","20","35","150","35","67","95"))

I have a dataset that looks like this:

  Study_ID time_point value
1      100      Time1    15
2      100      Time2    50
3      100      Time3   120
4      200      Time1    20
5      200      Time2    35
6      200      Time3   150
7      300      Time1    35
8      300      Time2    67
9      300      Time3    95

Where each patient (Study_ID) has 3 rows for the 3 time-points (Time 1, Time 2, and Time 3), with a value for each.

I would like to create a spaghetti plot with time_point on the x-axis, and the value on the y-axis, with a line for each patient. My desired output would look something like this:

Desired Graph

How can I go about doing this?

Reproducible data:

data<-data.frame(Study_ID=c("100","100","100","200","200","200","300","300","300"),time_point=c("Time1","Time2","Time3","Time1","Time2","Time3","Time1","Time2","Time3"),value=c("15","50","120","20","35","150","35","67","95"))

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

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

发布评论

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

评论(1

迷乱花海 2025-02-15 23:42:20

通过使用groupcolor aes()中的参数,您可以添加图层geom_point()和<代码> geom_line()将颜色和组保持在一起。

library(tidyverse)

data<-data.frame(Study_ID=c("100","100","100","200","200","200","300","300","300"),time_point=c("Time1","Time2","Time3","Time1","Time2","Time3","Time1","Time2","Time3"),value=c("15","50","120","20","35","150","35","67","95"))

ggplot(data, aes(time_point, value, group = Study_ID, color = Study_ID)) + 
  geom_point() + 
  geom_line()

“”

在2022-06-20创建的 reprex软件包(v2.0.1)

By using the group and color arguments within aes() you can then add the layers geom_point() and geom_line() to keep color and group together.

library(tidyverse)

data<-data.frame(Study_ID=c("100","100","100","200","200","200","300","300","300"),time_point=c("Time1","Time2","Time3","Time1","Time2","Time3","Time1","Time2","Time3"),value=c("15","50","120","20","35","150","35","67","95"))

ggplot(data, aes(time_point, value, group = Study_ID, color = Study_ID)) + 
  geom_point() + 
  geom_line()

Created on 2022-06-20 by the reprex package (v2.0.1)

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