R-ggplot2-瀑布图包安装使用太复杂,本文给出ggplot2版解决方案

目录

  • 0.问题导入
  • 1.ggplot2精仿版瀑布图预览
  • 2.示例数据生成
  • 3.绘制主图1
  • 4.绘制主图2
  • 5.主图1-2 图例提取
  • 6.绘制瀑布图
  • 7.本文总结
  • 8.本篇所用软件包(没有的,还需要install.packages('包名')进行安装哦~)
  • 9.致谢

0. 问题导入

前些天,我在平台看到一篇文章(R数据可视化13:瀑布图/突变图谱),深深被其中所介绍的瀑布图所吸引,觉得兼职太漂亮了吧!!!于是乎,我认真阅读了本文,试图根据文章介绍的步骤复现瀑布图(图1,源自R数据可视化13:瀑布图/突变图谱)。但是,在安装包的过程中就遇到了问题:不支持install.packages('')安装。嗯。。对于一个深度的ggplot2 使用者,个人觉得太麻烦啦,索性有了今天这篇利用ggplot2 来绘制瀑布图。

图1 SNP瀑布图

1. ggplot2 精仿版瀑布图预览

由于今天操作可能稍微比较多步骤,为减枯燥,先给大家展示下昨日精仿一天的结果图。但为了突出说明ggplot2绘制瀑布图的方法,本文采用较为简单的数据结构。


图2 ggplot2 精仿版瀑布图

2. 示例数据生成

2.1 主图1所需数据

samples = letters[1:26]

index_generate <- function(x){
  temp = round(runif(x, 1,26))
  temp = temp[order(temp)]
  temp = samples[temp]
  
  value = round(runif(x,-5,5),2)
  temp_df = data.frame(Sample = temp, value = value)
}

len_seq = round(runif(10,1,12))

df = lapply(len_seq,index_generate)

pl_df = 1
Type = LETTERS[1:10]

for(i in 1:length(df)){
  temp = df[[i]]
  temp$Type = Type[i]
  pl_df = rbind(pl_df, temp)
}
pl_df = pl_df[-1,]

pl_df$cuts = cut(pl_df$value,breaks = seq(-5,5,1))
pl_df$Type = factor(pl_df$Type,levels = rev(Type))
pl_df$Sample = factor(pl_df$Sample,levels = samples)

2.3 主图1数据结构预览

head(pl_df)
  Sample value Type    cuts
2      g -4.25    A (-5,-4]
3      h  4.53    A   (4,5]
4      j  0.52    A   (0,1]
5      j -3.31    A (-4,-3]
6      q -0.94    A  (-1,0]
7      r  4.61    A   (4,5]

2.3 主图2所需数据

df2 = 1
for(i in 1:length(samples)){
  temp_index = which(pl_df$Sample == samples[i])
  temp_value = pl_df$value[temp_index]
  
  temp_neg = length(which(temp_value<0))
  temp_pos = length(which(temp_value>=0))
  
  temp_df2 = data.frame(Sample = samples[i],Number_Neg = temp_neg,Number_Pos = temp_pos)
  df2 = rbind(df2, temp_df2)
}
df2 = df2[-1,]

re_h = which(df2$Number_Neg==0 & df2$Number_Pos == 0)
df2 = df2[-re_h,]
df2_m = melt(df2,'Sample')
colnames(df2_m) = c('Sample',"Group",'Number_of_Types')

2.4 主图2数据结构预览

head(df2_m)
  Sample      Group Number_of_Types
1      b Number_Neg               1
2      c Number_Neg               1
3      d Number_Neg               2
4      e Number_Neg               2
5      f Number_Neg               1
6      g Number_Neg               1

3. 绘制主图1

mycolors = colorRampPalette(brewer.pal(11,'Spectral'))(10)

p1 = ggplot()+
  geom_tile(data = pl_df, aes(x = Sample, y = Type, fill = cuts))+
  scale_fill_manual(values = mycolors)+
  theme(
    panel.background = element_rect(fill = 'transparent'),
    axis.text = element_text(color = 'black',size = 12, face = 'bold',hjust = 0.5),
    axis.title = element_text(color = 'black',size = 14,face = 'bold',hjust = 0.5),
    legend.position = 'none',
    legend.direction = 'horizontal'
  )+scale_y_discrete(position = 'right')

png('plot1.png',
    height = 25,
    width = 25,
    units = 'cm',
    res = 800)
print(p1)
dev.off()
图3 主图1

4. 绘制主图2

mycolor2 = colorRampPalette(brewer.pal(11,'Spectral'))(2)
p2 = ggplot()+
  geom_bar(data = df2_m,aes(x = Sample, y = Number_of_Types, fill = Group),
           stat = 'identity',position = 'stack')+
  scale_fill_manual(values = mycolor2)+
  theme(
    panel.background = element_rect(fill = 'transparent'),
    axis.text = element_text(color = 'black',size = 12, face = 'bold',hjust = 0.5),
    axis.text.x = element_blank(),
    axis.line.y = element_line(colour = 'black',size = 0.5,linetype = 'solid'),
    axis.title = element_text(color = 'black',size = 14,face = 'bold',hjust = 0.5),
    axis.title.x = element_blank(),
    legend.position = 'none',
    legend.direction = 'horizontal'
  )

png('plot2.png',
    height = 10,
    width = 25,
    units = 'cm',
    res = 800)
print(p2)
dev.off()
图4 主图2

5. 主图1-2 图例提取

p1_legend = get_legend(p1+theme(legend.position = 'bottom'))
p2_legend = get_legend(p2+theme(legend.position = 'bottom'))

p1_legend = as_ggplot(p1_legend)
p2_legend = as_ggplot(p2_legend)

6. 绘制瀑布图

p3 = plot_grid(p2,p1, align = 'v',
               axis = c('lr'),
               rel_widths = c(4,4),
               rel_heights = c(1,4),
               ncol = 1)

p4 = plot_grid(p3,p1_legend,p2_legend,align = 'v',
               axis = c('lr'),
               rel_heights = c(5,0.4,0.4),
               ncol = 1)

png('plot3.png',
    height = 26,
    width = 25,
    units = 'cm',
    res = 800)
print(p4)
dev.off()
图5 ggplot2 版瀑布图

7. 总结

瀑布图实则由统计图与热度图组成,而这两种图基于ggplot2 都可以实现,故昨日耗费一日完成基于ggplot2的瀑布图的绘制,即本篇核心问题为如何利用ggplot2绘制瀑布图?

8. 本篇所用软件包(没有的,还需要install.packages('包名')进行安装哦~)

library(ggplot2)
library(cowplot)
library(grid)
library(gridExtra)
library(RColorBrewer)
library(ggpubr)
library(reshape2)

9. 致谢

感谢R数据可视化13:瀑布图/突变图谱这篇文章的作者让我了解到了新的数据可视化方法~同时感谢大家的持续关注,小编会继续努力,持续更新下去的!

大家如果觉得有用,还麻烦大家关注点赞,也可以扩散到朋友圈,多谢大家啦~

大家如果在使用本文代码的过程有遇到问题的,可以留言评论,也可以私信我哈~~


小编联系方式

你可能感兴趣的:(R-ggplot2-瀑布图包安装使用太复杂,本文给出ggplot2版解决方案)