一步步绘制和美化dot plot
简介
开始
1. 查看数据并转换为长数据格式
2. 绘制基础图形
3. 添加errorbar和pointrange
4. 修改颜色和坐标轴主题
5. 加注释
总结
简介
作为文章中经常出现的一种图形,dot plot 可以展示点的分布和统计变化之后的数据均值等特征值。以下是一篇已发表的文章中的图,今天我们将构造数据,一步步实现它。
开始
1. 查看数据并转换为长数据格式
library(tidyverse)
library(magrittr)
data %<>% pivot_longer(cols = wt:mz_ko)
2. 绘制基础图形
data %>%
ggplot(aes(group, value, fill = name)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.9))
3. 添加errorbar和pointrange
data %>%
ggplot(aes(group, value, fill = name)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.9)) +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="errorbar", color="black", width = .07,
position = position_dodge(.9), size = .8)
data %>%
ggplot(aes(group, value, fill = name)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.9)) +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="errorbar", color="black", width = .07,
position = position_dodge(.9), size = .8) +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="black", position = position_dodge(.9))
有点感觉了哦!
4. 修改颜色和坐标轴主题
data %>%
ggplot(aes(group, value, fill = name)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.9), color = "grey77") +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="errorbar", color="black", width = .07,
position = position_dodge(.9), size = .8) +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="black", position = position_dodge(.9)) +
scale_y_continuous(limits = c(0, 1.5), expand = c(0, 0)) +
scale_fill_manual(values = c("grey77", "grey77")) +
labs(x= "", y = "Relative itensity") +
theme_classic() +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
legend.position = "none")
最后,再加上注释就可以了,来实现吧。
5. 加注释
加线段和文本建议使用AI,当然也可以用R实现。
data %>%
ggplot(aes(group, value, fill = name)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.9), color = "grey77") +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="errorbar", color="black", width = .07,
position = position_dodge(.9), size = .8) +
stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="black", position = position_dodge(.9)) +
geom_segment(aes(x = 0.5, y = 1.37, xend = 1.5, yend = 1.37),
color = "black", size = .5) +
geom_segment(aes(x = 1.6, y = 1.37, xend = 2.3, yend = 1.37),
color = "black", size = .5) +
annotate(geom = "text", x = c(0.95, 1.95), y = 1.47,
label = c("Early 2-cell", "Late 2-cell")) +
annotate(geom= "text", x = c(0.77, 1.2, 1.75, 2.22),
y = 1.3, label = c(rep(c("WT", "MZ-KO"),2))) +
scale_y_continuous(limits = c(0, 1.5), expand = c(0, 0)) +
scale_fill_manual(values = c("grey77", "grey77")) +
labs(x= "", y = "Relative itensity") +
theme_classic() +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
legend.position = "none")
总结
绘图时要思路清楚,一步一步实现。勤加练习!
————————————————
版权声明:本文为CSDN博主「生信小菜鸟啊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40794743/article/details/113989344