1. 写在前面
原文地址:
https://www.nature.com/articles/nature25479
今天要复刻的是这张Nature
上的高颜值统计图,一半box
,一半jitter
2. 用到的包
代码语言:text复制rm(list = ls())
library(tidyverse)
library(cowplot)
library(ggparl)
3. 示例数据
代码语言:text复制dat <- read.csv("geom_boxjitter_example.csv",row.names = 1)
长这个样子 ᑋᵉᑊᑊᵒ ᵕ̈ ᑋᵉᑊᑊᵒ
4. 数据整理
这里我们先把数据整理成ggplot
需要的格式
@(。・o・)@
代码语言:text复制dat_long <- dat %>%
gather(key, value, 1:6) %>%
mutate(loc = factor(loc, levels = c("abro", "dome")),
type = factor(type),
key = factor(key))
5. 开始画图
5.1 初步绘图
代码语言:text复制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 修改细节
去掉背景,更改颜色
代码语言:text复制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 只展示其中比较好看的两组
我果然是个颜狗啊 /ᐠ。ꞈ。ᐟ
代码语言:text复制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
6. 最后把上面的结果都拼到一起吧
代码语言:text复制plot_grid(p1, p2, p3, p5,
labels = "AUTO",
label_size = 30)
最后祝大家早日不卷!~