摘要:studytreatment group dose participant age sex score1 A placebo high p01 23 female 22 A placebo high p02 45 male 43 A placebo high
今天给大家介绍一个用于简化绘图代码的R包——tidyplots,相比较ggplot2,更加高级和实用。
我们首先看一下这个tidyplots包可以什么图吧,就是下面这些!常用的基本都能满足,优点在于绘图过程代码简单,更加方便快捷!
1.直接从cran进行安装即可
install.packages("tidyplots")1.tidyplots内置了非常多的数据,你可以用来进行练习
> studytreatment group dose participant age sex score1 A placebo high p01 23 female 22 A placebo high p02 45 male 43 A placebo high p03 32 female 54 A placebo high p04 37 male 45 A placebo high p05 24 female 66 B placebo low p06 23 female 97 B placebo low p07 45 male 88 B placebo low p08 32 female 129 B placebo low p09 37 male 1510 B placebo low p10 24 female 1611 C treatment high p01 23 female 3212 C treatment high p02 45 male 3513 C treatment high p03 32 female 2414 C treatment high p04 37 male 4515 C treatment high p05 24 female 5616 D treatment low p06 23 female 2317 D treatment low p07 45 male 2518 D treatment low p08 32 female 2119 D treatment low p09 37 male 2220 D treatment low p10 24 female 23> str(animals)Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 60 obs. of 14 variables:$ animal : chr "Honeybee" "Monarch Butterfly" "Dragonfly" "Firefly" ...$ size : num 1.5 10 10 2.5 17 1 4 0.8 7.5 9 ...$ size_unit : chr "cm" "cm" "cm" "cm" ...$ weight : num 1.2e-04 4.5e-04 2.0e-04 2.0e-05 3.0e-03 2.5e-06 5.0e-06 1.0e-05 2.0e-04 4.0e-04 ...$ weight_unit : chr "kg" "kg" "kg" "kg" ...$ speed : num 0.72 0.432 2.088 0.0432 0.144 ...$ speed_unit : chr "km/h" "km/h" "km/h" "km/h" ...$ habitat : chr "Gardens, meadows, hives" "Gardens, fields, forests" "Wetlands, near water" "Grasslands, forests, wetlands" ...$ activity : chr "Diurnal" "Diurnal" "Diurnal" "Nocturnal" ...$ family : chr "Insect" "Insect" "Insect" "Insect" ...$ color : chr "Yellow/Black" "Orange/Black" "Various" "Light Brown" ...$ number_of_legs : Factor w/ 4 levels "0","2","4","6": 4 4 4 4 4 4 4 4 4 4 ...$ body_temperature: chr "Cold-blooded" "Cold-blooded" "Cold-blooded" "Cold-blooded" ...$ diet : chr "Herbivore" "Herbivore" "Carnivore" "Carnivore" ...> str(energy)Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 242 obs. of 5 variables:$ year : num 2002 2002 2002 2002 2002 ...$ energy_source: Factor w/ 11 levels "Biomass","Fossil brown coal / lignite",..: 7 2 4 3 5 8 6 1 11 10 ...$ energy_type : Factor w/ 4 levels "Fossil","Nuclear",..: 2 1 1 1 1 3 4 4 4 4 ...$ power : num 23.5 20.3 28.3 20.3 5.3 ...$ power_unit : chr "GW" "GW" "GW" "GW" ...1.绘制一幅常见的柱形散点图,使用ggplot2的朋友可能知道,如果绘制这样的柱形+散点的图形,需要写很多代码,而使用tidyplots只需简单的几行
library(tidyplots)study %>% tidyplot(x = treatment, y = score, color = treatment) %>% # 选择映射数据add_mean_bar(alpha = 0.4) %>% # 添加平均数的柱状图add_sem_errorbar %>% # 添加标准误add_data_points_beeswarm # 添加数据点2.如果要绘制累积柱状图,也只需要两行代码
energy %>% tidyplot(x = year, y = power, color = energy_source) %>% # 选择映射的数据add_barstack_absolute # 添加累计柱状图的代码3.如果想按照某个分类进行分图,其实也比较简单
energy %>% dplyr::filter(year %in% c(2005, 2010, 2015, 2020)) %>% tidyplot(y = power, color = energy_source) %>% add_donut %>% split_plot(by = year) # 按照year进行分图energy %>% dplyr::filter(year %in% c(2005, 2010, 2015, 2020)) %>% tidyplot(y = power, color = energy_source) %>% add_donut(width = 2) %>% # 设置圈图中间的面积,数值越小,白圈越大split_plot(by = year,ncol = 3) # 设置行列的数量4.面积叠加图
energy_week %>% tidyplot(x = date, y = power, color = energy_source) %>% add_areastack_absolute5.想把数值加在柱子上也很容易
study %>% tidyplot(x = group, y = score, color = dose) %>% add_mean_bar(alpha = 0.4) %>% add_mean_dash %>% add_mean_value6.含有置信区间的图
time_course %>%tidyplot(x = day, y = score, color = treatment) %>%add_mean_line %>% # 平均值的线add_mean_dot %>% # 平均值的点add_sem_ribbon # 置信区间也可以一键绘制热图climate %>%tidyplot(x = month, y = year, color = max_temperature) %>%add_heatmap8.添加显著性,这个可能最常用了
study %>% tidyplot(x = treatment, y = score, color = treatment) %>% add_boxplot %>% add_test_pvalue(ref.group = 1)绘图流程下面是tidyplots的绘图流程,从tidyplot→add→remove→adjust→theme→split_plot→save_plot,熟悉这个流程,然后选择对应的函数即可
功能函数1.创建图形函数
tidyplot2.添加图形元素函数
a.数据点和数量
# 数据点add_data_points add_data_points_jitter add_data_points_beeswarm# 数值add_count_bar add_count_dash add_count_dot add_count_value add_count_line add_count_area# 数值总结add_sum_bar add_sum_dash add_sum_dot add_sum_value add_sum_line add_sum_area# 热图add_heatmap# 线或面积add_line add_areab.添加集中趋势
# 添加均值add_mean_bar add_mean_dash add_mean_dot add_mean_value add_mean_line add_mean_area# 添加中位数add_median_bar add_median_dash add_median_dot add_median_value add_median_line add_median_area# 拟合曲线add_curve_fitc.添加数值分布
# 直方图add_histogram# 箱线图add_boxplot# 小提琴图add_violin# 误差线add_sem_errorbar add_range_errorbar add_sd_errorbaradd_ci95_errorbar# 置信区间add_sem_ribbon add_range_ribbon add_sd_ribbon add_ci95_ribbond.比例
# 条形堆叠add_barstack_absolute add_barstack_relative# 面积堆叠add_areastack_absolute add_areastack_relative# 饼图或圆环图add_pie add_donute.统计测试
# 统计结果add_test_pvalue add_test_asterisksf.注释
# 标题或说明add_title add_caption# 数据标签add_data_labelsadd_data_labels_repel# 参考线add_reference_lines3.移除函数
用来移除绘图的元素
# 移除图例或标题remove_legend remove_legend_title# 删除绘图区域填充remove_padding# 删除标题或说明remove_title remove_caption# 删除 x 轴或其部分remove_x_axis remove_x_axis_line remove_x_axis_ticks remove_x_axis_labels remove_x_axis_title# 删除 y 轴或其部分remove_y_axisremove_y_axis_line remove_y_axis_ticks remove_y_axis_labels remove_y_axis_title4.调整函数
a. 用来调整绘图组件、属性和数据标签
# 调整颜色adjust_colors# 调整字体adjust_font# 调整图例adjust_legend_title adjust_legend_position# 调整标题和注释adjust_title adjust_x_axis_titleadjust_y_axis_title adjust_caption# 调整绘图大小adjust_size# 调整绘图区域填充adjust_padding# 调整x和y轴adjust_x_axis adjust_y_axisb. 坐标轴和颜色标签
# 重命名轴和颜色标签rename_x_axis_labelsrename_y_axis_labels rename_color_labels# 重排序轴和颜色标签reorder_x_axis_labels reorder_y_axis_labelsreorder_color_labels# 对轴或颜色标签进行排序sort_x_axis_labelssort_y_axis_labels sort_color_labels# 翻转轴或颜色标签reverse_x_axis_labels reverse_y_axis_labels reverse_color_labels5.主题调整
用于主题的调整
# 主题theme_tidyplot theme_ggplot2 theme_minimal_xy theme_minimal_x theme_minimal_y# 调节主题细节adjust_theme_details6.颜色主题
有多种配色方案可供选择
# 离散配色方案colors_discrete_friendly colors_discrete_seaside colors_discrete_apple colors_discrete_friendly_long colors_discrete_okabeito colors_discrete_ibm colors_discrete_metro colors_discrete_candy colors_discrete_alger colors_discrete_rainbow# 连续配色方案colors_continuous_viridis colors_continuous_magma colors_continuous_inferno colors_continuous_plasma colors_continuous_cividis colors_continuous_rocket colors_continuous_mako colors_continuous_turbo colors_continuous_bluepinkyellow# 不同的配色方案colors_diverging_blue2red colors_diverging_blue2brown colors_diverging_BuRd colors_diverging_BuYlRd colors_diverging_spectral colors_diverging_icefire# 新的配色方案new_color_scheme7.分割函数
将主图分成多个子图
# 将图形分割为多个子图split_plot8.输出
用于保存图片
# 在屏幕上查看图表view_plot# 保存图形文件save_plot9.一些小技巧
# 子集数据行all_rows filter_rowsmax_rows min_rows first_rows last_rows sample_rows# 添加ggplot2代码到tidyplotadd# 转换ggplot2到tidyplotas_tidyplot# 翻转 x 轴和 y 轴flip_plot# 格式化数字或 p 值format_number format_p_value10.内置数据集
# 动物数据animals# 气候数据climate# 恐龙数据dinosaurs# 分布数据distributions# 能源数据energy# 能源周数据energy_week# 欧盟国家数据eu_countries# RNA-Seq 表达数据gene_expression# 支出数据spendings# 研究数据study# 时间进程数据time_course参考链接:
[1] https://tidyplots.org/
[2] https://jbengler.github.io/tidyplots/reference/index.html
来源:Paper绘图