26  云雨图

26.1 什么是云雨图?

云雨图,顾名思义,由2部分组成,上方的半个小提琴图形似云朵,下方的点图形似雨滴。

上方的半个小提琴图,类似于核密度曲线,其曲线下的面积是1。

下方的点图,其实是频率分布直方图的点状抽象。

26.2 绘图前的数据准备

demo数据可以在https://www.bioladder.cn/shiny/zyp/bioladder2/demoData/rainCloud/demo.txt下载。

包含2个维度的数据,通常每一列是个样本,每一行是个基因

26.3 R语言怎么画云雨图

# 加载R包,没有安装请先安装  install.packages("包名") 
library(ggplot2)
library(reshape2)
library(RColorBrewer)
library(grid)

# 读取云雨图数据文件
df = read.delim("https://www.bioladder.cn/shiny/zyp/bioladder2/demoData/rainCloud/demo.txt",# 这里读取了网络上的demo数据,将此处换成你自己电脑里的文件
                header = T       # 指定第一行是列名
)
# 把数据转换成ggplot常用的类型(长数据)
df = melt(df)                    # melt出自reshape2包
head(df)                         # 查看转换完成的数据的前几行
# 绘图
ggplot(df,aes(x=variable,   
              y=value,
              fill=variable,     # fill填充颜色,根据变量名赋值
              colour=variable))+ # colour图形边界颜色,根据变量名赋值
  geom_flat_violin(position = position_nudge(x=.1))+ # 见下段代码
  geom_dotplot(binaxis = "y",
               binwidth = 0.1,   # 雨滴间隙
               stackdir = "down",
               dotsize = 0.6)+   # 雨滴大小
  coord_flip()+                  # 翻转坐标轴
  theme_classic()                # ggplot2主题

26.3.1 geom_flat_violin函数代码

#<<<<<<<<<<<<<<<<<<<<<<<<
# 由于ggplot2并没有提供半小提琴图,所以经由geom_violin的源码修改成geom_flat_violin函数
"%||%" <- function(a, b) {
  if (!is.null(a)) a else b
}

color<-brewer.pal(7,"Set2")[c(1,2,4,5)]


geom_flat_violin <- function(mapping = NULL, data = NULL, stat = "ydensity",
                             position = "dodge", trim = TRUE, scale = "area",
                             show.legend = NA, inherit.aes = TRUE, ...) {
  layer(
    data = data,
    mapping = mapping,
    stat = stat,
    geom = GeomFlatViolin,
    position = position,
    show.legend = show.legend,
    inherit.aes = inherit.aes,
    params = list(
      trim = trim,
      scale = scale,
      ...
    )
  )
}


GeomFlatViolin <-
  ggproto("GeomFlatViolin", Geom,
          setup_data = function(data, params) {
            data$width <- data$width %||%
              params$width %||% (resolution(data$x, FALSE) * 0.9)

            data %>%
              group_by(group) %>%
              mutate(ymin = min(y),
                     ymax = max(y),
                     xmin = x,
                     xmax = x + width / 2)

          },

          draw_group = function(data, panel_scales, coord) {
            data <- transform(data, xminv = x,
                              xmaxv = x + violinwidth * (xmax - x)) 

            newdata <- rbind(plyr::arrange(transform(data, x = xmaxv), -y),plyr::arrange(transform(data, x = xminv), y))
            newdata_Polygon <- rbind(newdata, newdata[1,])
            newdata_Polygon$colour<-NA

            newdata_Path <- plyr::arrange(transform(data, x = xmaxv), -y)

            ggplot2:::ggname("geom_flat_violin", grobTree(
              GeomPolygon$draw_panel(newdata_Polygon, panel_scales, coord),
              GeomPath$draw_panel(newdata_Path, panel_scales, coord))
            )
          },

          draw_key = draw_key_polygon,

          default_aes = aes(weight = 1, colour = "grey20", fill = "white", size = 0.5,
                            alpha = NA, linetype = "solid"),

          required_aes = c("x", "y")
  )


findParams <- function(mu, sigma, skew, kurt) {
  value <- .C("JohnsonMomentFitR", as.double(mu), as.double(sigma),
              as.double(skew), as.double(kurt - 3), gamma = double(1),
              delta = double(1), xi = double(1), lambda = double(1),
              type = integer(1), PACKAGE = "SuppDists")

  list(gamma = value$gamma, delta = value$delta,
       xi = value$xi, lambda = value$lambda,
       type = c("SN", "SL", "SU", "SB")[value$type])
}
#>>>>>>>>>>>>>>>>>>>>>>>>

26.4 BioLadder生信云平台在线绘制云雨图

不想写代码?可以用BioLadder生信云平台在线绘制云雨图。

网址:

云雨图-BioLadder生物信息在线分析可视化云平台​www.bioladder.cn/web/#/chart/47