自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

R語言可視化之UpSetR包

大數(shù)據(jù) 數(shù)據(jù)可視化
今天介紹一個R包UpSetR,專門用來集合可視化,來源于UpSet,Python里面也有一個相似的包py-upset。此外還有個UpSetR shiny app以及源代碼。

簡介

集合可視化我們用得最多的是韋恩圖,韋恩圖在集合數(shù)少的時候是很好用的,但是當集合數(shù)多比如五個以上的時候那就會看花眼了,比如下面這副含有6個集合韋恩圖,是發(fā)表在Nature上的文章里的,漂亮是漂亮,但是眼都快看花了。

R語言可視化之UpSetR包

今天介紹一個R包UpSetR,專門用來集合可視化,來源于UpSet,Python里面也有一個相似的包py-upset。此外還有個UpSetR shiny app以及源代碼。

安裝

兩種方式安裝: 

  1. #從CRAN安裝  
  2. install.packages("UpSetR" 
  3. #從Github上安裝  
  4. devtools::install_github("hms-dbmi/UpSetR"

數(shù)據(jù)導(dǎo)入

 

UpSetR提供了兩個函數(shù)fromList以及fromExpression將數(shù)據(jù)轉(zhuǎn)換為UpsetR適用的數(shù)據(jù)格式。   

  1. #fromList  
  2. listinput <- list(one = c(1, 2, 3, 5, 7, 8, 11, 12, 13), two = c(1, 2, 4, 5, 
  3.  
  4. 10), three = c(1, 5, 6, 7, 8, 9, 10, 12, 13)) 
  5.  
  6. #fromExpression 
  7.  
  8. expressionInput <- c(one = 2, two = 1, three = 2, `one&two` = 1, `one&three` = 4, 
  9.  
  10. `two&three` = 1, `one&two&three` = 2) 

接下來就可以繪制繪制圖形了 

  1. library(UpSetR)  
  2. upset(fromList(listinput), order.by = "freq" 
  3. #下面繪制的圖形等同于上圖  
  4. upset(fromExpression(expressionInput), order.by = "freq"

 R語言可視化之UpSetR包

參數(shù)詳解

下面所有的例子都將使用UpSetR內(nèi)置的數(shù)據(jù)集movies來繪制。 

  1. #導(dǎo)入數(shù)據(jù)  
  2. movies <- read.csv(system.file("extdata""movies.csv", package = "UpSetR"), header = TRUE, sep = ";" 
  3. #先大致瀏覽一下該數(shù)據(jù)集,數(shù)據(jù)集太長,就只看前幾列  
  4. knitr::kable(head(movies[,1:10])) 

 R語言可視化之UpSetR包

該數(shù)據(jù)集展示的是電影名(name)、發(fā)行時間(ReleaseDate)以及電影類型,多了去了就不詳講了,自個可以看去。

UpsetR繪制集合可視化圖形使用函數(shù)upset()。

  1. upset(movies, nsets = 6, number.angles = 30, point.size = 2, line.size = 1, mainbar.y.label = "Genre Intersections", sets.x.label = "Movies Per Genre", text.scale = c(1.3, 1.3, 1, 1, 1.5, 1)) 

 R語言可視化之UpSetR包

解釋一下上面部分參數(shù):

  • nsets: 顧名思義,就是展示幾個集合,movies數(shù)據(jù)集由20幾個集合,不可能全部展示,另外從圖中可以看出,這6個集合應(yīng)該不是按順序選擇的。
  • numble.angle: 柱子上的數(shù)字看到了吧,這個參數(shù)就是調(diào)整數(shù)字角度的,可有可無的
  • mainbar.y.label/sets.x.label:坐標軸名稱
  • text.scale(): 有六個數(shù)字,分別控制c(intersection size title, intersection size tick labels, set size title, set size tick labels, set names, numbers above bars)。

很多時候我們想要看特定的幾個集合,UpSetR滿足我們的需求。 

  1. upset(movies, sets = c("Action""Adventure""Comedy""Drama""Mystery" 
  2. "Thriller""Romance""War""Western"), mb.ratio = c(0.55, 0.45), order.by = "freq"

 R語言可視化之UpSetR包

文中的參數(shù):

  • mb.ratio: 控制上方條形圖以及下方點圖的比例
  • order.by: 如何排序,這里freq表示從大到小排序展示,其他選項有degree以及先按freq再按degree排序。

各個變量也可以通過參數(shù)keep.order來排序 

  1. upset(movies, sets = c("Action""Adventure""Comedy""Drama""Mystery" 
  2. "Thriller""Romance""War""Western"), mb.ratio = c(0.55, 0.45), order.by = "freq" 
  3. keep.order = TRUE

 R語言可視化之UpSetR包

也可以按group進行展示,這圖展示的就是按各個變量自身、兩個交集、三個交集…依次展示。參數(shù)cutoff控制每個group顯示幾個交集。

參數(shù)intersects控制總共顯示幾個交集。

  1. upset(movies, nintersects = 70, group.by = "sets", cutoff = 7) 

 R語言可視化之UpSetR包

還有很多參數(shù)比如控制顏色的參數(shù),點、線大小等,具體可查看?upset

queries參數(shù)

queries參數(shù)分為四個部分:query, param, color, active.

  • query: 指定哪個query,UpSetR有內(nèi)置的,也可以自定義,說到底就是一個查詢函數(shù)
  • param: list, query作用于哪個交集
  • color:每個query都是一個list,里面可以設(shè)置顏色,沒設(shè)置的話將調(diào)用包里默認的調(diào)色板
  • active:被指定的條形圖是否需要顏色覆蓋,TRUE的話顯示顏色,F(xiàn)ALSE的話則在條形圖頂端顯示三角形

內(nèi)置的intersects query 

  1. upset(movies, queries = list(list(query=intersects, params=list("Drama""Comedy""Action"), color="orange", active=T),  
  2. list(query=intersects, params=list("Drama"), color="red", active=F),  
  3. list(query=intersects, params=list("Action""Drama"), active=T))) 

 R語言可視化之UpSetR包

內(nèi)置的elements query

此query可以可視化特定交集在不同條件下的情況 

  1. upset(movies, queries = list(list(query=elements, params=list("AvgRating", 3.5, 4.1), color="blue", active=T),  
  2. list(query=elements, params=list("ReleaseDate", 1980, 1990, 2000), color="red", active=F))) 

 R語言可視化之UpSetR包

自定義一個query 

  1. myfunc <- function(row, release, rating){  
  2. newdata <- (row["ReleaseDate"]%in%release)&(row["AvgRating"]>rating)  
  3.  
  4. upset(movies, queries = list(list(query=myfunc, params=list(c(1950,1960,1990,2000), 3.0), color="red", active=T))) 

 R語言可視化之UpSetR包

添加query圖例 

  1. upset(movies, query.legend = "top", queries = list(list(query = intersects,  
  2. params = list("Drama""Comedy""Action"), color = "orange", active = T,  
  3. query.name = "Funny action"), list(query = intersects, params = list("Drama"),  
  4. color = "red", active = F), list(query = intersects, params = list("Action" 
  5. "Drama"), active = T, query.name = "Emotional action"))) 

 R語言可視化之UpSetR包

參數(shù)attribute.plots

主要是用于添加屬性圖,內(nèi)置有柱形圖、散點圖、熱圖等

柱形圖 

  1. upset(movies, main.bar.color = "black", queries = list(list(query = intersects,  
  2. params = list("Drama"), active = T)), attribute.plots = list(gridrows = 50,  
  3. plots = list(list(plot = histogram, x = "ReleaseDate", queries = F), list(plot = histogram,  
  4. x = "AvgRating", queries = T)), ncols = 2)) 

 R語言可視化之UpSetR包

散點圖 

  1. upset(movies, main.bar.color = "black", queries = list(list(query = intersects,  
  2. params = list("Drama"), color = "red", active = F), list(query = intersects,  
  3. params = list("Action""Drama"), active = T), list(query = intersects,  
  4. params = list("Drama""Comedy""Action"), color = "orange", active = T)),  
  5. attribute.plots = list(gridrows = 45, plots = list(list(plot = scatter_plot,  
  6. x = "ReleaseDate", y = "AvgRating", queries = T), list(plot = scatter_plot,  
  7. x = "AvgRating", y = "Watches", queries = F)), ncols = 2), query.legend = "bottom"

 R語言可視化之UpSetR包

箱線圖

箱線圖可以展示數(shù)據(jù)的分布,通過參數(shù)boxplot.summary控制,最多可以一次性顯示兩個箱線圖 

  1. upset(movies, boxplot.summary = c("AvgRating""ReleaseDate")) 

 R語言可視化之UpSetR包

還有一個十分重要的功能Incorporating Set Metadata這里就不講了。

SessionInfo 

  1. sessionInfo()  
  2. ## R version 3.4.2 (2017-09-28)  
  3. ## Platform: x86_64-w64-mingw32/x64 (64-bit 
  4. ## Running under: Windows 10 x64 (build 15063)  
  5. ##  
  6. ## Matrix products: default  
  7. ##  
  8. ## locale:  
  9. ## [1] LC_COLLATE=Chinese (Simplified)_China.936  
  10. ## [2] LC_CTYPE=Chinese (Simplified)_China.936  
  11. ## [3] LC_MONETARY=Chinese (Simplified)_China.936  
  12. ## [4] LC_NUMERIC=C  
  13. ## [5] LC_TIME=Chinese (Simplified)_China.936  
  14. ##  
  15. ## attached base packages:  
  16. ## [1] stats graphics grDevices utils datasets methods base  
  17. ##  
  18. ## other attached packages:  
  19. ## [1] UpSetR_1.3.3  
  20. ##  
  21. ## loaded via a namespace (and not attached):  
  22. ## [1] Rcpp_0.12.13 knitr_1.17 magrittr_1.5 munsell_0.4.3  
  23. ## [5] colorspace_1.3-2 rlang_0.1.2 stringr_1.2.0 highr_0.6  
  24. ## [9] plyr_1.8.4 tools_3.4.2 grid_3.4.2 gtable_0.2.0  
  25. ## [13] htmltools_0.3.6 yaml_2.1.14 lazyeval_0.2.0 rprojroot_1.2  
  26. ## [17] digest_0.6.12 tibble_1.3.4 gridExtra_2.3 ggplot2_2.2.1  
  27. ## [21] evaluate_0.10.1 rmarkdown_1.6 labeling_0.3 stringi_1.1.5  
  28. ## [25] compiler_3.4.2 scales_0.5.0 backports_1.1.1 
責任編輯:未麗燕 來源: 36大數(shù)據(jù)
相關(guān)推薦

2018-03-27 22:40:59

深度學(xué)習(xí)

2018-05-16 07:53:51

R函數(shù)可視化

2020-03-11 14:39:26

數(shù)據(jù)可視化地圖可視化地理信息

2017-10-14 13:54:26

數(shù)據(jù)可視化數(shù)據(jù)信息可視化

2022-08-26 09:15:58

Python可視化plotly

2009-04-21 14:26:41

可視化監(jiān)控IT管理摩卡

2018-03-26 20:07:25

深度學(xué)習(xí)

2022-07-13 15:54:14

Matplotlib圖表

2017-06-29 11:26:08

Python數(shù)據(jù)可視化

2020-08-21 14:09:46

可視化數(shù)據(jù)集R語言

2017-07-25 14:50:50

Rshinydashbo可視化

2009-10-21 14:49:46

VB入門教程

2015-08-20 10:06:36

可視化

2016-02-02 16:07:32

可視化大數(shù)據(jù)數(shù)據(jù)可視化

2022-08-04 13:58:54

SeabornFacetGrid代碼

2019-03-05 09:20:47

Vim可視化模式命令

2014-01-17 10:36:39

2015-08-20 10:00:45

可視化

2020-09-27 11:15:37

可視化PandasPython

2011-06-16 15:09:08

點贊
收藏

51CTO技術(shù)棧公眾號