我正在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: enter image description here

基本上,我希望3.5月龄组的点是Sham和mTBI数据的相同起点,但随后它会中断并连接到7.5月龄和9.5月龄组的点.目前,它只是连接所有数据点,这不是我想要的,但我不知道如何修复它.任何帮助都将不胜感激:).

推荐答案

因为您的数据没有3.5/mTBI行,并且您说希望mTBI行从3.5点开始,所以我建议我们需要复制3.5个月的行,将它们重新分类为mTBI,并将它们追加到数据中.

此外,我们需要从您的geom_line中删除group=1,因为它阻碍了我们对线路进行我们需要的操作.在本例中,它继承了对ggplot的原始调用及其美学group = factor(Treatment, levels = c("Sham", "mTBI")).

像这样的吗?

library(dplyr)
ungroup(FA_MRI_APOE) %>%
  filter(`Age (Months)` == "3.5") %>%
  mutate(Treatment = "mTBI") %>%
  bind_rows(FA_MRI_APOE) %>%
  ggplot(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() +
  # 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")

enter image description here

我注释掉了geom_points,因为我没有FA_MRI_APOE_raw的数据.

你提到了加分的可能性是一个问题.如果它看起来不适合您,我们可以过滤出3.5和mTBI数据,只需将上面的geom_point替换为

  geom_point(size = 2.5, data = ~ filter(., `Age (Months)` != "3.5" | Treatment != "mTBI")) +

R相关问答推荐

如何将y轴设置为在ggplot 2中x=0处与x轴相交?

如何将图案添加到ggplot中的一个类别

根据固定值范围在tible中添加新行

在通过最大似然估计将ODE模型与数据匹配时,为什么要匹配实际参数的转换值?

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

R Tidymodels textercipes-使用spacyR进行标记化-如何从生成的标记列表中删除标点符号

从R导出全局环境中的所有sf(numrames)对象

在数学中正确显示摄氏度、开氏度或华氏度

使用case_match()和char数组重新编码值

如何读取CSV的特定列时,给定标题作为向量

悬崖三角洲超大型群数计算导致整数溢出

在列表中排列R数据框中的列顺序

派生程序包| ;无法检索';return()';的正文

从R中发出的咕噜声中的BUG?

我们如何在R中透视数据并在之后添加计算

R中Gamma回归模型均方误差的两种计算方法不一致

警告消息";没有非缺失的参数到min;,正在返回数据中的inf";.表分组集

优化从每个面的栅格中提取值

计算多变量的加权和

我正在try 创建一个接近cos(X)的值的While循环,以便它在-或+1-E10范围内