如何防止重要字母在具有两个变量的 ggplot 中堆叠?

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

我有一个有两个变量的 df,基因型和治疗;在第二个数据框中,我已经有了与两个变量的组合相关的重要字母。所以基因型 1:治疗 1 可能是 A,基因型 1:治疗 2 B 等等。

我希望将字母绘制在相应基因型箱线图的下方或上方。

示例代码:

# Load the necessary library
library(ggplot2)

# Example data (replace with your own data)
set.seed(123)
treatment <- rep(c("Treatment1", "Treatment2"), each = 25)
genotype <- rep(c("Genotype1", "Genotype2", "Genotype3", "Genotype4", "Genotype5"), times = 10)
value <- rnorm(50, mean = 10, sd = 2)
data <- data.frame(treatment, genotype, value)

# Example dataframe with letters for significance
significance_df <- data.frame(
  treatment = c("Treatment1", "Treatment1", "Treatment2", "Treatment2"),
  genotype = c("Genotype1", "Genotype2", "Genotype3", "Genotype4"),
  letters = c("A", "B", "C", "D")
)

# Calculate x-coordinate for each letter
significance_df$x_pos <- with(significance_df, ave(as.numeric(treatment), treatment, FUN = function(x) seq_along(x)))

# Create the boxplot
ggplot(data, aes(x = treatment, y = value, fill = genotype)) +
  geom_boxplot() +
  labs(x = "Treatment", y = "Value", title = "Boxplot of Treatment vs Value by Genotype") +
  scale_fill_manual(values = c("Genotype1" = "blue", "Genotype2" = "red", "Genotype3" = "green", "Genotype4" = "orange", "Genotype5" = "purple")) +
  theme_minimal() +
  geom_text(data = significance_df, aes(label = letters, y = 7, x = x_pos), vjust = -1)


不幸的是,这会导致字母堆叠在治疗位置上,使它们无法区分。 Boxplots of treamtents and genotypes

有没有办法控制字母在x方向上的位置,使它们与箱线图相对应?

r ggplot2 boxplot letter significance
1个回答
0
投票

为了达到您想要的结果

complete
您的标签数据集包括
treatment
genotype
的所有组合,然后将
treatment
映射到
x
中的
geom_text
上并使用
position = position_dodge(.75)
来躲避标签:

library(ggplot2)
library(tidyr)

significance_df <- significance_df |>
  tidyr::complete(
    treatment,
    genotype = factor(genotype, levels = paste0("Genotype", 1:5))
  ) |>
  tidyr::replace_na(list(letters = ""))

ggplot(data, aes(x = treatment, y = value, fill = genotype)) +
  geom_boxplot() +
  labs(x = "Treatment", y = "Value", title = "Boxplot of Treatment vs Value by Genotype") +
  scale_fill_manual(values = c(
    "Genotype1" = "blue", "Genotype2" = "red",
    "Genotype3" = "green", "Genotype4" = "orange",
    "Genotype5" = "purple"
  )) +
  theme_minimal() +
  geom_text(
    data = significance_df, aes(
      label = letters, y = 7, x = treatment
    ),
    position = position_dodge(.75)
  )

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