R语言绘图 --- 折线图(Biorplot 开发日志 --- 1)

「写在前面」

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


相关链接

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

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

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

介绍

折线图,是一种通过线段连接数据点的方式来展示数据变化趋势的图表。每个数据点表示一个特定的时间点或数值,线段则反映了数据点之间的关系。折线图通常用于展示时间序列数据、趋势分析、周期性变化等。

Biorplot 中,我封装了 Bior_LinePlot() 函数来实现折线图的绘制。

基础折线图

绘制一个基础的分类折线图如下:

绘图代码:

data <- data.frame('x' = c(1:20), 'y' = rnorm(20), 'Type' = rep(c('A','B'), 10))
palette <- c("#f89588","#63b2ee")
Bior_LinePlot(data, x = "x", y = "y", color = "Type", title = "Test Bior_LinePlot",
              palette = palette, plot_type = "l", size = 2, text.size = 30,
              ggtheme = theme_minimal()) +
  font("title", size = 35)

相关系数折线图

绘制一个带有相关系数 R 和 P-value 的折线图如下:

绘图代码:

data <- data.frame('x' = c(1:10), 'y' = c(1,1.5,1.8,2.3,3.3,5.3,7.5,8,9,10))

Bior_LinePlot(data, x = "x", y = "y",
              color = "firebrick3", plot_type = "l", size = 2, 
              cor.test = TRUE, cor.label.x=1, cor.label.y=9, R.digits = 2, P.digits = 2,
              cor.label.size = 10,
              text.size = 30, ggtheme = theme_classic()) +
  geom_point(color="black", fill="firebrick3", shape=21, size=4, stroke=1) +
  font("title", size = 30)

源码解析

Biorplot::Bior_LinePlot() 函数主要继承了 ggpubr::ggline()stats::cor.test() 函数,并对默认参数进行了自定义设置。

新增了标题居中标题大小,相关参数:

  • title.hjust (defaut: title.hjust = 0.5); title hjust value
  • text.size (defaut: text.size = 20); text size value

新增了通过 stats::cor.test() 计算相关系数 R 和 P-value 的功能,并通过 ggplot2::geom_text() 设置相关系数文本显示的位置,相关参数:

  • cor.test (defaut: cor.test = FALSE); whether to use cor.test to calculate correlations
  • R.digits (defaut: R.digits = 2); digits for R
  • P.digits (defaut: P.digits = 2); digits for P
  • cor.label.x (defaut: cor.label.x = 1); cor.label x position
  • cor.label.y (defaut: cor.label.y = 1); cor.label y position
  • cor.label.size (defaut: cor.label.size=10); cor.label size

源码:

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

