R中实现面积图 (Area plot)

2023-09-21 18:46:35 浏览数 (2)

面积图是一种基于折线图的图形,可以通过图中的面积来表示数据的大小情况,比如下面两种都是面积图:

wiki

下面是堆积面积图 (stacked area plot) 的在R中的简单实现,主要是用geom_area画出面积:

代码语言:javascript复制
set.seed(1492)
Sector <- rep(c("S01","S02","S03","S04","S05","S06","S07"),times=7)
Year <- rep(c("1950","1960","1970","1980","1990","2000","2010"),each=7)
Value <- runif(49, 10, 100)
df <- data.frame(Sector,Year,Value)

gg <- ggplot(df, aes(x=as.numeric(as.character(Year)), y=Value))   
         geom_area(aes(colour=Sector, fill=Sector))
gg

参考:

  1. https://gis.stackexchange.com/questions/163143/why-is-my-stacked-area-graph-in-ggplot2-returned-as-stacked-lines
  2. http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
  3. http://t-redactyl.io/blog/2015/12/creating-plots-in-r-using-ggplot2-part-2-area-plots.html

0 人点赞