云雨图【R 语言】

什么是云雨图

在这里插入图片描述
不管它叫啥,就是一种对数据的可视化方法,可同时描述数据的概率密度和统计量。

为什么使用云雨图

当我们获得几组数据的时候,想看一看他们的统计量,在R中可以使用命令:summary() ,会将五分位数和均值等输出到 「Console」。但是相比于文字,一般人对图像的理解要更好,所以可以使用**箱线图(BoxPlot)**展示其统计量(下左),这时就会出现一个问题,(下左)几个箱线图似乎都一样,难道意味着这些组数据都一样?当我们把原始数据点添加上去(下右),就会发现数据之间的不同:数据的分布密度是不一样的。
在这里插入图片描述

这个时候就可以使用「 小提琴图(ViolinPlot)」描述数据的分布密度

在这里插入图片描述

这时,有人就把这些东西都集合到了一起:云雨图(RainCloud Plot),一半儿的小提琴 + 箱线图 + 数据点

例子 R

需要的「R Packages」:ggdist、 tidyquant、 tidyverse

需要的「数据集」:mpg (源自ggplot2)

  1. 加载需要的工具和数据:
# LIBRARIES ----
library(ggdist)
library(tidyquant)
library(tidyverse)

# DATA -----
mpg
  1. 筛选数据,并新建一个「ggplot2」的画布(Canvas)
mpg %>%
    filter(cyl %in% c(4, 6, 8)) %>%
    ggplot(aes(x = factor(cyl), y = hwy, fill = factor(cyl)))

  1. 画上一半儿的小提琴:ggdist::stat_halfeye()
# add half-violin from {ggdist} package
ggdist::stat_halfeye(
        ## custom bandwidth
        adjust = 0.5,
        ## move geom to the right
        justification = -.2,
        ## remove slab interval
        .width = 0,
        point_colour = NA
    )

在这里插入图片描述

  1. 加上箱线图: ggplot2::geom_boxplot() ,限制宽度和不透明度
geom_boxplot(
        width = .12,
        ## remove outliers
        outlier.color = NA,
        alpha = 0.5
    )

在这里插入图片描述

  1. 添加原始数据点:ggdist::stat_dots()
# Add dot plots from {ggdist} package
    ggdist::stat_dots(
        ## orientation to the left
        side = "left",
        ## move geom to the left
        justification = 1.1,
        ## adjust grouping (binning) of observations
        binwidth = .25
    )

在这里插入图片描述

6.「 」调细节:tidyquant::theme_tq() + coord_flip()

scale_fill_tq() +
    theme_tq() +
    labs(
        title = "Raincloud Plot",
        subtitle = "Showing the Bi-Modal Distribution of 6 Cylinder Vehicles",
        x = "Engine Size (No. of Cylinders)",
        y = "Highway Fuel Economy (MPG)",
        fill = "Cylinders"
    ) +
    coord_flip()

在这里插入图片描述

完整代码「R」

# LIBRARIES ----
library(ggdist)
library(tidyquant)
library(tidyverse)

# DATA -----
mpg

# RAINCLOUD PLOTS ----
# Powerful for visualizing modality of distributions

mpg %>%
    filter(cyl %in% c(4, 6, 8)) %>%
    ggplot(aes(x = factor(cyl), y = hwy, fill = factor(cyl))) +

    # add half-violin from {ggdist} package
    ggdist::stat_halfeye(
        ## custom bandwidth
        adjust = 0.5,
        ## move geom to the right
        justification = -.2,
        ## remove slab interval
        .width = 0,
        point_colour = NA
    ) +
    geom_boxplot(
        width = .12,
        ## remove outliers
        outlier.color = NA,
        alpha = 0.5
    ) +
    # Add dot plots from {ggdist} package
    ggdist::stat_dots(
        ## orientation to the left
        side = "left",
        ## move geom to the left
        justification = 1.1,
        ## adjust grouping (binning) of observations
        binwidth = .25
    ) +
    # Adjust theme
    scale_fill_tq() +
    theme_tq() +
    labs(
        title = "Raincloud Plot",
        subtitle = "Showing the Bi-Modal Distribution of 6 Cylinder Vehicles",
        x = "Engine Size (No. of Cylinders)",
        y = "Highway Fuel Economy (MPG)",
        fill = "Cylinders"
    ) +
    coord_flip()
欢迎关注:猪猪的乌托邦
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值