R绘图R语言

探索 ggplot2 的其它图层

2021-04-06  本文已影响0人  c7a1c4db132c

ggplot2 包自带了很多图层,基本可以满足我们的各种绘图需要,但是有时候我们需要绘制一些“古怪”的图,就需要使用一些 ggplot2 的拓展包了,本文将介绍一些好用的 ggplot2 拓展包。

加载 R 包

library(tidyverse)
# install.packages('wesanderson')
library(wesanderson)

wesanderson 包里面提供了很多好看的调色板。

流图:geom_stream()

# 安装
# remotes::install_github("davidsjoberg/ggstream")
library(ggstream)

# 示例数据
blockbusters

#> # A tibble: 157 x 3
#>     year genre     box_office
#>    <dbl> <chr>          <dbl>
#>  1  1977 Action         2.98 
#>  2  1977 Adventure      0.209
#>  3  1977 Comedy         0.516
#>  4  1977 Drama          2.54 
#>  5  1978 Action         1.92 
#>  6  1978 Adventure      0.760
#>  7  1978 Comedy         1.04 
#>  8  1978 Drama          0.202
#>  9  1979 Action         1.15 
#> 10  1979 Adventure      0.312
#> # … with 147 more rows

# 绘图示例
ggplot(blockbusters, aes(year, box_office, fill = genre)) +
  geom_stream() +
  scale_fill_manual(values = wes_palette("Darjeeling2"))
image

山岭图:geom_density_ridges()

# 安装
# install.packages("ggridges")
library(ggridges)
 
ggplot(blockbusters, aes(x = box_office, y = genre, fill = genre)) +
  geom_density_ridges(scale = 4) +
  scale_fill_manual(values = wes_palette("Darjeeling2", n = 5))
image

桑基图:geom_sankey()

# 安装
# devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)

# 准备示例数据:
mtcars %>% 
  as_tibble()
#> # A tibble: 32 x 11
#>      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
#>    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
#>  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
#>  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
#>  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
#>  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
#>  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
#>  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
#>  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
#>  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
#> 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
#> # … with 22 more rows

mtcars %>%
  make_long(cyl, vs, am, gear, carb) -> example_dat

example_dat %>% 
  as_tibble()
#> # A tibble: 160 x 4
#>    x      node next_x next_node
#>    <fct> <dbl> <fct>      <dbl>
#>  1 cyl       6 vs             0
#>  2 vs        0 am             1
#>  3 am        1 gear           4
#>  4 gear      4 carb           4
#>  5 carb      4 <NA>          NA
#>  6 cyl       6 vs             0
#>  7 vs        0 am             1
#>  8 am        1 gear           4
#>  9 gear      4 carb           4
#> 10 carb      4 <NA>          NA
#> # … with 150 more rows

ggplot(example_dat,
       aes(x = x, 
               next_x = next_x, 
               node = node, 
               next_node = next_node,
               fill = factor(node))) +
   geom_sankey(flow.alpha = 0.6)
image

另一个用来绘制冲积图的包是 ggalluvial 包:

# 安装
# install.packages("ggalluvial")
library(ggalluvial)

ggplot(as.data.frame(UCBAdmissions),
       aes(y = Freq, axis1 = Gender, axis2 = Dept)) +
  geom_alluvium(aes(fill = Admit), width = 1/12) +
  scale_fill_manual(values = wes_palette("Darjeeling2"))
image

凹凸图:geom_bump()

可以用于展示排名的变化。

# 安装
# devtools::install_github("davidsjoberg/ggbump")
library(ggbump)

blockbusters %>% 
  dplyr::filter(genre %in% c("Action", "Comedy", "Drama")) %>% 
  group_by(year) %>% 
  mutate(rank = rank(box_office)) -> blockbusters2

ggplot(blockbusters2, aes(year, rank, color = genre)) +
  geom_point(size = 7) +
  geom_bump() +
  scale_color_manual(values = wes_palette("Darjeeling2"))
image

华夫图:geom_waffle

# 安装
# install.packages("waffle", repos = "https://cinc.rud.is")
library(waffle)

ggplot(as_tibble(Titanic), aes(fill = Sex, values = n)) +
  geom_waffle(n_rows = 20, color = "white") +
  facet_wrap(~ Survived, ncol = 1)  +
  scale_fill_manual(values = wes_palette("Darjeeling2"))
image

蜂巢图:geom_quasirandom()

# 安装
# install.packages("ggbeeswarm")
library(ggbeeswarm)

ggplot(blockbusters, aes(x = genre, y = box_office, color = genre)) + 
  geom_quasirandom() +
  scale_color_manual(values = wes_palette("Darjeeling2"))
image

镶嵌图

# 安装
# devtools::install_github("haleyjeppson/ggmosaic")
library(ggmosaic)

ggplot(as.data.frame(UCBAdmissions)) +
  geom_mosaic(aes(x = product(Admit, Dept), fill = Gender, weight = Freq)) +
  scale_fill_manual(values = wes_palette("Darjeeling2")) + 
  coord_flip()
image
上一篇下一篇

猜你喜欢

热点阅读