R可视化之小提琴图

本文翻译自“The graph gallery”网站,原帖文地址链接:https://www.r-graph-gallery.com/95-violin-plot-with-ggplot2.html.

0.1 基础小提琴图绘制

0.1.1 使用ggplot2包中的geom_violin()函数.

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.6.1
#构建数据
data <- data.frame(
  name = c( rep("A",500), rep("B",500), rep("B",500), rep("C",20), rep('D', 100)),
  value = c( rnorm(500, 10, 5), rnorm(500, 13, 1), rnorm(500, 18, 1), rnorm(20, 25, 4), rnorm(100, 12, 1) )
)

head(data)
##   name     value
## 1    A 14.586213
## 2    A  8.547782
## 3    A 17.655333
## 4    A  5.494113
## 5    A 15.576023
## 6    A 12.404492
# Most basic violin chart
p <- 
  ggplot(data, aes(x = name, y = value, fill = name)) + 
  geom_violin() +
  theme_bw()

p

0.1.1.1 数据格式

注意在使用geom_violin()函数构建小提琴图时,数据格式为长数据(long format).每一行时一个观测.一共需要两列:

  • 一个分类变量,用于X轴.需要为factor格式.
  • 数值型变量,用于Y轴.

0.1.1.2 数据转换

如果数据为宽数据格式,需要对其进行转变.可以使用tidyr中的pivot_longer()函数将其转变为长数据格式.

data_wide <- 
  iris[ , 1:4]
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.1
## -- Attaching packages ---------------------------------------------------------------------------------- tidyverse 1.2.1 --
## v tibble  2.1.3     v purrr   0.3.2
## v tidyr   1.0.0     v dplyr   0.8.3
## v readr   1.3.1     v stringr 1.4.0
## v tibble  2.1.3     v forcats 0.4.0
## Warning: package 'tibble' was built under R version 3.6.1
## Warning: package 'tidyr' was built under R version 3.6.1
## Warning: package 'dplyr' was built under R version 3.6.1
## -- Conflicts ------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
data_long <- 
  data_wide %>% 
  tidyr::pivot_longer(cols = everything(), 
                      names_to = "variable", values_to = "value")
head(data_long)
## # A tibble: 6 x 2
##   variable     value
##   <chr>        <dbl>
## 1 Sepal.Length   5.1
## 2 Sepal.Width    3.5
## 3 Petal.Length   1.4
## 4 Petal.Width    0.2
## 5 Sepal.Length   4.9
## 6 Sepal.Width    3
library(ggplot2)

data_long %>% 
  ggplot(., aes(variable, value)) +
  geom_violin(aes(fill = variable)) +
  theme_bw()

0.2 将小提琴图水平展示

library(ggplot2)
library(tidyverse)
  iris[ , 1:4] %>% 
  tidyr::pivot_longer(cols = everything(), 
                      names_to = "variable",
                      values_to = "value") %>% 
  ggplot(., aes(variable, value)) +
  geom_violin(aes(fill = variable, colour = variable)) +
  theme_bw() +
    theme(legend.position = "none") +
    coord_flip()

0.3 小提琴图与箱型图同时展示

library(ggplot2)
library(tidyverse)
  iris[ , 1:4] %>% 
  tidyr::pivot_longer(cols = everything(), 
                      names_to = "variable",
                      values_to = "value") %>% 
  ggplot(., aes(variable, value)) +
  geom_violin(aes(fill = variable, colour = variable), width = 1.4) +
    geom_boxplot(fill = NA, width = 0.1, colour = "grey") +
  theme_bw() +
    theme(legend.position = "none") +
    coord_flip()
## Warning: position_dodge requires non-overlapping x intervals

相关

下一页
上一页
comments powered by Disqus