我有一个有两个变量的 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)
有没有办法控制字母在x方向上的位置,使它们与箱线图相对应?
为了达到您想要的结果
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)
)