首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >78-R可视化14-不同分组差异基因数目的上下柱状图显示

78-R可视化14-不同分组差异基因数目的上下柱状图显示

作者头像
北野茶缸子
发布2022-01-04 19:38:22
发布2022-01-04 19:38:22
1.9K0
举报

前言

在上一节中[[77-R可视化13-多个ggplot图象映射实现以假乱真的dodge+stack效果]],我们提到了这张图:

下面是本来要复现的图:

有同学给我说了,这个图其实是有它的道理的,它其实显示的是,堆叠在一起的OM 与YM 的差异基因,各自有50多,合计对应Y 轴上的内容。

哇,原来是这样。那它就更垃圾了。

这种基本的misunderstanding 可是数据科学家可视化的禁忌啊!

你明明有更好的选择,比如将另一个对比分组调整到坐标轴的负轴。

代码实现

非常简单,假数据和绘图我一并写了:

代码语言:javascript
复制

# fake data
a1 <- data.frame(
  counts = c(-53, -40, -59, -39), #将a1显示在x轴下方
  type1 = c(rep("NK",2), rep("TC",2)),
  type2 = rep(c("YM", "YF"), 2)
)
a1$counts2 <- abs(a1$counts) #为了让label 显示正值

a2 <- data.frame(
  counts = c(52, 24, 57,28),
  type1 = c(rep("NK",2), rep("TC",2)),
  type2 = rep(c("OM", "OF"), 2)
)

ggplot() + 
  geom_col(data = a2, aes(type1, counts, fill = type2),
           position = "dodge") +
  geom_col(data = a1, 
                    aes(type1, counts, fill = type2),
                    position = "dodge") +
  geom_text(data = a2, 
            aes(type1, counts,fill = type2, label = counts),
            position = position_dodge(0.9), vjust = -0.8) + 
  geom_text(data = a1, 
            aes(type1, counts,fill = type2, label = counts2),
            position = position_dodge(0.9), vjust = 1.5) + 
  ggthemes::theme_economist() + 
  # 在这个网站找颜色https://colorbrewer2.org/
  scale_fill_manual(values = c("#fc9272", "#9ecae1", "#de2d26","#3182bd"))

但忽然发现,结果有点突兀:

这里发现因为存在两个图形映射,分面让人很不满意:

稍微修改下主题再出图:

代码语言:javascript
复制

p1 <- ggplot() + 
  geom_col(data = a2, aes(type1, counts, fill = type2),
           position = "dodge") +
  geom_col(data = a1, 
                    aes(type1, counts, fill = type2),
                    position = "dodge") +
  geom_text(data = a2, 
            aes(type1, counts,fill = type2, label = counts),
            position = position_dodge(0.9), vjust = -0.8) + 
  geom_text(data = a1, 
            aes(type1, counts,fill = type2, label = counts2),
            position = position_dodge(0.9), vjust = 1.5) + 
  ggthemes::theme_economist() + 
  # 在这个网站找颜色https://colorbrewer2.org/
  scale_fill_manual(values = c("#fc9272", "#9ecae1", "#de2d26","#3182bd")) + 
  labs(x = NULL) + theme(
    axis.ticks.x = element_blank()
  )
p1

这种100以内的加法,还需要一个坐标轴告诉你差异基因的数目吗?

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 北野茶缸子 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档