欢迎关注R语言数据分析指南
❝本节来介绍如何使用「ggplot2」来绘制小清新版箱线图并添加统计信息,下面通过一个小栗子来进行展示
❞
加载R包
library(tidyverse)
library(scales)
library(ggtext)
导入数据
binded_first <- read.delim("data.xls",sep="\t")
自定义分面标签
facet_labels <- data.frame(test = c("aca199", "ca125", "cea", "crp"),
label = c("CA 19-9 [U/mL]", "CA 125 [U/mL]",
"CEA [ng/mL]", "CRP [mg/mL]"))
自定义刻度标签
my_y_labels <- c(0,.01,.1,.5,1,5,10,50,100,500,"1,000","10,000")
数据清洗
stats_data_first <- binded_first %>%
pivot_longer(first_aca199:first_crp,names_to = "names",
values_to = "values") %>%
separate(names, into = c("time", "test"), sep = "_") %>%
group_by(test) %>%
summarise(p_value = round(wilcox.test(values~group)$p.value, 2)) %>%
mutate(label = if_else(p_value < .05 , paste0(p_value, "*"), as.character(p_value)),
label_asteriks = if_else(p_value == 0, "p<0.01*", paste0("p=", as.character(label))))
# A tibble: 4 x 4
test p_value label label_asteriks
<chr> <dbl> <chr> <chr>
1 aca199 0.27 0.27 p=0.27
2 ca125 0.34 0.34 p=0.34
3 cea 0.73 0.73 p=0.73
4 crp 0.81 0.81 p=0.81
数据可视化
binded_first %>%
pivot_longer(first_aca199:first_crp,
names_to = "names",
values_to = "values") %>%
separate(names, into = c("time", "test"), sep = "_") %>%
ggplot(aes(group, values)) +
stat_boxplot(geom ="errorbar", width = .3, size=.7, coef = 1) +
geom_boxplot(width = .55, outlier.shape = NA, coef = 1) +
geom_point(position = position_jitter(seed = 2021, width = .25),
aes(color = group, shape = group), alpha = .4, size = 3) +
scale_y_continuous(trans = "log10", breaks = c(0, .01, .1, .5, 1, 5, 10, 50, 100, 500, 1000, 10000),
limits = c(0.003,9500),
labels = as.character(my_y_labels)) +
scale_color_manual(values = c("#3B9AB2", "#00A08A")) +
facet_wrap(~ test, nrow = 1, strip.position = "right") +
scale_x_discrete(position = "top",labels = c("Controls", "iCRS/HIPEC")) +
theme_bw()+
theme(strip.text = element_blank(),
panel.spacing = unit(0, "cm"),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_text(size = 12, vjust = 1,color="black"),
axis.ticks.x.top = element_line(color = "black", size = .5),
axis.ticks.length = unit(-0.2, "cm"),
axis.ticks.y = element_line(color = "black", size = .5),
axis.text.y = element_text(size = 10,color="black"),
legend.position = "none",
panel.grid = element_blank(),
plot.margin=unit(c(0.5,0.1,0.5,0.1),"cm")) +
geom_rect(xmin=1, xmax=2, ymin=-1.7, ymax=-2.3, color="black", fill="white", size = .5)+
geom_text(data = facet_labels, aes(x = 1.5, y = .01, label = label), size = 4.1) +
geom_text(data = stats_data_first, aes(x = 1.5, y = 3900, label = label_asteriks), size = 5) +
geom_segment(aes(x=1, xend=2, y=2300, yend=2300), color="black") +
geom_segment(aes(x=1, xend=1, y=1600, yend=3500), color="black") +
geom_segment(aes(x=2, xend=2, y=1600, yend=3500), color="black")
数据获取
❝可以看到相对与常见的分面箱线图,此图别有一番小清新的感觉,转发此文档附上一句话到朋友圈后台截图给我,即可获取对应的数据及代码
❞
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流

微信交流群

「关注下方公众号下回更新不迷路」添加作者微信请备注单位+方向+姓名
2022-02-17
2022-02-07
2022-02-11
2022-02-06
2022-01-28
2022-01-26
2022-01-24
2022-01-25

