【R語言】--- 云雨圖

基本簡介

云雨圖(Raincloud plots)其實是可以看成核密度估計曲線圖、箱形圖和抖動散點圖的組合圖,清晰、完整、美觀地展示了所有數(shù)據(jù)信息。本質(zhì)上是一個混合圖,可同時將原始數(shù)據(jù)、數(shù)據(jù)分布和關(guān)鍵匯總統(tǒng)計表現(xiàn)出來,由對分的小提琴圖(Violin plot)、箱線圖(boxplot)和作為某種散點的原始數(shù)據(jù)組成。具體可以使用gglayer包的geom_flat_violin()函數(shù)繪制,由于該包貌似還沒有更新,因此使用網(wǎng)頁(https://gist.githubusercontent.com/benmarwick/2a1bb0133ff568cbe28d/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R)的函數(shù)功能進(jìn)行繪制。

示例代碼

#清空數(shù)據(jù)
rm(list=ls())
#加載所需要的函數(shù)
source("E:/所有R語言/geom_flat_violin.R")
#或者直接在R中運行此函數(shù)
'
# somewhat hackish solution to:
# https://twitter.com/EamonCaddigan/status/646759751242620928
# based mostly on copy/pasting from ggplot2 geom_violin source:
# https://github.com/hadley/ggplot2/blob/master/R/geom-violin.r
library(ggplot2)
library(dplyr)

"%||%" <- function(a, b) {
  if (!is.null(a)) a else b
}

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,
      ...
    )
  )
}

#' @rdname ggplot2-ggproto
#' @format NULL
#' @usage NULL
#' @export
GeomFlatViolin <-
  ggproto("GeomFlatViolin", Geom,
          setup_data = function(data, params) {
            data$width <- data$width %||%
              params$width %||% (resolution(data$x, FALSE) * 0.9)
            
            # ymin, ymax, xmin, and xmax define the bounding rectangle for each group
            data %>%
              group_by(group) %>%
              mutate(ymin = min(y),
                     ymax = max(y),
                     xmin = x,
                     xmax = x + width / 2)
            
          },
          
          draw_group = function(data, panel_scales, coord) {
            # Find the points for the line to go all the way around
            data <- transform(data, xminv = x,
                              xmaxv = x + violinwidth * (xmax - x))
            
            # Make sure it's sorted properly to draw the outline
            newdata <- rbind(plyr::arrange(transform(data, x = xminv), y),
                             plyr::arrange(transform(data, x = xmaxv), -y))
            
            # Close the polygon: set first and last point the same
            # Needed for coord_polar and such
            newdata <- rbind(newdata, newdata[1,])
            
            ggplot2:::ggname("geom_flat_violin", GeomPolygon$draw_panel(newdata, 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")
  )
'

使用iris數(shù)據(jù)集

iris
#作圖
ggplot(iris, aes(x=Species, y=Sepal.Width)) +
  geom_flat_violin(aes(fill=Species),position=position_nudge(x=.25),color="black") +
  geom_jitter(aes(color=Species), width=0.1) +
  geom_boxplot(width=.1,position=position_nudge(x=0.25),fill="white",size=0.5) +
  theme_bw()
#或者x和y轉(zhuǎn)置
ggplot(iris, aes(x=Species, y=Sepal.Width)) +
  geom_flat_violin(aes(fill=Species),position=position_nudge(x=.25),color="black") +
  geom_jitter(aes(color=Species), width=0.1) +
  geom_boxplot(width=.1,position=position_nudge(x=0.25),fill="white",size=0.5) +
  coord_flip() +
  theme_bw()
#調(diào)整細(xì)節(jié)
a<- ggplot(iris, aes(x=Species, y=Sepal.Width)) +
  geom_flat_violin(aes(fill=Species),position=position_nudge(x=.25),color="black") +
  geom_jitter(aes(color=Species), width=0.1) +
  geom_boxplot(width=.1,position=position_nudge(x=0.25),fill="white",size=0.5) +
  theme_few()+
  ylab("Sepal width")+xlab("Species")+
  theme(legend.text=element_text(size=12))+
  theme(title=element_text(size=14))+
  theme(axis.text.x = element_text(size = 13, color = "black"))+
  theme(axis.text.y = element_text(size = 13, color = "black"))+
  theme(legend.position="none")+
  theme(axis.ticks.length=unit(0.2,"cm"))


b<- ggplot(iris, aes(x=Species, y=Sepal.Width)) +
  geom_flat_violin(aes(fill=Species),position=position_nudge(x=.25),color="black") +
  geom_jitter(aes(color=Species), width=0.1) +
  geom_boxplot(width=.1,position=position_nudge(x=0.25),fill="white",size=0.5) +
  coord_flip() +
  theme_few()+
  ylab("Sepal width")+xlab("Species")+
  theme(legend.text=element_text(size=12))+
  theme(title=element_text(size=14))+
  theme(axis.text.x = element_text(size = 13, color = "black"))+
  theme(axis.text.y = element_text(size = 13, color = "black"))+
  theme(legend.position="none")+
  theme(axis.ticks.length=unit(0.2,"cm"))
#組合圖
cowplot::plot_grid(a,b,
                   align="vh")

參考文獻(xiàn)

[1] https://wellcomeopenresearch.org/articles/4-63/v2#ref-9

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容