线连接嵌套GGPLOT中同一ID的点

发布于 2025-02-11 10:39:01 字数 4871 浏览 1 评论 0原文

我有一组数据来研究男女对治疗的反应。 变量:

时间ID变量
MT0控制ID0187.4468574183993
MT0治疗ID0261.6036264109425
MT0CONTROMT0和30
测量时间要么管理治疗或对照车辆0
,并在每个人的感兴趣的控制ID0572.958341162652
MT0处理ID0656.7137802368961
MT0控制ID0765.8371140481904
MT0处理ID08903
MT30控制ID0184.504537214525
MT30处理ID0256.6733720363118
MT30控制ID03
处理T30MID0491.0738771315664
控制ID0576.23775213724
MT30处理ID0674.898213578538
MT30控制ID0780.9279862442054
MT30处理ID0851.5291749034077
MT0控制ID1191.9786257203668
FT0处理ID1252.1936888922937
FT0处理ID08CONTROM
FT30FID14
控制ID1383.2809177343734
T0
控制ID1589.8885271861218
FT0处理ID1684.3541337642819
FT0控制ID1787.5198906287551
FT0处理ID1885
FT30控制ID1153.5416435450315
FT30处理ID1461.9461603346281
FT30控制ID1263.9171704184264
FT30处理ID13
控制ID1577.3780003190041
FT30处理ID1680.7108032982796
FT30控制ID1782.9249111004174
FT30处理ID18https
hrefa72.65731166029613
=//i.sstatic

要生成合适的嵌套

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) +  facet_grid(. ~ Group) +   geom_boxplot(alpha = 0.80,outlier.colour = NA) +   geom_point(aes(fill = Time), size   2, shape = 21, position = position_jitterdodge(0))

< net/q1sfx.png“ rel =” nofollow noreferrer“> ”

但是我喜欢要包括连接属于同一个人的点的行,类似的点(我添加了黑线作为使用图像版本的软件添加的示例)

“我想要的情节”

我尝试了:

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) +   facet_grid(. ~ Group) +   geom_boxplot(alpha = 0.80,outlier.colour = NA) +   geom_point(aes(fill = Time), size = 2, shape = 21, position = position_jitterdodge(0))+   geom_line(aes(group=interaction(ID,Sex)), size=0.3, alpha=0.8) 

但是线条未正确绘制

<< a href =“ https://i.sstatic.net/o0r9i.png” rel =“ nofollow noreferrer”>

我还研究了这个解决方案: 如何添加R?<<<<<<<<<<<<< /a>

,但我不想在我的情节中添加更多面板。 非常感谢您的任何帮助。

I have a set of data to study the response to a treatment in both sexes. Either treatment or control vehicle is administered and the variable of interest measured at time 0 and 30 days for every individual:

SexTimeGroupIDVariable
Mt0controlID0187.4468574183993
Mt0treatmentID0261.6036264109425
Mt0controlID0387.1487529366277
Mt0treatmentID0450.6166256731376
Mt0controlID0572.9583341162652
Mt0treatmentID0656.7137802368961
Mt0controlID0765.8371140481904
Mt0treatmentID0886.0453418688849
Mt0controlID0983.5156044340692
Mt0treatmentID1068.5201310901903
Mt30controlID0184.504537214525
Mt30treatmentID0256.6733720363118
Mt30controlID0391.0738771315664
Mt30treatmentID0494.9689620872959
Mt30controlID0576.23775213724
Mt30treatmentID0674.8982135788538
Mt30controlID0780.9279862442054
Mt30treatmentID0851.5291749034077
Mt30controlID0985.7785020256415
Mt30treatmentID1051.461190963164
Ft0controlID1152.1936888922937
Ft0treatmentID1291.9786257203668
Ft0controlID1383.2809177343734
Ft0treatmentID1471.7776856268756
Ft0controlID1589.8885271861218
Ft0treatmentID1684.3541337642819
Ft0controlID1787.5198906287551
Ft0treatmentID1894.8092532227747
Ft0controlID1956.6507609561086
Ft0treatmentID2072.2115040174685
Ft30controlID1153.5416435450315
Ft30treatmentID1261.9461603346281
Ft30controlID1363.9171704184264
Ft30treatmentID1473.8425903371535
Ft30controlID1577.3780003190041
Ft30treatmentID1680.7108032982796
Ft30controlID1782.9249111004174
Ft30treatmentID1872.6573116029613
Ft30controlID1988.3334786165506
Ft30treatmentID2050.722946121823

I managed to generate a suitable nested plot:

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) +  facet_grid(. ~ Group) +   geom_boxplot(alpha = 0.80,outlier.colour = NA) +   geom_point(aes(fill = Time), size   2, shape = 21, position = position_jitterdodge(0))

the first plot I obtained

But I would like to include lines joining the dots belonging to a same individual, something like this (I added the black lines as an example using a software for image edition)

the plot I want

I tried:

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) +   facet_grid(. ~ Group) +   geom_boxplot(alpha = 0.80,outlier.colour = NA) +   geom_point(aes(fill = Time), size = 2, shape = 21, position = position_jitterdodge(0))+   geom_line(aes(group=interaction(ID,Sex)), size=0.3, alpha=0.8) 

But the lines were not correctly drawn

the second plot I obtained

I also looked at this solution:
How to add lines connecting dots in boxplot in R?

But I would not like to add more panels to my plot.
Thanks a lot for any help.

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

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

发布评论

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

