R绘图 | 云雨图+双向条形图

简介: R绘图 | 云雨图+双向条形图

bar+rain_cover

整个新系列。目前的几个系列, 「#R实战」  以「生信分析」为主, 「#跟着CNS学作图」「复现顶刊」Figure为主,而本系列 「#R绘图」 则是学习不在文章中但同样很好看的图,致力于给同学们在数据可视化中提供新的思路和方法。

本系列往期文章

  1. R绘图 | 气泡散点图+拟合曲线
  2. R绘图 | 对比条形图+连线
  3. R绘图 | 一幅小提琴图的美化之旅
  4. R绘图 | 山峦图(ggridges)
  5. R绘图 | 哑铃图+区域放大
  6. R绘图 | 描述性统计常用图(散点图+柱状图+饼图)
  7. R绘图 | 圆角堆叠柱状图(ggchicklet )
  8. R绘图 | 时间线热图
  9. R绘图 | 堆叠柱状图

本期图片

bar+plot

示例数据和代码领取

点赞在看 本文,分享至朋友圈集赞20个保留30分钟,截图发至微信mzbj0002领取。

「木舟笔记2022年度VIP可免费领取」

木舟笔记2022年度VIP企划

「权益:」

  1. 「2022」年度木舟笔记所有推文示例数据及代码(「在VIP群里实时更新」)。
    data+code

绘制

library(tidyverse)
library(grid)
library(colorspace)
library(cowplot)
library(MetBrewer)
library(patchwork)
# prep data for plots    ------------------------------------------
plot_prep <- read.csv('plot_prep.csv')
# color palette
colors <- MetBrewer::met.brewer("Moreau")
# bar plot   ------------------------------------------
bars <- plot_prep |> 
  group_by(classification, sense) |> 
  summarise(total = sum(ratio),
            n = n()) |> 
  ungroup() |> 
  group_by(classification) |> 
  mutate(perc = n / sum(n) * sign(total),
         classification = factor(classification,
                                 levels = c("Class C",
                                            "Class B",
                                            "Class A"))) |> 
  filter(sense != "none") |> 
  mutate(lab = mean(perc)) |> 
  ggplot(aes(classification, perc, fill = classification)) +
  geom_hline(yintercept = 0, linetype = 2) +
  geom_col(width = .5, aes(alpha = sense)) +
  geom_label(aes(x = classification, y = lab, 
                 label = classification),
             fill = "grey90", size = 12, ) +
  geom_text(aes(
    label = case_when(classification == "Class A" &
                        sense == "sight" ~ paste0(label_percent()(abs(perc)),
                                                  " rely more non sight"),
                      classification == "Class A" &
                        sense == "sound" ~ paste0(label_percent()(abs(perc)),
                                                  " rely more non hearing"),
                      TRUE ~ label_percent()(abs(perc))),
    y = perc + .05 * sign(perc),
    hjust = ifelse(sense == "sight", 0, 1)
  ),
  size = 7, lineheight = .25) +
  scale_fill_met_d(name = "Moreau") +
  scale_alpha_manual(values = c(1, .5)) +
  scale_y_continuous(labels = function(x) label_percent()(abs(x)),
                     limits = c(-.75, 1),
                     breaks = c(seq(from = -.5, to = 1, by = .5))) +
  coord_flip(clip = "off") +
  theme_void() +
  theme(plot.margin = margin(r = 20),
        legend.position = "none",
        text = element_text( size = 15),
        plot.title.position = "plot",
        plot.title = element_textbox(fill = colors[7], color = "white", hjust = .5,
                                     padding = margin(4,4,2,4), r = unit(2, "points"),
                                     margin = margin(b = 5))) +
  labs(x = "", y = "")
