我在显示包含多种代谢物的条形图上的 t 检验显着性值时遇到问题。我写的代码如下:
my_comparisons <- list(c("0", "50"), c("0", "100"), c("0", "150"))
lr1to13p <- ggplot(lr_data_long_1to13, aes(x=Metabolite, y=Value, fill=factor(CSE.Dose))) +
geom_bar(stat="summary", fun="mean", position=position_dodge(width=0.8), alpha=0.7) +
geom_point(aes(color=factor(CSE.Dose)), position=position_dodge(width=0.8), size=2) +
theme_minimal() +
labs(x="Metabolite", y="Relative Expression", fill="CSE.Dose", color="CSE.Dose") +
theme(axis.text.x = element_text(angle=45, hjust=1)) +
stat_compare_means(comparisons = my_comparisons) +
stat_compare_means(method = "t.test", paired = T, label = "p.signif", vjust = 1)
输出是
但我的目的是在每种代谢物的每个剂量(条形)上直接添加 3 个重要符号。
我尝试过调整参数中的参数以及包含比较术语来获得特定的比较,但这些都没有真正移动重要性符号。
您没有提供最小的工作示例,因此您期望我们“在黑暗中工作”!
好吧,希望这能实现你想要的:
# Simulate data
set.seed(42)
metabolites <- c("AcetylCarnitine", "Acotinate", "Choline", "Lactate", "Leucine", "N1 MethylNicotinamide", "Nicotinamide", "Proline", "Succinate", "Thiamine", "Tryptophan", "Tyrosine", "Valine")
cse_doses <- c(0, 50, 100, 150)
data_list <- lapply(metabolites, function(metabolite) {
data.frame(
Metabolite = metabolite,
CSE.Dose = as.factor(rep(cse_doses, each = 10)),
Value = c(rnorm(10, mean = 2, sd = 0.5),
rnorm(10, mean = 3, sd = 0.5),
rnorm(10, mean = 4, sd = 0.5),
rnorm(10, mean = 5, sd = 0.5))
)
})
lr_data_long_1to13 <- do.call(rbind, data_list)
# Define comparisons for t-tests
my_comparisons <- list(c("0", "50"), c("0", "100"), c("0", "150"))
# Calculate pairwise t-tests manually
calculate_p_values <- function(data, group_var, value_var, comparisons) {
p_values <- data %>%
group_by(Metabolite) %>%
do({
test_results <- list()
for (comp in comparisons) {
group1 <- .[[value_var]][.[[group_var]] == comp[1]]
group2 <- .[[value_var]][.[[group_var]] == comp[2]]
if (length(group1) > 1 && length(group2) > 1) {
test_results[[paste(comp, collapse = "_vs_")]] <- t.test(group1, group2, paired = FALSE)$p.value
} else {
test_results[[paste(comp, collapse = "_vs_")]] <- NA
}
}
data.frame(test_results)
})
return(p_values)
}
p_values <- calculate_p_values(lr_data_long_1to13, "CSE.Dose", "Value", my_comparisons)
# Create annotations for plot
annotations <- data.frame(
Metabolite = rep(unique(lr_data_long_1to13$Metabolite), each = length(my_comparisons)),
Comparison = rep(paste0("(", sapply(my_comparisons, paste, collapse = " vs "), ")"), times = length(unique(lr_data_long_1to13$Metabolite))),
P.value = unlist(p_values[-1]) # Remove the group variable column
)
# Add significance levels
annotations$Significance <- cut(annotations$P.value, breaks = c(-Inf, 0.001, 0.01, 0.05, Inf), labels = c("***", "**", "*", "ns"))
# Determine the y-axis position for the annotations
max_values <- lr_data_long_1to13 %>%
group_by(Metabolite) %>%
summarise(max_value = max(Value))
annotations <- annotations %>%
left_join(max_values, by = "Metabolite") %>%
group_by(Metabolite) %>%
mutate(y_position = max_value + 0.5 + row_number() * 0.3)
# Adjust y-axis limits to fit the annotations
y_max <- max(annotations$y_position) + 0.5
# Now Creat the plot
lr1to13p <- ggplot(lr_data_long_1to13, aes(x = Metabolite, y = Value, fill = CSE.Dose)) +
geom_bar(stat = "summary", fun = "mean", position = position_dodge(width = 0.8), alpha = 0.7) +
geom_point(aes(color = CSE.Dose), position = position_dodge(width = 0.8), size = 2) +
theme_minimal() +
labs(x = "Metabolite", y = "Relative Expression", fill = "CSE.Dose", color = "CSE.Dose") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
ylim(0, y_max) # Adjust y-axis limits
# Add significance annotations with optional tweaks
lr1to13p <- lr1to13p +
geom_text(data = annotations,
aes(x = Metabolite, y = y_position, label = Significance),
inherit.aes = FALSE, size = 5, vjust = -1, color = "red") # Increase size and change color
print(lr1to13p)