我有两组相对于基线的百分比变化数据,并且我遇到了异常值。为了可视化数据,我创建了一个箱线图,并使用 stat_summary 在底部添加了观察数。此外,我还进行了一项测试来评估相对于基线的百分比变化是否与零显着不同。
我尝试将 y 轴分成两部分,但遇到了一个问题:观察数显示了两次。
这是我的数据和我在 R 中的尝试:
set.seed(200)
# data
df <- data.frame(
G1 = c(rep("A", 8), rep("B", 30)),
G2 = c(rnorm(8, mean = 5, sd = 10), rnorm(29, mean = 50, sd = 20), 1000)
)
# ----function for Annotate sample size----
n_fun <- function(x){
return(data.frame(y = -Inf,
label = paste("N=", length(x), "\n")))
}
# calculate the mean and SD by group
mean_sd <- df %>%
group_by(G1) %>%
summarize(
rmean = mean(G2),
rsd = sd(G2)
)
# ____ Boxplots:
theme_set(theme_minimal())
library(ggpubr)
library(ggbreak)
# gplot
ggboxplot(df, x = "G1", y = "G2", color = "G1", add = "jitter", palette = "jco") +
# Axis and legende
xlab("") +
ylab("Percent Change") +
theme(legend.key.size = unit(2.5, "lines")) +
scale_y_break(c(300, 950)) + # BREAK
theme(axis.text.x = element_text(angle = 0, size = 9),
axis.text.y = element_text(size = 10)) +
# Sample size
stat_summary(fun.data = n_fun, geom = "text",
aes(group = G1), hjust = 0.8,
position = position_dodge(0.9), size = 3) +
# Wilcoxon test mean comparaison
stat_compare_means(method = "wilcox.test", method.args = list(mu = 0),
label = 'p.signif') +
# y = 0
geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
# Mean and sd
geom_text(data = mean_sd, aes(x = G1, y = rmean,
label = paste("Mean(SD):",
round(rmean, 2),
"(",
round(rsd, 2),
")")),
vjust = -15, color = "black")
您能帮我修正一下图表吗? 谢谢你:)
这似乎与 ggbreak 如何对待
-Inf
有关。您可以改为使用基于 df 值的计算位置,例如类似于 min(df$G2) * 5
里面的 n_fun
:
library(tidyverse)
library(ggpubr)
library(ggbreak)
set.seed(200)
# data
df <- data.frame(
G1 = c(rep("A", 8), rep("B", 30)),
G2 = c(rnorm(8, mean = 5, sd = 10), rnorm(29, mean = 50, sd = 20), 1000)
)
n_fun <- function(x){
return(data.frame(y = min(df$G2) * 5,
label = paste("N=", length(x), "\n")))
}
# calculate the mean and SD by group
mean_sd <- df %>%
group_by(G1) %>%
summarize(
rmean = mean(G2),
rsd = sd(G2)
)
# ____ Boxplots:
theme_set(theme_minimal())
# gplot
ggboxplot(df, x = "G1", y = "G2", color = "G1", add = "jitter", palette = "jco") +
# Axis and legende
xlab("") +
ylab("Percent Change") +
theme(legend.key.size = unit(2.5, "lines")) +
scale_y_break(c(300, 950)) + # BREAK
theme(axis.text.x = element_text(angle = 0, size = 9),
axis.text.y = element_text(size = 10)) +
# Sample size
stat_summary(fun.data = n_fun, geom = "text",
aes(group = G1), hjust = 0.8,
position = position_dodge(0.9), size = 3) +
# Wilcoxon test mean comparaison
stat_compare_means(method = "wilcox.test", method.args = list(mu = 0),
label = 'p.signif') +
# y = 0
geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
# Mean and sd
geom_text(data = mean_sd, aes(x = G1, y = rmean,
label = paste("Mean(SD):",
round(rmean, 2),
"(",
round(rsd, 2),
")")),
vjust = -15, color = "black")
创建于 2024-03-17,使用 reprex v2.1.0