今天小编再给大家介绍一个在统计学中常用的图表-云雨图(Raincloud plots)。雨云图本质上是一个混合图,可同时将原始数据、数据分布和关键汇总统计数据可视化表现出来,由对分的小提琴图(Violin plot)、箱线图(boxplot) 和作为某种散点的原始数据组成。接下来,小编就介绍几种绘制该类型图表的绘制方法,详细内容如下:
R-ggdist包可以很好的绘制云雨图(Raincloud plots),主要使用其stat_halfeye() 和ggplot2的geom_boxplot() 和 geom_jitter() 图层,具体如下:
plot01 <- ggplot(iris, aes(Species, Sepal.Width)) +
ggdist::stat_halfeye(aes(color=Species,fill=Species),adjust = .5, width = .7, .width = 0, justification = -.2, point_colour = NA) +
geom_boxplot(aes(color=Species),width = .2, outlier.shape = NA) +
geom_jitter(aes(color=Species),width = .05, alpha = .3) +
ggsci::scale_color_nejm()+
ggsci::scale_fill_nejm() +
coord_flip()+
labs(
title = "Example of <span style='color:#D20F26'>ggdist::stat_halfeye function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>stat_halfeye()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
legend.position = "none")
Example01 Of ggdist Raincloud plots made
此外,还可以将geom_jitter()替换成ggdist::stat_dots() 绘制另一种样式的云雨图,如下:
plot02 <- ggplot(iris, aes(Species, Sepal.Width)) +
ggdist::stat_halfeye(aes(color=Species,fill=Species),adjust = .5, width = .7, .width = 0, justification = -.2, point_colour = NA) +
geom_boxplot(aes(color=Species),width = .2, outlier.shape = NA) +
ggdist::stat_dots(aes(color=Species,fill=Species),side = "left", dotsize = .5, justification = 1.1, binwidth = .1)+
ggsci::scale_color_nejm()+
ggsci::scale_fill_nejm() +
labs(
title = "Example of <span style='color:#D20F26'>ggdist::stat_halfeye function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>stat_halfeye()+stat_dots()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
legend.position = "none")
Example02 Of ggdist Raincloud plots made
更多关于 R-ggdist包的介绍,可参考:R-ggdist包介绍[1]
由于上述绘制的结果不同类型图表较为集中,这里可以使用R-gghalves包对其布局进行优化,如下:
plot03 <- ggplot(iris, aes(Species, Sepal.Width)) +
ggdist::stat_halfeye(aes(color=Species,fill=Species),adjust = .5, width = .7, .width = 0, justification = -.3, point_colour = NA) +
geom_boxplot(aes(color=Species),width = .1, outlier.shape = NA) +
gghalves::geom_half_point(aes(color=Species),side = "l", range_scale = .4, alpha = .5) +
ggsci::scale_color_nejm()+
ggsci::scale_fill_nejm() +
coord_flip()+
labs(
title = "Example of <span style='color:#D20F26'>gghalves::geom_half_point function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_half_point()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
legend.position = "none")
Example01 Of ggdist+ gghalves Raincloud plots made
当然,你也可以将更改点的形状,如下:
plot04 <- ggplot(iris, aes(Species, Sepal.Width)) +
ggdist::stat_halfeye(aes(color=Species,fill=Species),adjust = .5, width = .5, .width = 0, justification = -.3, point_colour = NA) +
geom_boxplot(aes(color=Species),width = .1, outlier.shape = NA) +
gghalves::geom_half_point(aes(color=Species),side = "l", range_scale = 0, shape = 95, size = 15, alpha = .3) +
ggsci::scale_color_nejm()+
ggsci::scale_fill_nejm() +
labs(
title = "Example of <span style='color:#D20F26'>gghalves::geom_half_point function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_half_point()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
legend.position = "none")
Example02 Of ggdist+ gghalves Raincloud plots made
更多关于gghalves包的介绍可参考:gghalves包介绍[2]
这里小编再介绍一个专门绘制云雨图(Raincloud plots) 的第三方包-R-raincloudplots 。该包和上述上述介绍的方法对比:
下面就通过几个具体的例子进行展示:
「样例一」:
library(raincloudplots)
df_1x1 <- data_1x1(
array_1 = iris$Sepal.Length[1:50],
array_2 = iris$Sepal.Length[51:100],
jit_distance = .09,
jit_seed = 321)
mycolor <-pal_jco()(2)
raincloud_1_h <- raincloud_1x1(
data = df_1x1,
colors = mycolor,
fills = mycolor,
size = 1,
alpha = .8,
ort = 'h') +
scale_x_continuous(breaks=c(1,2), labels=c("Group1", "Group2"), limits=c(0, 3)) +
xlab("Groups") +
ylab("Score") +
labs(
title = "Example of <span style='color:#D20F26'>raincloudplots::raincloud_1x1 function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>raincloud_1x1()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
legend.position = "none")
Example01 Of raincloudplots::raincloud_1x1()
「样例二」:
raincloud_2 <- raincloud_1x1_repmes(
data = df_1x1,
colors = mycolor,
fills = mycolor,
line_color = 'gray',
line_alpha = .6,
size = 1,
alpha = .8,
align_clouds = FALSE) +
scale_x_continuous(breaks=c(1,2), labels=c("Pre", "Post"), limits=c(0, 3)) +
xlab("Time") +
ylab("Score") +
labs(
title = "Example of <span style='color:#D20F26'>raincloudplots::raincloud_1x1 function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>raincloud_1x1()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
plot.caption = element_markdown(face = 'bold',size = 12),
legend.position = "none")
Example01 Of raincloudplots::raincloud_1x1_repmes()
更多关于R-raincloudplots包绘制云雨图(Raincloud plots) 的小例子,可参考:raincloudplots[3]
今天的这篇推文,小编简单的汇总了一下如何使用R进行云雨图(Raincloud plots) 绘制的小技巧,当然,Python也是可以进行绘制的,这里小编就不做介绍了,希望对大家有所帮助。
[1]R-ggdist包官网: https://mjskay.github.io/ggdist/。
[2]gghalves包介绍: https://erocoar.github.io/gghalves/。
[3]raincloudplots: https://github.com/jorvlan/raincloudplots。