R语言绘图 --- 气泡图(Biorplot 开发日志 --- 4)

「写在前面」

在科研数据分析中我们会重复地绘制一些图形,如果代码管理不当经常就会忘记之前绘图的代码。于是我计划开发一个 R 包(Biorplot),用来管理自己 R 语言绘图的代码。本系列文章用于记录 Biorplot 包开发日志。


相关链接

相关代码和文档都存放在了 Biorplot GitHub 仓库:
https://github.com/zhenghu159/Biorplot

欢迎大家 Follow 我的 GitHub 账号:
https://github.com/zhenghu159

我会不定期更新生物信息学相关工具和学习资料。如果您有任何问题和建议,或者想贡献自己的代码,请在我的 GitHub 上留言。

介绍

气泡图,是一种以二维图形展示多维数据的可视化工具。它将数据点绘制在平面坐标系中,每个数据点用一个圆圈表示,圆圈的大小通常与某个维度的数值大小相关。通过气泡图,我们可以轻松地观察到数据点在各个维度上的分布情况,从而更好地理解数据的结构和规律。

Biorplot 中,我封装了 Bior_DotPlot() 函数来实现气泡图的绘制。

基础气泡图

绘制一个基础的气泡图如下:

绘图代码:

df <- data.frame(
  Sample = rep(paste('sample', 1:5, sep=''), 4),
  Gene = rep(paste('gene', 1:4, sep=''), 5),
  size = round(rnorm(20, mean = 10, sd = 5))
)
colour <- c("#1F77B4FF","#FF7F0EFF","#2CA02CFF","#D62728FF","#9467BDFF")
Bior_DotPlot(data = df, x = "Sample", y = "Gene", size = "size", color = "Gene",
             x.text.col = F, ggtheme = theme_bw()) + 
  theme(axis.text.x = element_text(angle = 0, hjust = 0.5))

表达量气泡图

绘制一个表达量气泡图如下:

绘图代码:

df <- data.frame(
  Sample = rep(paste('sample', 1:4, sep=''), each=4),
  Gene = rep(paste('gene', 1:4, sep=''), 4),
  Pct = c(80,10,10,10,10,80,10,10,10,10,80,10,10,10,10,80),
  Expression = c(3,0.5,0.1,0.3,0.3,3,0.2,0.6,0.1,0.7,3,0.1,0.5,0.2,0.1,3)
)

Bior_DotPlot(data = df, x = "Sample", y = "Gene", size="Pct", color = "Expression",
             x.text.col = F, ggtheme = theme_bw()) + 
  theme(axis.text.x = element_text(angle = 0, hjust = 0.5)) +
  scale_color_gradientn(colours = c("lightblue3", "lightblue", "white", "red", "red4"))

源码解析

Biorplot::Bior_DotPlot() 函数主要继承了 ggpubr::ggdotchart() 函数。

源码:

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#' Dot Plot
#' @description Create a dot plot.
#'
#' @importFrom ggpubr ggdotchart
#' @import ggplot2
#'
#' @inheritParams ggpubr::ggdotchart
#'
#' @return A ggplot object
#' @export
#'
#' @examples
#' # Examples 1
#' x <- rep(paste('sample', 1:5, sep=''), 4)
#' y <- rep(paste('gene', 1:4, sep=''), 5)
#' size <- round(rnorm(20, mean = 10, sd = 5))
#' colour <- c("#1F77B4FF","#FF7F0EFF","#2CA02CFF","#D62728FF","#9467BDFF")
#' p <- Bior_DotPlot(x = x, y = y, size = size, group.by = x, colour = colour, max_size=10)
#' p
Bior_DotPlot <- function(data, x, y, group = NULL,
                         combine = FALSE,
                         color = "black", palette = NULL,
                         shape = 19, size = NULL, dot.size = size,
                         sorting = c("ascending", "descending", "none"),
                         x.text.col = TRUE,
                         rotate = FALSE,
                         title = NULL, xlab = NULL, ylab = NULL,
                         facet.by = NULL, panel.labs = NULL, short.panel.labs = TRUE,
                         select = NULL, remove = NULL, order = NULL,
                         label = NULL, font.label = list(size = 11, color = "black"),
                         label.select = NULL, repel = FALSE, label.rectangle = FALSE,
                         position = "identity",
                         ggtheme = theme_pubr(),
                         ...)
{
  # Default options
  .opts <- list(data = data, x = x, y = y, group = group,
                combine = combine,
                color = color, palette = palette,
                shape = shape, size = size, dot.size = size,
                sorting = sorting,
                x.text.col = x.text.col,
                rotate = rotate,
                title = title, xlab = xlab, ylab = ylab,
                facet.by = facet.by, panel.labs = panel.labs, short.panel.labs = short.panel.labs,
                select = select, remove = remove, order = order,
                label = label, font.label = font.label,
                label.select = label.select, repel = repel, label.rectangle = label.rectangle,
                position = position,
                ggtheme = ggtheme,
                ...)

  p <- do.call(ggpubr::ggdotchart, .opts)

  return(p)
}

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

「结束」

注:本文为个人学习笔记,仅供大家参考学习,不得用于任何商业目的。如有侵权,请联系作者删除。

本文由mdnice多平台发布

相关推荐
高申航15 小时前
程序人生 - (002)
程序人生·职场和发展
大数据杂货铺16 小时前
使用 mongo2neo4j 和 SemSpect 通过各种方式进行图探索
程序人生
程序员曦曦1 天前
unittest自动化测试框架讲解以及实战
自动化测试·软件测试·功能测试·程序人生·职场和发展·log4j
鸽鸽程序猿1 天前
【C语言】分支(选择)和循环语句
c语言·开发语言·学习·程序人生·学习方法·visual studio
蓝牙语音芯片方案2 天前
杰理科技AD142A语音芯片,语音玩具方案—云信通讯
程序人生·开源软件·iot·杰理芯片·杰理代理·杰理云信
编程刘明2 天前
学会拥抱Python六剑客,提高编程效率
开发语言·数据库·python·程序人生·职场和发展·学习方法
禁默2 天前
c语言回顾-内存操作函数
c语言·开发语言·程序人生
潇潇说测试2 天前
软件测试面试题常见一百道【含答案】
自动化测试·软件测试·功能测试·程序人生·职场和发展
wl85113 天前
SAP HCM ABAP DEBUG小技巧
程序人生
CIb0la3 天前
局部的专业性过高有可能降低(而不是提升)整体的协调性
运维·程序人生·安全·生活