基本簡(jiǎn)介
云雨圖(Raincloud plots)其實(shí)是可以看成核密度估計(jì)曲線圖、箱形圖和抖動(dòng)散點(diǎn)圖的組合圖,清晰羹膳、完整、美觀地展示了所有數(shù)據(jù)信息根竿。本質(zhì)上是一個(gè)混合圖陵像,可同時(shí)將原始數(shù)據(jù)、數(shù)據(jù)分布和關(guān)鍵匯總統(tǒng)計(jì)表現(xiàn)出來(lái)寇壳,由對(duì)分的小提琴圖(Violin plot)醒颖、箱線圖(boxplot)和作為某種散點(diǎn)的原始數(shù)據(jù)組成。具體可以使用gglayer包的geom_flat_violin()函數(shù)繪制壳炎,由于該包貌似還沒(méi)有更新泞歉,因此使用網(wǎng)頁(yè)(https://gist.githubusercontent.com/benmarwick/2a1bb0133ff568cbe28d/raw/fb53bd97121f7f9ce947837ef1a4c65a73bffb3f/geom_flat_violin.R)的函數(shù)功能進(jìn)行繪制。
示例代碼
#清空數(shù)據(jù)
rm(list=ls())
#加載所需要的函數(shù)
source("E:/所有R語(yǔ)言/geom_flat_violin.R")
#或者直接在R中運(yùn)行此函數(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")