GGPLOT -批量创建饼图

简介: 本文分享了 数据预处理到 GGPLOT 批量创建饼图 的基本代码过程以供参考

统计目标:根据报告文件批量绘制每个RNA文库的 序列比对情况的饼图;
代码设计: 数据预处理 和 图样式处理 + 循环出图

1、数据集概况

文库比对报告文件中每行记录了文库的序列比对信息,每列为比对指标,每个饼图根据表中每行的信息生成。

2、转换数据格式

pacman::pload(RColorBrewer,ggforce,ggplot2,dplyr,tidyverse)
data.set <- read.table("*.tsv") #加载数据集
df <- data.set[1,] #提取数据集的一行进行脚本测试
df %>% data.frame() %>% t() %>% data.frame() %>% tibble::rownames_to_column(var = "group") %>% filter_all( any_vars(grepl("Reads", .)) ) %>% 
    dplyr::rename(labels_1 = 2) %>% filter(!grepl("Mapped.to.Genome", group)) %>% 
    mutate(perc = as.numeric(sub("%", "", labels_1))/100) %>%
    mutate(labels = scales::percent(perc),labels = paste( gsub("\\."," ",x = group) ,labels,sep = "-")) -> t
> t
                                              group labels_1  perc                                                 labels
1                    Reads.Mapped.to.Exonic.Regions   42.90% 0.429                   Reads Mapped to Exonic Regions-42.9%
2                  Reads.Mapped.to.Intronic.Regions   25.70% 0.257                 Reads Mapped to Intronic Regions-25.7%
3  Reads.Mapped.to.both.Exonic.and.Intronic.Regions    1.80% 0.018  Reads Mapped to both Exonic and Intronic Regions-1.8%
4                    Reads.Mapped.Antisense.to.Gene   12.50% 0.125                   Reads Mapped Antisense to Gene-12.5%
5                Reads.Mapped.to.Intergenic.Regions   16.70% 0.167               Reads Mapped to Intergenic Regions-16.7%
6 Reads.Mapped.to.Gene.but.Failed.to.Interpret.Type    0.40% 0.004 Reads Mapped to Gene but Failed to Interpret Type-0.4%

2、定义饼图分组变量绘图顺序

t$group <- factor(t$group,levels = t$group[6:1]) #取分组排列的倒序

3、计算绘图的极坐标数据

#计算文本标签的极坐标
t %>% mutate(
    end = 2 * pi * cumsum(perc)/sum(perc),
    start = lag(end, default = 0),
    middle = 0.5 * (start + end),
    hjust = ifelse(middle > pi, 1, 0),
    vjust = ifelse(middle < pi/2 | middle > 3 * pi/2, 0, 1)) ->t

4、准备不同样式的饼图代码

  • 样式一
