1. 程式人生 > >R語言視覺化學習筆記之ggridges包

R語言視覺化學習筆記之ggridges包

640?wx_fmt=gif&wxfrom=5&wx_lazy=1

作者簡介Introduction

taoyan:R語言中文社群特約作家,偽碼農,R語言愛好者,愛開源。

個人部落格: https://ytlogos.github.io/

公眾號:生信大講堂

往期回顧

R語言data manipulation學習筆記之subset data

640?wx_fmt=gif&wxfrom=5&wx_lazy=1

640?wx_fmt=png

簡介

ggridges包主要用來繪製山巒圖。尤其是針對時間或者空間分佈份視覺化具有十分好的效果。ggridges主要提供兩個幾何影象函式:

  • geom_ridgeline():主要繪製山脊線圖

  • geom_density_ridges():主要根據密度繪製山脊線圖

具體用法可以參考官方文件(https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html)

geom_ridgeline()

library(ggridges)

library(tidyverse)

library(gridExtra)

my_data <- data.frame(x=1:5, y=rep(1,5), height=c(0,1,-1,3,2))

plot_base <- ggplot(my_data, aes(x, y, height=height))

grid.arrange(plot_base+geom_ridgeline(),

    plot_base+geom_ridgeline(min_height=-2), ncol=2)

640?wx_fmt=png

geom_density_ridges()

geom_density_ridges()函式首先會根據資料計算密度然後繪圖,此時美學對映height沒有必要寫入函式中。下面使用lincoln_weather資料集。

library(viridis)

head(lincoln_weather[ ,1:4])

## # A tibble: 6 x 4

##   CST      `Max Temperature [F]` `Mean Temperature [F]` `Min Temperature ~

##   <chr>                    <int>                  <int>              <int>

## 1 2016-1-1                    37                     24                 11

## 2 2016-1-2                    41                     23                  5

## 3 2016-1-3                    37                     23                  8

## 4 2016-1-4                    30                     17                  4

## 5 2016-1-5                    38                     29                 19

## 6 2016-1-6                    34                     33                 32

ggplot(lincoln_weather, aes(x=`Mean Temperature [F]`, y=`Month`, fill=..x..))+

    geom_density_ridges_gradient(scale=3, rel_min_height=0.01, gradient_lwd = 1.)+

    scale_x_continuous(expand = c(0.01, 0))+

    scale_y_discrete(expand = c(0.01,0))+

    scale_fill_viridis(name="Temp. [F]", option = "C")+

    labs(title="Temperature in Lincoln NE",subtitle="Mean temperature (Fahrenheit) by month for 2016\nData:Orogin CSV from the Weather Underground ")+

    theme_ridges(font_size = 13, grid = FALSE)+

    theme(axis.title.y = element_blank())

640?wx_fmt=png

cyclinal scales

為了使得ggridges繪製的圖形視覺化效果最好,同時為了減少使用者對顏色設定的困難,作者提供了cyclinal scales用於顏色輪轉對映。

ggplot(diamonds, aes(x=price, y=cut, fill=cut))+

    geom_density_ridges(scale=4)+

    scale_fill_cyclical(values = c("blue", "green"))+

    theme_ridges(grid = FALSE)

640?wx_fmt=png

預設的,cyclinal scales為了防止誤解是不繪製圖例的,但是可以通過選項guide="legend"新增圖例。

ggplot(diamonds, aes(x=price, y=cut, fill=cut))+

    geom_density_ridges(scale=4)+

    scale_fill_cyclical(values = c("blue", "green"), guide="legend")+

    theme_ridges(grid = FALSE)

640?wx_fmt=png

跟ggplot2一樣,圖例是可以修改的,其他引數比如大小、透明度、形狀等都是可以通過cyclinal scales修改。

ggplot(diamonds, aes(x=price, y=cut, fill=cut))+

    geom_density_ridges(scale=4)+

    scale_fill_cyclical(values = c("blue", "green"), guide="legend",labels=c("Fair"="blue", "Good"="green"),name="Fill colors")+

    theme_ridges(grid = FALSE)

640?wx_fmt=png

還有很多用法有興趣的可以檢視官方文件繼續學習。

SessionInfo

sessionInfo()
## R version 3.4.3 (2017-11-30)

## Platform: x86_64-w64-mingw32/x64 (64-bit)

## Running under: Windows 10 x64 (build 16299)

##

## Matrix products: default

##

## locale:

## [1] LC_COLLATE=Chinese (Simplified)_China.936

## [2] LC_CTYPE=Chinese (Simplified)_China.936

## [3] LC_MONETARY=Chinese (Simplified)_China.936

## [4] LC_NUMERIC=C

## [5] LC_TIME=Chinese (Simplified)_China.936

##

## attached base packages:

## [1] stats     graphics  grDevices utils     datasets  methods   base

##

## other attached packages:

##  [1] viridis_0.5.0       viridisLite_0.3.0   gridExtra_2.3

##  [4] forcats_0.2.0       stringr_1.2.0       dplyr_0.7.4

##  [7] purrr_0.2.4         readr_1.1.1         tidyr_0.8.0

## [10] tibble_1.4.2        tidyverse_1.2.1     ggridges_0.4.1.9990

## [13] ggplot2_2.2.1.9000

##

## loaded via a namespace (and not attached):

##  [1] reshape2_1.4.3    haven_1.1.1       lattice_0.20-35

##  [4] colorspace_1.3-2  htmltools_0.3.6   yaml_2.1.16

##  [7] utf8_1.1.3        rlang_0.1.6       pillar_1.1.0

## [10] foreign_0.8-69    glue_1.2.0        modelr_0.1.1

## [13] readxl_1.0.0      bindrcpp_0.2      bindr_0.1

## [16] plyr_1.8.4        munsell_0.4.3     gtable_0.2.0

## [19] cellranger_1.1.0  rvest_0.3.2       psych_1.7.8

## [22] evaluate_0.10.1   labeling_0.3      knitr_1.19

## [25] parallel_3.4.3    broom_0.4.3       Rcpp_0.12.15

## [28] scales_0.5.0.9000 backports_1.1.2   jsonlite_1.5

## [31] mnormt_1.5-5      hms_0.4.1         digest_0.6.15

## [34] stringi_1.1.6     grid_3.4.3        rprojroot_1.3-2

## [37] cli_1.0.0         tools_3.4.3       magrittr_1.5

## [40] lazyeval_0.2.1    crayon_1.3.4      pkgconfig_2.0.1

## [43] xml2_1.2.0        lubridate_1.7.1   assertthat_0.2.0

## [46] rmarkdown_1.8     httr_1.3.1        rstudioapi_0.7

## [49] R6_2.2.2          nlme_3.1-131      compiler_3.4.3

 往期精彩內容整理合集 

640?wx_fmt=jpeg

公眾號後臺回覆關鍵字即可學習

回覆 R                  R語言快速入門及資料探勘 
回覆 Kaggle案例  Kaggle十大案例精講(連載中)
回覆 文字挖掘      手把手教你做文字挖掘
回覆 視覺化          R語言視覺化在商務場景中的應用 
回覆 大資料         大資料系列免費視訊教程 
回覆 量化投資      張丹教你如何用R語言量化投資 
回覆 使用者畫像      京東大資料,揭祕使用者畫像
回覆 資料探勘     常用資料探勘演算法原理解釋與應用
回覆 機器學習     人工智慧系列之機器學習與實踐
回覆 爬蟲            R語言爬蟲實戰案例分享