一咨察、介紹
所謂的云雨圖论熙,基本形狀由云朵和雨點組成,上面云朵是數(shù)據(jù)密度圖摄狱,傘就是平鋪的箱線圖脓诡,雨就是下面的數(shù)據(jù)點。百聞不如一見媒役,快隨我一起來瞅瞅如何繪制吧祝谚。
二、R 語言實現(xiàn)
2.1 準備數(shù)據(jù)
準備下圖所示數(shù)據(jù)酣衷,數(shù)據(jù)文件第一列對應分類名稱交惯,其它列為對應的數(shù)據(jù)。
2.2 命令實現(xiàn)
# 自定義函數(shù)
"%||%" <- 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,
...
)
)
}
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")
)
繪制基礎圖
p0 <- ggplot(data = mat, aes(x = variable, y = value, fill = variable)) +
geom_flat_violin(position = position_nudge(x = 2, y = 0.8), alpha = 0.6)
添加散點圖
p1 <- p0 + geom_jitter(aes(color=variable),
width=10,
size=2,
shape=1,
alpha = 0.6,
show.legend=NA)
添加箱線圖
p2 <- p0 + geom_boxplot(
position=position_nudge(x=0.2),
outlier.shape = FALSE,
alpha = 0.4,
width = 20,
colour = "#000000")
2.3 平臺實現(xiàn)
用“派森諾基因云” 的 【云雨圖】,可以很方便的作上述圖席爽,并且提供了很多參數(shù)可以在線自由調整樣式意荤。
用上述示例數(shù)據(jù),上傳后提交繪圖即可只锻。
參數(shù)調整
- 在【圖表設置】里面玖像,可以調整圖表類型為云雨箱線圖、云箱線圖齐饮、云雨圖捐寥。
- 同時還可調整云雨圖的圖表方向,云朵的距離祖驱,云朵的顏色及透明度等握恳,更多效果歡迎登錄派森諾基因云進行體驗。
每周掌握一個可視化工具捺僻,希望以后手里有很多把錘子乡洼。