r语言rcpp 无法加载_R语言可视化(三十四):面积图绘制

35c0fb01c1993b2ae4072d1d8b117abf.gif

dc3fa20e2560007af0c37c2c385906a1.gif

e571afb57b34bc525234ff5ad350b249.gif

34. 面积图绘制


清除当前环境中的变量

rm(list=ls())

设置工作目录

setwd("C:/Users/Dell/Desktop/R_Plots/34area/")

使用ggplot2包绘制面积图

# 加载所需的R包
library(ggplot2)

# 构建示例数据
xValue yValue data # 查看示例数据
head(data)
## xValue yValue
## 1 1 0.23272473
## 2 2 -0.43662890
## 3 3 -0.07695937
## 4 4 -1.64320298
## 5 5 -2.32700864
## 6 6 -3.02549140

# 使用geom_area()函数绘制基础面积图
ggplot(data, aes(x=xValue, y=yValue)) +
geom_area()
49df7de6e5e85604c496dd112c2833d8.png
image.png
# 添加填充颜色,边界线和点,更换主题
ggplot(data, aes(x=xValue, y=yValue)) +
  geom_area(fill="#69b3a2", alpha=0.6) +
  geom_line(color="black", size=1.5) +
  geom_point(size=3, color="red") +
  theme_minimal() +
  ggtitle("Evolution of something")
a7d6d98b80682fd845e5f2fea33c15ed.png
image.png
# 绘制堆叠面积图
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

# 构建示例数据
time # x Axis
value # y Axis
group times=7)        # group, one shape per group
data head(data)
##   time    value group
## 1    1 89.38125     A
## 2    1 98.02650     B
## 3    1 89.63381     C
## 4    1 96.27630     D
## 5    1 44.17764     E
## 6    1 97.44857     F

# 绘制基础堆叠面积图
ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area()
1b4d359977435d80696e90faab4ed5ee.png
image.png
# 指定顺序进行堆叠
data$group $group, levels=c("B", "A", "D", "E", "G", "F", "C"))

# Plot again
ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area(color="black",alpha=0.6,size=1) + theme_bw()
d60e241cd3b12bf4ee4bf6bd97dd993c.png
image.png
# 更换填充色和主题
library(viridis)
## Loading required package: viridisLite

ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area(alpha=0.6 , size=.5, colour="white") +
  scale_fill_viridis(discrete = T) +
  theme_minimal() + 
  ggtitle("The race between ...")
8f138cdf597698c5b3569bdd20c8be86.png
image.png
# 进行分面展示
ggplot(data, aes(x=time, y=value, fill=group)) + 
  geom_area(alpha=0.6 , size=.8, colour="black") +
  scale_fill_viridis(option = "C",discrete = T) +
  theme_classic() + facet_wrap(.~group,ncol = 3)
9decb84d35f66f66d75fac7de7c5b69b.png
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] viridis_0.5.1 viridisLite_0.3.0 dplyr_1.0.2 ggplot2_3.3.2
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.5 knitr_1.23 magrittr_1.5 tidyselect_1.1.0
## [5] munsell_0.5.0 colorspace_1.4-1 R6_2.4.0 rlang_0.4.7
## [9] stringr_1.4.0 tools_3.6.0 grid_3.6.0 gtable_0.3.0
## [13] xfun_0.8 withr_2.1.2 htmltools_0.3.6 yaml_2.2.0
## [17] digest_0.6.20 lifecycle_0.2.0 tibble_2.1.3 crayon_1.3.4
## [21] gridExtra_2.3 purrr_0.3.2 vctrs_0.3.2 glue_1.4.2
## [25] evaluate_0.14 rmarkdown_1.13 labeling_0.3 stringi_1.4.3
## [29] compiler_3.6.0 pillar_1.4.2 generics_0.0.2 scales_1.0.0
## [33] pkgconfig_2.0.2

389fd07fd86c301388d669c2e8daa2ca.png

END

c41d7cba2ebe8b751f4952efabfca7cf.gif

caca3a0afbce0564a3bcf5d1ab2a55cd.png 935b0ba8936190e68face0eecdb1d9a0.png▼更多精彩推荐,请关注我们▼ 3d3fb390b9dfff818c4bf8d9f115d1e6.png把时间交给阅读 cabc9d23344cc48a1c5e86297f8516bf.png a1fb1f608f09babb634edffe88f74c52.gif 您点的每个赞,我都认真当成了喜欢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值