复现Nature期刊图,换数据即可用!
本次使用R语言复现一下Nature 子刊中的1张图!
✅读入测试数据,
✅KS统计检验,计算p值,
# 通用函数:执行KS检验并格式化p值
format_ks_pvalue <- function(sample1, sample2) {
p_value <- ks.test(sample1, sample2)$p.value
gsub("e", "e", format(p_value, scientific = TRUE, digits = 4))
}
✅density plot,
# 核心代码
create_density_plot <- function(data, x_range, p_value, p_x, p_y,
x_label, fill_var = "variable",
expand_x = c(0, 0), expand_y = c(0, 0.05)) {
ggplot(data, aes(x = value, fill = !!sym(fill_var))) +
geom_density(alpha = 0.5, size = 0.25) +
scale_fill_manual(values = color_palette, labels = var_labels) +
labs(x = x_label, y = "Density") +
scale_x_continuous(limits = x_range, expand = expand_x) +
scale_y_continuous(expand = expand_y) +
annotate("text", size = 2,
label = paste0("italic(p)==", '"', p_value, '"'),
x = p_x, y = p_y, parse = TRUE)
}
(加入学习,收费,备注:299)