Yangni 建议把数据的具体格式贴上来,否则实在不知道怎么回帖。。。
分类汇总的方法有很多,像aggregate()
、data.table
包、dplyr
包等,可以自己去搜搜看。
set.seed(1234)
dat<-data.frame(park=rep(LETTERS[1:5],each=5),
year=rep(1990:1994,5),
pop=sample(1e3:2e3,25))
head(dat,10)
#> park year pop
#> 1 A 1990 1283
#> 2 A 1991 1847
#> 3 A 1992 1917
#> 4 A 1993 1100
#> 5 A 1994 1622
#> 6 B 1990 1904
#> 7 B 1991 1644
#> 8 B 1992 1933
#> 9 B 1993 1399
#> 10 B 1994 1899
newdat<-aggregate(pop~year,data=dat,sum)
newdat
#> year pop
#> 1 1990 6545
#> 2 1991 8139
#> 3 1992 8461
#> 4 1993 7360
#> 5 1994 7887
library(ggplot2)
ggplot(newdat,aes(x=year,y=pop))+geom_line()
<sup>Created on 2020-09-07 by the reprex package (v0.3.0)</sup>