ggplot-RNA文库reads比对情况-饼图[pie chart]展示

开发者 2024-9-25 13:27:53 50 0 来自 中国
1.png 任务目标:批量绘制每个RNA文库reads比对情况的饼图;
任务流程: 数据预处理 和 图样式处理 + 循环出图
library(RColorBrewer);library(ggforce);set.seed(123);;library(ggplot2);library(dplyr);library(tidyverse)数据集概况


加载进来的的数据集是按行记录了每个文库的reads比对信息,其中比对类别存在列向量,绘图注意数据格式转换,绘制一个文库的饼图需要提取数据集的一行来进行处理。
数据处理脚本


  • 第一步,绘图数据格式转换
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                                                 labels1                    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%

  • 第二步,定义饼图分组变量绘图顺序
t$group <- factor(t$group,levels = t$group[6:1]) #取分组排列的倒序

  • 第三步,计算绘图的极坐标数据
#计算文本标签的极坐标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图样式脚本


  • 样式一
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"), #图例大小    )批量出图

将上面步骤的处理和出图脚本封装到函数f,使用 apply循环体遍历数据集的行,调用出图函数f出图
f <- function(x) { ... ; ggsave(plot = . ,filename = paste0(file_name,".jpg"),dpi = 300)}apply(df, 1, FUN = function(x) f(x) )
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:52, Processed in 0.209862 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表