bars
# raincloud plot   ------------------------------------------
rain <- plot_prep |> 
  mutate(classification = factor(classification,
                                 levels = c("Class C",
                                            "Class B",
                                            "Class A"))) |> 
  ggplot(aes(classification, ratio)) +
  ggdist::stat_halfeye(
    aes(fill = classification),
    adjust = 1, 
    width = .6, 
    .width = 0, 
    justification = -.3, 
    point_colour = NA) + 
  gghalves::geom_half_boxplot(
    side = "l", outlier.color = NA, center = TRUE, errorbar.draw = FALSE,
    width = .5, nudge = .1, alpha = .25,
    aes(fill = classification,
        color = classification)
  ) +
  geom_point(
    aes(fill = classification,
        color = classification),
    shape = 21,
    alpha = .1,
    position = position_jitter(
      seed = 1, width = .075
    )
  ) +
  stat_summary(fun.data = function(x) data.frame(y = median(x),
                                                 label = paste0("n = ",
                                                                label_comma()(length(x)))), 
               geom = "text", aes(x = classification, y = -30, color = classification),
               size = unit(10, "points"),
               position = position_nudge(x = -.25))  +
  scale_color_met_d(name = "Moreau") +
  scale_fill_met_d(name = "Moreau") +
  scale_y_continuous(labels = c("+40 instances of hearing",
                                "+20",
                                "Neutral",
                                "+20 instances of sight"),
                     breaks =  c(-40, -20, 0, 20)) +
  coord_flip() +
  theme_minimal() +
  theme(legend.position = "none",
        text = element_text(size = 30),
        plot.title.position = "plot",
        panel.grid.minor.x = element_blank(),
        panel.grid.major.y = element_blank(),
        axis.text = element_text(lineheight = .25),
        plot.title = element_textbox(fill = colors[7], color = "white", hjust = .5,
                                     padding = margin(4,4,2,4), r = unit(2, "points"),
                                     margin = margin(b = 5))) +
  labs(x = "", y = "")
rain
# plot patchwork   ------------------------------------------
bars/rain
# save file  ------------------------------------------
ggsave(filename = "rain_barpolt.pdf",w = 22, h = 8)

result

参考

  • tidytuesday/final_plot.R at master · Pecners/tidytuesday (github.com)
相关文章
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
2097 1
R实战 | 对称云雨图 + 箱线图 + 配对散点 + 误差棒图 +均值连线
|
数据挖掘
跟着 Nature 学作图 | 相关性热图(显示相关性散点图)
跟着 Nature 学作图 | 相关性热图(显示相关性散点图)
1070 0
|
机器学习/深度学习 数据采集 搜索推荐
多模型DCA曲线:如何展现和解读乳腺癌风险评估模型的多样性和鲁棒性?
多模型DCA曲线:如何展现和解读乳腺癌风险评估模型的多样性和鲁棒性?
526 1
|
数据可视化 数据挖掘 数据处理
R绘图 | 浅谈散点图及其变体的作图逻辑
R绘图 | 浅谈散点图及其变体的作图逻辑
682 0
|
数据安全/隐私保护
jupyterlab远程服务器配置
jupyterlab远程服务器配置
437 0
|
机器学习/深度学习 搜索推荐 数据挖掘
深度学习之因果关系建模
基于深度学习的因果关系建模是一项旨在通过深度学习技术识别和理解数据之间因果关系的研究领域。因果关系建模不仅仅关注变量之间的相关性,还希望揭示导致某种结果的根本原因。
551 2
|
SQL 开发框架 JavaScript
Sentieon | 应用教程:唯一分子标识符(UMI)
**Sentieon®工具通过UMI处理NGS数据,减少PCR误差和提高变异检测准确性。流程包括:umi extract(提取UMI标签),bwa mem对齐,umi consensus(创建一致性分子),再次对齐并排序。umi extract根据读取结构提取条形码,umi consensus生成共识Fastq,最终比对产生用于变异调用的BAM文件。该流程适用于体细胞突变检测,推荐使用TNscope®。日志提供质量控制信息,如组大小直方图和双工统计。**
336 1
|
机器学习/深度学习 数据采集 自然语言处理
深入浅出:用Python实现简单文本分类器
【8月更文挑战第31天】本文旨在通过简明的Python代码示例,引导读者理解并实现一个简单的文本分类器。从数据预处理到模型训练,再到结果评估,我们将一步步构建起一个基于朴素贝叶斯算法的文本分类系统。无论你是编程新手还是机器学习初学者,这篇文章都将为你打开一扇通往文本分析世界的大门。
系统工程是一个跨学科的领域,它关注于如何设计、管理和优化复杂的系统。
系统工程是一个跨学科的领域,它关注于如何设计、管理和优化复杂的系统。