我正在尝试在函数内创建一个多面箱形图,以对患者数据进行暴露反应分析。该图按类别划分,y 轴为曝光度,x 轴为分类响应。我还想用观察数来注释该图。
一些数据:
set.seed(100)
dummy_data = data.frame(id = rep(1:50, each = 3),
expo = rnorm(50*3, mean = 50000, sd = 10000),
cat = rep(c("cat1", "cat2", "cat3"), times = 3, length.out = 50*3),
resp = sample(0:3, 50*3, replace = T))
outliers = data.frame(id = rep(51:60, each = 3),
expo = rnorm(10*3, mean = 300000, sd = 100000),
cat = rep(c("cat1", "cat2", "cat3"), times = 3, length.out = 10*3),
resp = 0)
dummy_data = rbind(dummy_data, outliers)
这是迄今为止我的代码:
boxplot_cat_er = function(data, exposure, response, cat, title = NULL, xlab = NULL, ylab = NULL) {
give.n <- function(x){
return(c(y = max(eval(substitute(exposure), data), na.rm = T), label = length(x)))
# experiment with the multiplier to find the perfect position
}
ggplot(data=data,
aes(x = as.factor(as.character({{response}})) , y = {{exposure}}))+
geom_boxplot(outlier.shape = NA) +
geom_jitter(width = 0.25) +
stat_summary(fun.data = give.n, geom = "text", color = "blue", fun.y = median, size=6) +
xlab(xlab)+
ylab(ylab)+
ggtitle(title) +
theme_bw() +
theme(axis.text.x=element_text(size=15),axis.text.y=element_text(size=15),
axis.title.y=element_text(size=15),axis.title.x=element_text(size=15),
legend.text=element_text(size=rel(1.2)), legend.title = element_text(size = rel(1.5)),
plot.title = element_text(size = 20)) +
facet_grid(as.formula(paste("~", paste(substitute(cat)))), scales = "free", space = "free") +
theme(strip.text.x = element_text(size = 15)) +
theme(plot.title = element_text(hjust = 0.5, vjust = 1))
}
除了蓝色注释之外,一切正常。问题似乎在于我的自定义
give.n
函数被传递给 stat_summary
函数。当我运行 boxplot_cat_er(dummy_data, expo, resp, cat)
时,我得到的图没有注释,并且出现以下错误:
评估仍然在 R 中暗示我。我做错了什么?
问题是
expo
是您传递给绘图函数的数据集 data
的一列。为了说明这一点,您可以使用
max(data[[deparse(substitute(exposure))]], na.rm = TRUE)
计算数据中
expo
的总体最大值。在下面的代码中,我还在调用 stat_summary
之外计算一次最大值,并将其存储在变量 max_y
中。
在您的情况下,您还可以考虑使用
geom_text
和 stat="count"
:
geom_text(
aes(label = after_stat(count), y = after_stat(max_y)),
stat = "count", color = "blue", size = 6
) +
library(ggplot2)
boxplot_cat_er <- function(data, exposure, response, cat, title = NULL, xlab = NULL, ylab = NULL) {
max_y <- max(data[[deparse(substitute(exposure))]], na.rm = TRUE)
give.n <- function(x) {
c(y = max_y, label = length(x))
}
ggplot(
data = data,
aes(x = as.factor(as.character({{ response }})), y = {{ exposure }})
) +
geom_boxplot(outlier.shape = NA) +
geom_jitter(width = 0.25) +
stat_summary(
fun.data = give.n, geom = "text",
color = "blue", size = 6
) +
facet_grid(cols = vars(cat), scales = "free", space = "free") +
labs(x = xlab, y = ylab, title = title) +
theme_bw() +
theme(
axis.text.x = element_text(size = 15), axis.text.y = element_text(size = 15),
axis.title.y = element_text(size = 15), axis.title.x = element_text(size = 15),
legend.text = element_text(size = rel(1.2)), legend.title = element_text(size = rel(1.5)),
plot.title = element_text(size = 20, hjust = 0.5, vjust = 1),
strip.text.x = element_text(size = 15)
)
}
boxplot_cat_er(dummy_data, expo, resp, cat)