评论(1

任谁 2025-02-18 10:39:01

通过计算x的位置来手动躲避geom_line

library(ggplot2)

width <- .75

mydata$x_line <- as.numeric(factor(mydata$Sex)) + 
  ifelse(mydata$Time == "t0", -1, 1) * width / 4

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) +
  facet_grid(. ~ Group, scales = "free_x") +
  geom_boxplot(alpha = 0.80, outlier.colour = NA, position = position_dodge(width)) +
  geom_point(aes(group = Time), size = 2, shape = 21, position = position_dodge(width)) +
  geom_line(aes(x = x_line, group = ID), size = 0.3, alpha = 0.8)

实现所需结果的一种选项是, sstatic.net/luqrv.png“ alt =”“>

data

mydata <- data.frame(
  Sex = c(
    "M", "M", "M", "M", "M", "M",
    "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
    "M", "M", "F", "F", "F", "F", "F", "F", "F", "F", "F",
    "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F"
  ),
  Time = c(
    "t0", "t0", "t0", "t0", "t0",
    "t0", "t0", "t0", "t0", "t0", "t30", "t30", "t30", "t30",
    "t30", "t30", "t30", "t30", "t30", "t30", "t0", "t0",
    "t0", "t0", "t0", "t0", "t0", "t0", "t0", "t0", "t30", "t30",
    "t30", "t30", "t30", "t30", "t30", "t30", "t30", "t30"
  ),
  Group = c(
    "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment"
  ),
  ID = c(
    "ID01", "ID02", "ID03", "ID04",
    "ID05", "ID06", "ID07", "ID08", "ID09", "ID10", "ID01",
    "ID02", "ID03", "ID04", "ID05", "ID06", "ID07", "ID08",
    "ID09", "ID10", "ID11", "ID12", "ID13", "ID14", "ID15",
    "ID16", "ID17", "ID18", "ID19", "ID20", "ID11", "ID12",
    "ID13", "ID14", "ID15", "ID16", "ID17", "ID18", "ID19",
    "ID20"
  ),
  Variable = c(
    87.4468574183993,
    61.6036264109425, 87.1487529366277, 50.6166256731376, 72.9583341162652,
    56.7137802368961, 65.8371140481904, 86.0453418688849,
    83.5156044340692, 68.5201310901903, 84.504537214525,
    56.6733720363118, 91.0738771315664, 94.9689620872959,
    76.23775213724, 74.8982135788538, 80.9279862442054,
    51.5291749034077, 85.7785020256415, 51.461190963164, 52.1936888922937,
    91.9786257203668, 83.2809177343734, 71.7776856268756,
    89.8885271861218, 84.3541337642819, 87.5198906287551,
    94.8092532227747, 56.6507609561086, 72.2115040174685,
    53.5416435450315, 61.9461603346281, 63.9171704184264,
    73.8425903371535, 77.3780003190041, 80.7108032982796,
    82.9249111004174, 72.6573116029613, 88.3334786165506, 50.722946121823
  )
)

One option to achieve your desired result would be to manually do the dodging for the geom_line by computing the x positions like so:

library(ggplot2)

width <- .75

mydata$x_line <- as.numeric(factor(mydata$Sex)) + 
  ifelse(mydata$Time == "t0", -1, 1) * width / 4

ggplot(mydata, aes(x = Sex, y = Variable, fill = Time)) +
  facet_grid(. ~ Group, scales = "free_x") +
  geom_boxplot(alpha = 0.80, outlier.colour = NA, position = position_dodge(width)) +
  geom_point(aes(group = Time), size = 2, shape = 21, position = position_dodge(width)) +
  geom_line(aes(x = x_line, group = ID), size = 0.3, alpha = 0.8)

DATA

mydata <- data.frame(
  Sex = c(
    "M", "M", "M", "M", "M", "M",
    "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
    "M", "M", "F", "F", "F", "F", "F", "F", "F", "F", "F",
    "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F"
  ),
  Time = c(
    "t0", "t0", "t0", "t0", "t0",
    "t0", "t0", "t0", "t0", "t0", "t30", "t30", "t30", "t30",
    "t30", "t30", "t30", "t30", "t30", "t30", "t0", "t0",
    "t0", "t0", "t0", "t0", "t0", "t0", "t0", "t0", "t30", "t30",
    "t30", "t30", "t30", "t30", "t30", "t30", "t30", "t30"
  ),
  Group = c(
    "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment", "control",
    "treatment", "control", "treatment", "control", "treatment",
    "control", "treatment", "control", "treatment"
  ),
  ID = c(
    "ID01", "ID02", "ID03", "ID04",
    "ID05", "ID06", "ID07", "ID08", "ID09", "ID10", "ID01",
    "ID02", "ID03", "ID04", "ID05", "ID06", "ID07", "ID08",
    "ID09", "ID10", "ID11", "ID12", "ID13", "ID14", "ID15",
    "ID16", "ID17", "ID18", "ID19", "ID20", "ID11", "ID12",
    "ID13", "ID14", "ID15", "ID16", "ID17", "ID18", "ID19",
    "ID20"
  ),
  Variable = c(
    87.4468574183993,
    61.6036264109425, 87.1487529366277, 50.6166256731376, 72.9583341162652,
    56.7137802368961, 65.8371140481904, 86.0453418688849,
    83.5156044340692, 68.5201310901903, 84.504537214525,
    56.6733720363118, 91.0738771315664, 94.9689620872959,
    76.23775213724, 74.8982135788538, 80.9279862442054,
    51.5291749034077, 85.7785020256415, 51.461190963164, 52.1936888922937,
    91.9786257203668, 83.2809177343734, 71.7776856268756,
    89.8885271861218, 84.3541337642819, 87.5198906287551,
    94.8092532227747, 56.6507609561086, 72.2115040174685,
    53.5416435450315, 61.9461603346281, 63.9171704184264,
    73.8425903371535, 77.3780003190041, 80.7108032982796,
    82.9249111004174, 72.6573116029613, 88.3334786165506, 50.722946121823
  )
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文