library(tidyverse)# install.packages('wesanderson')library(wesanderson)wesanderson 包里面提供了很多好看的调色板。
# 安装# 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"))# 安装# 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))# 安装# 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 rowsmtcars %>% make_long(cyl, vs, am, gear, carb) -> example_datexample_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 rowsggplot(example_dat, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node))) + geom_sankey(flow.alpha = 0.6)另一个用来绘制冲积图的包是 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"))可以用于展示排名的变化。
# 安装# devtools::install_github("davidsjoberg/ggbump")library(ggbump)blockbusters %>% dplyr::filter(genre %in% c("Action", "Comedy", "Drama")) %>% group_by(year) %>% mutate(rank = rank(box_office)) -> blockbusters2ggplot(blockbusters2, aes(year, rank, color = genre)) + geom_point(size = 7) + geom_bump() + scale_color_manual(values = wes_palette("Darjeeling2"))# 安装# 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"))# 安装# install.packages("ggbeeswarm")library(ggbeeswarm)ggplot(blockbusters, aes(x = genre, y = box_office, color = genre)) + geom_quasirandom() + scale_color_manual(values = wes_palette("Darjeeling2"))# 安装# 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()原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。