美文网首页R语言学习
R语言可视化(三十五):流动图绘制

R语言可视化(三十五):流动图绘制

作者: Davey1220 | 来源:发表于2020-11-29 20:42 被阅读0次

35. 流动图绘制

清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/35streamgraph/")

安装并加载所需的R包

# 安装streamgraph包
#devtools::install_github("hrbrmstr/streamgraph")

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

library(streamgraph)
## Loading required package: htmlwidgets
## Loading required package: htmltools

构建示例数据

data <- data.frame(
  year=rep(seq(1990,2016) , each=10),
  name=rep(letters[1:10] , 27),
  value=sample( seq(0,1,0.0001) , 270)
)

# 查看示例数据
head(data)
##   year name  value
## 1 1990    a 0.2709
## 2 1990    b 0.1812
## 3 1990    c 0.2304
## 4 1990    d 0.0452
## 5 1990    e 0.4080
## 6 1990    f 0.0464

使用streamgraph包绘制流动图

# 基础绘图,需要提供三列变量
# 默认interactive=T,绘制可交互式图
streamgraph(data, key="name", value="value", date="year")
image.png
# 设置图片的高度和宽度,interactive = F绘制静态图
pp <- streamgraph(data, key="name", value="value", date="year", 
                  height="600px", width="1000px",interactive = F)
pp
image.png
# 指定offset参数设置纵坐标偏移模式,默认offset = "silhouette"
streamgraph(data, key="name", value="value", date="year", 
            offset = "silhouette", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            offset = "wiggle", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            offset = "expand", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            offset = "zero", interactive = F)
image.png
# 指定interpolate参数设置绘图类型,默认interpolate = "cardinal"
streamgraph(data, key="name", value="value", date="year", 
            interpolate = "cardinal", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            interpolate = "linear", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            interpolate = "step", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            interpolate = "basis", interactive = F)
image.png
streamgraph(data, key="name", value="value", date="year", 
            interpolate = "monotone", interactive = F)
image.png
# 更改绘图颜色
# Graph 1: choose a RColorBrewer palette -> continuous
p1 <- streamgraph(data, key="name", value="value", date="year") %>%
  sg_fill_brewer("Blues")
p1
image.png
# Graph 2: choose a RColorBrewer palette -> categorical
p2 <- streamgraph(data, key="name", value="value", date="year") %>%
  sg_fill_brewer("Pastel1")
p2
image.png
# Graph 3: choose color manually with number, color name, rgb ...
p3 <- streamgraph(data, key="name", value="value", date="year") %>%
  sg_fill_manual(c(1:10))
p3
image.png
sessionInfo()
## R version 3.6.0 (2019-04-26)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 18363)
## 
## 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] streamgraph_0.7 htmltools_0.3.6 htmlwidgets_1.3 dplyr_1.0.2    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5       knitr_1.23       magrittr_1.5     tidyselect_1.1.0
##  [5] lattice_0.20-38  R6_2.4.0         rlang_0.4.7      stringr_1.4.0   
##  [9] tools_3.6.0      xts_0.12.1       grid_3.6.0       xfun_0.8        
## [13] ellipsis_0.2.0.1 yaml_2.2.0       digest_0.6.20    tibble_2.1.3    
## [17] lifecycle_0.2.0  crayon_1.3.4     tidyr_1.1.2      purrr_0.3.2     
## [21] vctrs_0.3.2      glue_1.4.2       evaluate_0.14    rmarkdown_1.13  
## [25] stringi_1.4.3    compiler_3.6.0   pillar_1.4.2     generics_0.0.2  
## [29] jsonlite_1.6     pkgconfig_2.0.2  zoo_1.8-6

相关文章

网友评论

    本文标题:R语言可视化(三十五):流动图绘制

    本文链接:https://www.haomeiwen.com/subject/zjecwktx.html