#' Line plot
#' @description Create a line plot.
#'
#' @import ggplot2
#' @importFrom ggpubr ggline theme_pubr
#'
#' @inheritParams ggpubr::ggline
#' @inheritParams stats::cor.test
#' @param title.hjust (defaut: title.hjust = 0.5); title hjust value
#' @param text.size (defaut: text.size = 20); text size value
#' @param cor.test (defaut: cor.test = FALSE); whether to use cor.test to calculate correlations
#' @param R.digits (defaut: R.digits = 2); digits for R
#' @param P.digits (defaut: P.digits = 2); digits for P
#' @param cor.label.x (defaut: cor.label.x = 1); cor.label x position
#' @param cor.label.y (defaut: cor.label.y = 1); cor.label y position
#' @param cor.label.size (defaut: cor.label.size=10); cor.label size
#'
#' @return A ggplot object
#' @export
#'
#' @seealso \code{\link{ggline}}
#' @examples
#' # Examples 1
#' data <- data.frame('x' = c(1:20), 'y' = rnorm(20), 'Type' = rep(c('A','B'), 10))
#' palette <- c("#f89588","#63b2ee")
#' Bior_LinePlot(data, x = "x", y = "y", color = "Type", title = "Test Bior_LinePlot",
#'               palette = palette, plot_type = "l", size = 2, text.size = 30,
#'               ggtheme = theme_minimal()) +
#'   font("title", size = 35)
#'
#'
#' # Examples 2
#' data <- data.frame('x' = c(1:10), 'y' = c(1,1.5,1.8,2.3,3.3,5.3,7.5,8,9,10))
#' Bior_LinePlot(data, x = "x", y = "y",
#'               color = "firebrick3", plot_type = "l", size = 2,
#'               cor.test = TRUE, cor.label.x=1, cor.label.y=9, R.digits = 2, P.digits = 2,
#'               cor.label.size = 10,
#'               text.size = 30, ggtheme = theme_classic()) +
#'   geom_point(color="black", fill="firebrick3", shape=21, size=4, stroke=1) +
#'   font("title", size = 30)
#'
#'
Bior_LinePlot <- function(
    # ggpubr::ggline Arguments
    data, x, y, group = 1, numeric.x.axis = FALSE, combine = FALSE, merge = FALSE,
    color = "black", palette = NULL, linetype = "solid", plot_type = c("b", "l", "p"),
    size = 0.5, shape = 19, stroke = NULL, point.size = size, point.color = color,
    title = NULL, xlab = NULL, ylab = NULL, facet.by = NULL, panel.labs = NULL,
    short.panel.labs = TRUE, select = NULL, remove = NULL, order = NULL, add = "none",
    add.params = list(), error.plot = "errorbar", label = NULL, font.label = list(size = 11, color = "black"),
    label.select = NULL, repel = FALSE, label.rectangle = FALSE, show.line.label = FALSE,
    position = "identity", ggtheme = theme_pubr(),
    # add new Arguments
    title.hjust = 0.5, text.size = 20,
    # stats::cor.test Arguments
    alternative = "two.sided", method = "pearson", exact = NULL, conf.level = 0.95,
    continuity = FALSE,
    # add new Arguments
    cor.test = FALSE, R.digits = 2, P.digits = 2, cor.label.x = 1, cor.label.y = 1,
    cor.label.size=10,
    ...)
{

  ggline.Arguments <- list(
    data = data, x = x, y = y, group = group, numeric.x.axis = numeric.x.axis,
    combine = combine, merge = merge, color = color, palette = palette, linetype = linetype,
    plot_type = plot_type, size = size,  shape = shape, stroke = stroke,
    point.size = point.size, point.color = point.color, 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, add = add, add.params = add.params,
    error.plot = error.plot, label = label, font.label = font.label, label.select = label.select,
    repel = repel, label.rectangle = label.rectangle, show.line.label = show.line.label,
    position = position, ggtheme = ggtheme, ...)

  cor.test.Arguments <- list(
    alternative = alternative, method = method, exact = exact, conf.level = conf.level,
    continuity = continuity, ...
  )

  p <- do.call(ggpubr::ggline, ggline.Arguments) +
    theme(plot.title = element_text(hjust = title.hjust),
          text = element_text(size = text.size))

  if (cor.test == TRUE) {
    cor <- do.call(stats::cor.test, c(data[x], data[y], cor.test.Arguments))
    R <- round(cor$estimate, R.digits)
    P_value <- format(cor$p.value, digits = P.digits)
    p <- p +
      geom_text(x = cor.label.x, y = cor.label.y, label = paste('R =',R,'\nP =',P_value,sep=' '),
                hjust=0, color="black", size=cor.label.size)
  }

  return(p)
}

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

「结束」

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

相关推荐
大模型八哥2 小时前
大模型扫盲系列——大模型实用技术介绍(上)
人工智能·程序人生·ai·大模型·llm·llama·ai大模型
HHHTTY-2 天前
开启AI新篇章:探索GPT-4与大模型!订阅方案!简单支付!
人工智能·经验分享·python·深度学习·程序人生·机器学习·课程设计
龙之叶3 天前
文字的力量
程序人生
影雀3 天前
爬虫设计思考之一
程序人生
mysqloffice4 天前
[大象快讯]:PostgreSQL 17 重磅发布!
程序人生
哲伦贼稳妥4 天前
面试经验分享
经验分享·程序人生·其他·职场和发展·电脑
web_learning_3214 天前
python开源代码自学问题解决(requests+openpyxl+pymysql)
开发语言·python·程序人生·学习方法
独泪了无痕5 天前
帝都程序猿十二时辰
程序人生
微刻时光5 天前
影刀RPA实战:Excel拆分与合并工作表
笔记·程序人生·自动化·excel·rpa·影刀
Y_3_75 天前
RabbitMQ 概述及安装 : 程序之间沟通的桥梁
开发语言·分布式·后端·程序人生·中间件·rabbitmq·学习方法