前几天看到一个刚出炉的神包,tidyplots ,https://jbengler.github.io/tidyplots/,https://www.biorxiv.org/content/ 10.1101/2024.11.08.621836v2 原文PDF(推荐看看,学习设计)地址。我第一眼就被PDF介绍吸引了,优美大气的配色+简约代码身段,这几天导致我魂牵梦绕,一直幻想,从她的姿色来看肯定是ggplot2定制版姐妹。昨夜深夜测评一番,心得在此分享给小伙伴们。
tidyplots 提供了一个用户友好的基于代码的界面,用于创建可定制且富有洞察力的图表。使研究人员能够利用自动化数据可视化流程,同时将所需的编程技能降至最低!
传统的数据分析工作流程,通常依赖于复制粘贴和手动电子表格操作,难以满足更高通量以及日益提高的可重复性和透明度标准的要求。最广泛绘图工具包括R包如tidyverse和 ggplot2,以及基于 Python 的库如 Pandas、NumPy、Matplotlib 和 Seaborn,但是吧,这些包每个都不一样的语法。tidyplots则是引入了一种直观且一致的全新界面,相当程度简化了代码量。tidyplots 的设计旨在:(i) 通过使用自然语言元素最大化人类代码的可读性,(ii) 在函数之间保持一致性,减少记住特殊情况的需要,以及 (iii) 具有模块化的特性,即可以将复杂任务分解为多个步骤。函数名称以动作动词开头,如 add、remove、adjust 或 save,清楚地表明函数执行的动作类型。对于添加汇总统计数据,统计实体优先于图形展示,鼓励在决定如何表示之前,先有意识地决定要绘制什么,例如先考虑均值,再考虑用柱状图表示。
从35个代码简化到10单词,直接 -70%代码量。
tidyplots 工作流程由一系列函数调用组成,这些调用在一个管道中连接(图 1a)。在使用 tidyplot()函数开始绘图后,有三个主要动词用于构建和修改图形,即 add、remove 和 adjust。用户可以选择不同的主题,将图分成多图布局,或将其保存到文件中(图 1a)。作为示例,我们将使用 tidyplots 包中的研究数据集,该数据集包括在两种不同治疗剂量下的治疗组和安慰剂组,以及衡量治疗成功的评分(图 1b)。在 tidyplot()函数中,我们定义了用于 x 轴、y 轴和颜色的变量。在下一行中,我们使用 add_mean_bar()函数添加每个组的平均值,并以条形表示。这体现了一种通用方案,其中函数名称以动作动词开始,如 add,后跟统计实体,如 mean,最后是实体的图形表示,如 bar。同样,我们可以使用 add_sem_errorbar() 将均值的标准误差添加为误差条,使用 add_data_points() 将原始数据值添加为点,使用 add_test_pvalue() 将统计测试添加为 p 值(图 1b)。tidyplots 提供了超过 50 个 add 函数,涵盖了原始数据值、汇总统计、离散、分布、比例、注释和统计比较的绘图(图 1c,d)。幸运的是,用户不必记住所有这些名称,因为代码编辑器中的自动完成功能通常会在输入几个字符后建议合适的函数。因此,输入“add_”会显示所有 add 函数的列表,输入“add_mean”会显示所有可用的均值图形表示,输入“add_bar”会显示所有可以表示为条形的统计实体的列表。下一步中,用户可以移除不需要的图表元素并调整图表的外观,包括颜色、字体、图例、标题、大小、填充和坐标轴(图 1e)
数据要求整洁数据,https://r4ds.hadley.nz/data-tidy,下列以自带数据study为例。
可以针对不同xy类型,进行特定绘图,代码简洁。tidyplots 带有许多默认的配色方案。其中许多是改编自viridisLite和RColorBrewer软件包。我觉得还行颜色。
#install.packages("tidyverse")
#install.packages("tidyplots")
library(tidyplots)
study <- study
#-------一、基本函数-------
#---添加元素add_
#条图
study %>%
tidyplot(x = treatment, y = score) %>%
add_data_points() %>%
add_mean_bar(alpha = 0.4)
#误差条
study %>%
tidyplot(x = group, y = score, color = dose) %>%
add_data_points() %>%
add_mean_dash() %>%
add_sem_errorbar()
#---消除元素remove_
study %>%
tidyplot(x = group, y = score, color = dose) %>%
add_data_points() %>%
add_mean_dash() %>%
add_sem_errorbar() %>%
remove_legend_title() %>%
remove_y_axis()
#--- 细节调整adjust_
#tidyplots 中的默认宽度为 50 毫米,高度为 50 毫米
study %>%
tidyplot(x = treatment, y = score, color = treatment) %>%
add_data_points() %>%
#add_data_points_beeswarm(shape = 1) 空心圆
add_mean_bar(alpha = 0.4) %>%
add_sem_errorbar() %>%
#1 adjust_调整是更改图、轴或图例的标题
adjust_title("This is my fantastic plot title") %>%
adjust_x_axis_title("Treatment group") %>%
adjust_y_axis_title("Disease score") %>%
adjust_legend_title("") %>%
adjust_caption("Here goes the caption")%>%
#2 adjust_调整绘图中的颜色
#adjust_colors(new_colors = c("#644296","#F08533","#3B78B0", "#D1352C"))
#内置颜色
adjust_colors(new_colors = colors_discrete_seaside) %>%
#3 rename_重命名
rename_x_axis_labels(new_names = c("A" = "This",
"B" = "is",
"C" = "totally",
"D" = "new")) %>%
#3 reorder_重排序
reorder_x_axis_labels("new", "totally") %>%
sort_x_axis_labels()
#--- 风格切换theme_
study %>%
tidyplot(x = group, y = score, color = group) %>%
add_data_points() %>%
add_sem_errorbar() %>%
add_mean_dash() %>%
theme_tidyplot() %>%
#ggplot2风格
theme_ggplot2() %>%
#横线风格theme_minimal_y()
#split_分面图
split_plot(by = dose)%>%
#save_输出文件
save_plot("tidyplot_1.pdf")
#------二、数据可视化-------
#2.1 连续xy
library(tidyplots)
animals %>%
tidyplot(x = weight, y = size) %>%
#添加细白色边框
add_data_points(white_border = TRUE)
#点透明add_data_points(alpha = 0.4)
#空心圆圈add_data_points(shape = 1)
#2.2 离散x连续y
study %>%
tidyplot(x = treatment, y = score) %>%
## add_data_points()
add_data_points_jitter()
##蜜蜂图 add_data_points_beeswarm()
#2.3 直方图
spendings %>%
tidyplot(x = category, y = amount, color = category) %>%
add_sum_bar() %>%
adjust_x_axis(rotate_labels = TRUE) %>%
remove_x_axis_labels() %>%
remove_x_axis_title() %>%
remove_x_axis_ticks()
#棒棒糖图
spendings %>%
tidyplot(x = category, y = amount, color = category) %>%
add_sum_bar(width = 0.03) %>%
add_sum_dot() %>%
add_sum_value(accuracy = 1) %>%
adjust_x_axis(rotate_labels = TRUE) %>%
remove_x_axis_labels() %>%
remove_x_axis_title() %>%
remove_x_axis_ticks()
#2.4 热图
gene_expression %>%
##dplyr::glimpse()
tidyplot(x = sample, y = external_gene_name, color = expression) %>%
##add_heatmap()%>%
#保留每行内差异的颜色范围
add_heatmap(scale = "row")%>%
adjust_size(height = 100) %>%
#将基因分类为“上调”或“下调”调节
sort_y_axis_labels(direction)
#2.5 趋势图
time_course %>%
tidyplot(x = day, y = score, color = treatment, dodge_width = 0) %>%
add_mean_line() %>%
add_mean_dot() %>%
#add_mean_area(alpha = 0.2)
#带errorbar
add_sem_errorbar(width = 2) %>%
#半透明ribbon
add_sem_ribbon()
#2.6 小提琴图/箱线图
study %>%
tidyplot(x = treatment, y = score, color = treatment) %>%
##箱线图 add_boxplot()
add_violin(draw_quantiles = 0.5) %>%
add_data_points_beeswarm()
#2.7 饼图
energy %>%
tidyplot(color = energy_type) %>%
add_pie()
#圆环饼图
energy %>%
tidyplot(y = power, color = energy_type) %>%
add_donut()
#add_barstack_absolute()
#% 堆叠图
energy %>%
tidyplot(x = year, y = power, color = energy_type) %>%
add_barstack_absolute()
# add_barstack_relative()
#add_areastack_relative()
#分年度圆环图
energy %>%
# downsample to 4 representative years
dplyr::filter(year %in% c(2005, 2010, 2015, 2020)) %>%
# start plotting
tidyplot(y = power, color = energy_type) %>%
add_donut() %>%
adjust_colors(new_colors = c("Fossil" = "grey",
"Nuclear" = "#F6C54D",
"Renewable" = "#4FAE62",
"Other" = "#C02D45")) %>%
split_plot(by = year)
#----三、带p值统计图-----
study %>%
tidyplot(x = dose, y = score, color = group) %>%
add_mean_dash() %>%
add_sem_errorbar() %>%
add_data_points() %>%
#add_test_pvalue()提供计算t检验的p值
#add_test_pvalue(hide.ns = TRUE) 可以隐藏不显著p值!
## add_test_asterisks() %>%
#此处非正态修改为wilcoxon多重校正p
add_test_pvalue(method = "wilcoxon", p.adjust.method = "BH")
#隐藏不显著p 隐藏图形信息
gene_expression %>%
# filter to one gene
dplyr::filter(external_gene_name == "Apol6") %>%
# start plotting
tidyplot(x = condition, y = expression, color = sample_type) %>%
add_mean_dash() %>%
add_sem_errorbar() %>%
add_data_points() %>%
add_test_pvalue(hide.ns = TRUE, hide_info = TRUE) %>%
#添加额外title caption等
add_title("Interesting study") %>%
add_caption("Here is some more detail how the study was performed") %>%
#添加参考线
add_reference_lines(x = 1, y = c(3, 6))
#---添加参考线
animals %>%
tidyplot(x = weight, y = speed) %>%
#添加参考线
add_reference_lines(x = 4000, y = c(100, 200)) %>%
add_data_points() %>%
add_data_labels_repel(data = max_rows(weight, 3), animal) %>%
add_data_labels_repel(data = max_rows(speed, 3), animal)
#-----四、高级统计图-------
#4.1 散点子集图
animals %>%
tidyplot(x = weight, y = size) %>%
add_data_points() %>%
add_data_points(data = filter_rows(size > 300), color = "red")%>%
#显示X最高的三种动物的名称
add_data_labels_repel(data = max_rows(weight, n = 3), label = animal, color = "black") %>%
#加个圈
add_data_points(data = max_rows(weight, n = 3), color = "red", shape = 1, size = 3)
#4.2 中间过程PDF
study %>%
tidyplot(x = treatment, y = score, color = treatment) %>%
add_mean_dash() %>%
save_plot(filename = "stage_1.pdf") %>%
add_sem_errorbar() %>%
save_plot(filename = "stage_2.pdf") %>%
add_data_points_beeswarm() %>%
save_plot(filename = "stage_3.pdf")
#4.3 公式
study %>%
tidyplot(x = treatment, y = score, color = treatment) %>%
add_data_points() %>%
add_mean_bar(alpha = 0.4) %>%
add_sem_errorbar() %>%
#下标
adjust_title("$H[2]*O$") %>%
#上标;所有绘图表达式都需要以$字符开头和结尾
adjust_x_axis_title("$E==m*c^{2}$") %>%
adjust_y_axis_title("$TNF*alpha~level$")
“
tidyplots 始终使用管道%>%来添加绘图组件,而 ggplot 使用+,前者只能说是后者小小妹形态(稚嫩)。这里要赞美下,作者PDF中流程图1相当棒,图配色逻辑100分! 2.测试完没有初见的心动(还是被配色骗了!),可能见过太多医学图形太复杂而tidyplots无法胜任。当然,其中两两比较误差条图自动比较p值是亮点。
3.tidyplots适合R绘图新手,代码超级简约且人性化。
本公众号建立了学术交流群(群6),仅供SCI学术交流,人数有限需要实名制。入群请加笔者微信 popnie,请备注说明(不写清楚不加):姓名-学校(单位)-专业