R语言基础绘图——dot plot

简介

作为文章中经常出现的一种图形,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")

在这里插入图片描述

总结

绘图时要思路清楚,一步一步实现。勤加练习!

欢迎关注我的微信公众号。
在这里插入图片描述

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值