做了个参考代码
- library(ggplot2)
- library(dplyr)
- #示例数据
- data<-
- data.frame(
- year=
- c(2001,2002,2003,2002,2003,2005,2006,
- 2008,2009,2010,2003,2006,2007,2009,
- 2010,2007,2008,2002,2008,2002,2003,
- 2008,2009,2003,2004,2008,2009,2010),
- age=
- c(20,20,20,30,30,30,30,
- 30,30,30,40,40,40,40,
- 40,50,50,60,60,70,70,
- 70,70,80,80,80,80,80),
- dQ=
- c(1,1,1,1,1,3,3,
- 5,5,5,1,3,3,5,
- 5,3,3,2,4,2,2,
- 4,4,2,2,4,4,4)
- )
- #函数确定顶点
- findVertex<-function (x) {
- vertex<-data.frame()
- for (i in 1:5) {
- temp<-x%>%filter(x[,3]==i)%>%chull()
- vertex<-rbind(vertex,x[which(x[,3]==i),][temp,])
- }
- vertex
- }
- #形成顶点数据
- polyg<-findVertex(data)
- #画图
- ggplot(polyg)+
- aes(year,age,group=dQ,fill=dQ)+
- geom_polygon()+
- scale_fill_gradient(low = "yellow",
- high = "blue",
- aesthetics = "fill")+
- theme_minimal()
复制代码
|