我正在try 创建一个曲线图,其中的线连接到各自的组,但两组都从相同的起点开始.
我有一个数据帧,看起来是这样的:
FA_MRI_APOE
Condition Treatment Age (Months) Region avg_FA sd_FA
<fctr> <chr> <fctr><chr> <dbl> <dbl>
APOE2 Sham 3.5 CC 0.1990432 NA
APOE2 Sham 3.5 EC 0.2269353 NA
APOE2 Sham 3.5 HP 0.2253147 NA
APOE2 Sham 3.5 TH 0.3257256 NA
APOE2 Sham 7.5 CC 0.3093073 0.08619885
APOE2 Sham 7.5 EC 0.2255272 0.07652789
APOE2 Sham 7.5 HP 0.2897462 0.02708867
APOE2 Sham 7.5 TH 0.2866555 0.03557000
APOE2 Sham 9.5 CC 0.1840524 NA
APOE2 Sham 9.5 EC 0.3347699 NA
数据输出:
structure(list(Condition = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), levels = c("APOE2", "APOE3", "APOE4",
"PS19"), class = "factor"), Treatment = c("Sham", "Sham", "Sham",
"Sham", "Sham", "Sham", "Sham", "Sham", "Sham", "Sham", "Sham",
"Sham", "mTBI", "mTBI", "mTBI", "mTBI", "Sham", "Sham", "Sham",
"Sham", "Sham", "Sham", "Sham", "Sham", "mTBI", "mTBI", "mTBI",
"mTBI"), `Age (Months)` = structure(c(1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), levels = c("3.5", "7.5", "9.5"), class = "factor"),
Region = c("CC", "EC", "HP", "TH", "CC", "EC", "HP", "TH",
"CC", "EC", "HP", "TH", "CC", "EC", "HP", "TH", "CC", "EC",
"HP", "TH", "CC", "EC", "HP", "TH", "CC", "EC", "HP", "TH"
), avg_FA = c(0.199043221771717, 0.226935303, 0.225314745679497,
0.325725596398115, 0.309307302666667, 0.225527225666667,
0.289746216333333, 0.286655532, 0.184052395, 0.334769852,
0.34609792, 0.237951324, 0.303560921, 0.233204448, 0.30656011,
0.296695315, 0.256386488908901, 0.2787716635, 0.270610670559108,
0.265520500484854, 0.289559764, 0.337927988666667, 0.370170086833333,
0.352682695666667, 0.391898785, 0.112882524, 0.29461883,
0.258586437), sd_FA = c(NA, NA, NA, NA, 0.0861988548059451,
0.0765278945487883, 0.0270886655892862, 0.0355699995442251,
NA, NA, NA, NA, NA, NA, NA, NA, 0.0332061193275388, 0.0121768772823131,
0.0441863595350275, 0.063138972154108, 0.11418350071109,
0.0829486022039601, 0.0831420050073176, 0.0689368863615038,
NA, NA, NA, NA)), class = c("grouped_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -28L), groups = structure(list(
Condition = structure(c(1L, 1L, 1L, 1L, 3L, 3L, 3L), levels = c("APOE2",
"APOE3", "APOE4", "PS19"), class = "factor"), Treatment = c("Sham",
"Sham", "Sham", "mTBI", "Sham", "Sham", "mTBI"), `Age (Months)` = structure(c(1L,
2L, 3L, 2L, 1L, 2L, 2L), levels = c("3.5", "7.5", "9.5"), class = "factor"),
.rows = structure(list(1:4, 5:8, 9:12, 13:16, 17:20, 21:24,
25:28), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -7L), .drop = TRUE))
以下是我为生成剧情所写的内容:
ggplot(FA_MRI_APOE, aes(x = `Age (Months)`,
y = avg_FA,
color = factor(Treatment, levels = c("Sham", "mTBI")),
group = factor(Treatment, levels = c("Sham", "mTBI")))) +
geom_errorbar(aes(ymin = avg_FA - sd_FA,
ymax = avg_FA + sd_FA),
width = 0.1) +
geom_point(size = 2.5) +
geom_line(group = 1) +
geom_jitter(aes(x = `Age (Months)`,
y = `Mean`),
position = position_jitter(0.1),
alpha = 0.5,
size = 1.5,
data = FA_MRI_APOE_raw) +
scale_color_manual(name = "Treatment", values = c("#0074C1", "#F7530B")) +
facet_grid(Region ~ Condition) +
theme(legend.position = "bottom")
The generated plot looks like this:
基本上,我希望3.5月龄组的点是Sham和mTBI数据的相同起点,但随后它会中断并连接到7.5月龄和9.5月龄组的点.目前,它只是连接所有数据点,这不是我想要的,但我不知道如何修复它.任何帮助都将不胜感激:).