ggplot(t) + 
    geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = 1,start = start, end = end, fill = group)) +
    geom_text(aes(x = 1.05 * sin(middle_n), y = 1.05 * cos(middle_n), 
                  label = stringr::str_wrap(labels, width = 40), #每行最大文本长度为40,超过文本长度进行折叠
                  hjust = hjust, vjust = vjust),
              size=4.5, fontface="italic",color="grey20") + #绘图标签文本样式

    coord_fixed(ratio = 1) + #固定绘图显示比例
    #调整比例防止标签截断
    scale_x_continuous(limits = c(-2.5, 2.5), name = "", breaks = NULL, labels = NULL) +
    scale_y_continuous(limits = c(-1.2, 1.2), name = "", breaks = NULL, labels = NULL) +

    scale_fill_manual(values = brewer.pal(6,"Dark2"), #填充色修改
                      labels = function(x)  str_replace(x, "(.{100})", "\\1\n") #图例文本折叠
                      ) +
    guides(fill=guide_legend(ncol=2,title.position = "top"))+ #图例标签折叠
    theme_void() +

    ###绘图注释
    labs(
        title =  df[1],#添加标题头
        caption = paste0("Reads.Mapped.to.Genome  ",df[2])#添加注释文本
         )+ 

    theme(
        legend.position = "bottom",
        legend.text = element_text(size=12),
        legend.title = element_text(size = 15),
        legend.title.align = 0,
        legend.margin= margin(t = 2, unit='cm'),
        text = element_text(size = 18, face = "bold"),
        plot.title = element_text(size = 23, face = "bold",hjust = 0.5,vjust = 5),
        plot.caption  = element_text(size = 22, face = "bold",hjust = 0.1,vjust = 30,colour = "#56b1cf"), #引用字样式
        plot.caption.position = "panel"
    )
  • 样式二 代码
    ggplot(t, aes(x = "", y = perc, fill = labels)) +
      geom_col() +
      geom_label(aes(label = labels_1),color=c("white","white","white","white","white","red"),size=10,position = position_stack(vjust = 0.5),show.legend = FALSE) +
      scale_fill_manual(values = viridis::viridis(6), #填充色修改
                        labels = function(x) stringr::str_wrap(x, width = 30) #图例文本折叠
      )+
      coord_polar(theta = "y")+
      theme_void() + 
      ###绘图注释
      labs(
          title = paste0("Reads.Mapped.to.Genome  ",df[2]), 
          caption = df[1] )+
      theme(text = element_text(size = 18, face = "bold"),
            plot.title = element_text(size = 22, face = "bold",vjust = -115,hjust = 0.1,color = "skyblue"),
            plot.caption = element_text(size = 22, face = "bold",hjust = 0.5,vjust = 115),
            plot.caption.position = "panel",
            legend.margin= margin(l = -1.5, unit='cm'), #图例距离绘图的距离
            legend.text = element_text( size = 11,margin = margin(l = 5, unit = "pt")), #图例文本
            legend.key.size = unit(30, "pt"), #图例大小
      )
    

    5、批量出图

    将上面步骤的处理和出图脚本封装到函数f,通过 apply遍历数据行,调用出图函数f绘图并保存
    f <- function(x) { ... ; ggsave(plot = . ,filename = paste0(file_name,".jpg"),dpi = 300)}
    apply(df, 1, FUN = function(x) f(x) )
    

Reference

How to wrap long axis tick labels into multiple lines in ggplot2 - Data Viz with Python and R (datavizpyr.com)

目录
相关文章
|
12月前
|
数据可视化 Python
【100天精通Python】Day62:Python可视化_Matplotlib绘图基础,绘制折线图、散点图、柱状图、直方图和饼图,以及自定义图标外观和功能,示例+代码
【100天精通Python】Day62:Python可视化_Matplotlib绘图基础,绘制折线图、散点图、柱状图、直方图和饼图,以及自定义图标外观和功能,示例+代码
184 0
|
24天前
R语言基于表格文件的数据绘制具有多个系列的柱状图与直方图
【9月更文挑战第9天】在R语言中,利用`ggplot2`包可绘制多系列柱状图与直方图。首先读取数据文件`data.csv`,加载`ggplot2`包后,使用`ggplot`函数指定轴与填充颜色,并通过`geom_bar`或`geom_histogram`绘图。参数如`stat`, `position`, `alpha`等可根据需要调整,实现不同系列的图表展示。
|
2月前
|
数据可视化 定位技术 Python
geopandas轻松绘制交互式在线地图
geopandas轻松绘制交互式在线地图
|
5月前
|
数据可视化 数据挖掘 定位技术
R语言读取Excel表格数据并绘制多系列柱状图、条形图
R语言读取Excel表格数据并绘制多系列柱状图、条形图
142 1
|
5月前
|
数据可视化
Excel实例:Excel图表可视化:条形图、折线图、散点图和步骤图
Excel实例:Excel图表可视化:条形图、折线图、散点图和步骤图
|
5月前
ggplot2如何在R语言中绘制表格
ggplot2如何在R语言中绘制表格
|
5月前
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程(上)
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程
349 0
|
5月前
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程(下)
【SPSS】基础图形的绘制(条形图、折线图、饼图、箱图)详细操作过程
321 0
|
5月前
|
存储 数据可视化 定位技术
Python中matplotlib为多个列表数据绘制小提琴图
Python中matplotlib为多个列表数据绘制小提琴图
|
11月前
|
缓存
EasyXnote5关于批量绘图
EasyXnote5关于批量绘图
79 0
EasyXnote5关于批量绘图