
jamesbang
V1
2022/08/29阅读:63主题:雁栖湖
🤗 boxjitter | 完美复刻Nature上的高颜值统计图
Part1boxjitter | 完美复刻Nature上的高颜值统计图
11. 写在前面
原文地址:
https://www.nature.com/articles/nature25479
今天要复刻的是这张Nature
上的高颜值统计图,一半box
,一半jitter

22. 用到的包
rm(list = ls())
library(tidyverse)
library(cowplot)
library(ggparl)
33. 示例数据
dat <- read.csv("geom_boxjitter_example.csv",row.names = 1)
长这个样子 ᑋᵉᑊᑊᵒ ᵕ̈ ᑋᵉᑊᑊᵒ

44. 数据整理
这里我们先把数据整理成ggplot
需要的格式
@(。・o・)@
dat_long <- dat %>%
gather(key, value, 1:6) %>%
mutate(loc = factor(loc, levels = c("abro", "dome")),
type = factor(type),
key = factor(key))

55. 开始画图
5.1 初步绘图
p1 <- ggplot(dat_long, aes(x = type, y = value, fill = key))+
geom_boxjitter(outlier.color = NA,
jitter.shape = 21,
jitter.color = NA,
jitter.height = 0.05,
jitter.width = 0.075,
errorbar.draw = TRUE)
p1

5.2 减少分组
原图只有四个组,我们在这里用filter
函数过滤一下,变成四组
dat_long <- dat_long %>%
filter(key %in% c("p1", "p2","p3","p4"))
p2 <- ggplot(dat_long,
aes(x = type, y = value, fill = key))+
geom_boxjitter(outlier.color = NA,
jitter.shape = 21,
jitter.color = NA,
jitter.height = 0.05,
jitter.width = 0.075,
errorbar.draw = TRUE)
p2

5.3 修改细节
去掉背景,更改颜色
p3 <- p2 +
theme_bw()+
theme(panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line(colour = "black"),
legend.position = "none") +
ylim(c(-0.1, 1.05)) +
scale_fill_manual(values = c("#0E72BA", "#D95426",
"#ecb21e", "#812e91"))
p3

5.4 只展示其中比较好看的两组
我果然是个颜狗啊 /ᐠ。ꞈ。ᐟ\
p4 <- ggplot(
dat_long %>% filter(key %in% c("p1", "p2")),
aes(x = type, y = value, fill = key)) +
geom_boxjitter(outlier.color = NA, jitter.shape = 21, jitter.color = NA,
jitter.height = 0.05, jitter.width = 0.075, errorbar.draw = TRUE) +
theme_bw()+
theme(panel.grid = element_blank(),
panel.border = element_blank(),
axis.line = element_line(colour = "black"),
legend.position = "none") +
ylim(c(-0.05, 1.05)) +
scale_fill_manual(values = c("#ecb21e", "#812e91"))
p4

5.5 加上统计值
这里我们用ggpubr
包的stat_compare_means()
函数给神图加上统计值
library(ggpubr)
# 添加p值
p5 <- p4 +
stat_compare_means(#label = "p.signif",
#label.x = 1.5,
method = "t.test",
label.y = 1)
p5

66. 最后把上面的结果都拼到一起吧
plot_grid(p1, p2, p3, p5,
labels = "AUTO",
label_size = 30)


需要示例数据的小伙伴,在公众号回复
boxjitter
获取吧点个在看吧各位~ ✐.ɴɪᴄᴇ ᴅᴀʏ 〰
作者介绍

jamesbang
V1
wx🔍: Grassssss 卷起来了