在 ggplot 中,我想用一条线连接 x 分组变量中的点

问题描述 投票:0回答:1

我有一个名为 Pathway2 的分组变量和一个进一步分组的 Treatment 变量。 我希望根据我的分组变量通过一条线连接治疗变量的两个级别。 我目前的代码是这样的,它会产生下图。

ggplot(df, aes(x = Pathway2, y = Detected.spots, color = Treatment)) +
    geom_point(size = 3, stat = 'summary', fun.y = 'mean', position = position_dodge(width = 0.5)) +
    geom_errorbar(stat = 'summary', fun.data = 'mean_se', width = 0, fun.args = list(mult = 1.96), 
                  position = position_dodge(width = 0.5)) +
    geom_line(stat = 'summary', aes(group = Treatment), position = position_dodge(width = 0.5)) +  # Összekapcsolt vonalak hozzáadása
    facet_wrap(~Cancer.type.in.co.culture + Time, ncol = 2) +
    labs(x = "Pathway", y = "Detected spot", color = "Treatment") +
    scale_color_manual(values = c("black", "red")) +
    theme_gray(base_size = 20)

enter image description here

但是我想看到这个结果,有人可以帮助我吗?

enter image description here

ggplot2
1个回答
0
投票

困难在于以一种方式躲避线路的一端,以另一种方式躲避另一端:)

有几个选项(也可能是其他选项):

  1. 通过在 x 轴上组合路径和治疗,第一个方法会更容易。
  2. 第二个在美学上更好,但有点棘手,因为它是一个两步情节。

看看这些是否适合您?

(ggpubr 中还有

ggpaired()
可能值得一看,但可能需要重新调整数据。)

我制作了少量示例数据,因为我们没有您的数据,但希望它足以说明这些选项。

library(tidyverse)

df <- tribble(
  ~Pathway2, ~Detected.spots, ~Treatment, ~Cancer.type.in.co.culture, ~Time,
  "C-E", 1, "a", "Melanona", 24,
  "C-E", 2, "a", "Melanona", 24,
  "C-E", 5, "b", "Melanona", 24,
  "C-E", 6, "b", "Melanona", 24,
  "C-F", 1, "a", "Melanona", 24,
  "C-F", 2, "a", "Melanona", 24,
  "C-F", 6, "b", "Melanona", 24,
  "C-F", 7, "b", "Melanona", 24
)

# Option 1 - x-axis is Pathway & Treatment instead of dodging
df |> 
  mutate(pairing = paste(Pathway2, "\n", Treatment)) |> 
  ggplot(aes(x = pairing, y = Detected.spots, color = Treatment)) +
  geom_point(size = 3, stat = 'summary') +
  geom_errorbar(stat = 'summary', fun.data = 'mean_se', width = 0, fun.args = list(mult = 1.96)) +
  geom_line(stat = 'summary', aes(group = Pathway2), colour = "grey30", linetype = "dashed") + 
  facet_wrap(~Cancer.type.in.co.culture + Time, ncol = 2) +
  labs(x = "Pathway", y = "Detected spot", color = "Treatment") +
  scale_color_manual(values = c("black", "red")) +
  theme_gray(base_size = 20)


# Option 2 - Save the plot without lines, get the coordinates of the points, then add lines
p <- df |> 
  ggplot(aes(x = Pathway2, y = Detected.spots, color = Treatment)) +
  geom_point(size = 3, stat = 'summary', position = position_dodge(width = 0.5)) +
  geom_errorbar(stat = 'summary', fun.data = 'mean_se', width = 0, fun.args = list(mult = 1.96), 
                position = position_dodge(width = 0.5)) +
  # geom_line(stat = 'summary', aes(group = Treatment), position = position_dodge(width = 0.5)) + 
  facet_wrap(~Cancer.type.in.co.culture + Time, ncol = 2) +
  labs(x = "Pathway", y = "Detected spot", color = "Treatment") +
  scale_color_manual(values = c("black", "red")) +
  theme_gray(base_size = 20)

line_df <- layer_data(p, 1) |> 
  select(x, y, Treatment = colour) |> 
  mutate(pairing = (row_number() + 1) %/% 2)

p + geom_line(aes(x, y, group = pairing), colour = "grey30", data = line_df)

创建于 2024-04-25,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.