以下是一个之前在网络上引起巨大轰动的人口金字塔动态图:
没有找到原始数据,后来在UN的官网上找到了一个五年间隔的数据,顺便还原了下图表:
library(ggplot2)
library(animation)
library(dplyr)
library(tidyr)
library(xlsx)
library(ggthemes)
setwd("E:/数据可视化/R/R语言学习笔记/可视化/ggplot2/商务图表/动态图表")
female<-read.xlsx("Population.xlsx",sheetName="Female",header=T,encoding='UTF-8',check.names = FALSE)
male<-read.xlsx("Population.xlsx",sheetName="Male",header=T,encoding='UTF-8',check.names = FALSE)
female<-female%>%gather(Year,Poputation,-1)
male<-male%>%gather(Year,Poputation,-1)
female$Poputation<-female$Poputation*-1
male$sex<-"male";female$sex<-"female"
China_Population<-rbind(male,female)%>%mutate(abs_pop=abs(Poputation))
China_Population$agegroup<-factor(China_Population$agegroup,
levels=c("0-4","5-9","10-14","15-19","20-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64","65-69","70-74","75-79","80+") ,order=T)
m<-seq(1950,2015,by=5)
saveGIF({
for (i in m) {
title <- as.character(i)
year_data <- filter(China_Population,Year==i)
g1<-ggplot(year_data,aes(x =agegroup,y=Poputation,fill=sex,width=1)) +
coord_fixed()+
coord_flip() +
geom_bar(data=subset(year_data,sex=="female"),stat = "identity") +
geom_bar(data=subset(year_data,sex=="male"), stat = "identity") +
scale_y_continuous(breaks = seq(-70000,70000,length=9),
labels = paste0(as.character(c(abs(seq(-70,70,length=9)))), "m"),
limits = c(-75000, 75000)) +
theme_economist(base_size = 14) +
scale_fill_manual(values = c('#D40225', '#374F8F')) +
labs(title=paste0("Population structure of China:", title),
caption="Data Source:United Nations Department of Economic and Docial Affairs\nPopulation Division\nWorld Population Prospects,the 2015 Revision"
,y="Population",x="Age") +
guides(fill=guide_legend(reverse = TRUE))+
theme(
legend.position =c(0.8,0.9),
legend.title = element_blank(),
plot.title = element_text(size=20),
plot.caption = element_text(size=12,hjust=0),
)
print(g1)
}
},movie.name='japan_pyramid.gif',interval=0.5,ani.width=700,ani.height=600)
wechat:ljty1991
Mail:[email protected]
个人公众号:数据小魔方(datamofang)
团队公众号:EasyCharts
qq交流群:[魔方学院]553270834
本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。