【问题标题】:population pyramid for different value in a data frame数据框中不同值的人口金字塔
【发布时间】:2021-06-22 11:43:25
【问题描述】:

我想创建人口金字塔。在mydata 中,我有两个国家italyindia 的两组数据。我需要的是为两国建立人口金字塔。

df<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "male", 
population =c(180,160,130,140,150,160,170,90,85,80,75,70,65,60,40), country = "italy")
                
df1<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "female", 
population =c(160,150,120,130,140,150,160,80,75,70,65,60,55,50,30),country  = "italy")

df2<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "male", 
 population =c(185,165,135,148,159,166,177,99,89,88,74,73,68,63,43), country = "india")

df3<- data_frame(age= c(0,5,10,15,20,25,30,35,40,45,50,55,60,65,70), gender = "female", 
 population =c(150,143,117,126,134,144,157,77,71,67,56,51,49,40,24), country = "india")
mydata<- rbind(df,df1,df2,df3)

【问题讨论】:

标签: r


【解决方案1】:

也许这就是你要找的东西:

library(ggplot2)

ggplot(data=mydata, aes(x=age, y = ifelse(gender == "male", - population, population), fill=gender)) + 
  geom_col() + 
  facet_wrap(~country) +
  coord_flip()

【讨论】:

  • @谢谢斯特凡。我可以为这两个金字塔制作动画吗?
  • 原则上这可以使用gganimate。参见例如towardsdatascience.com/…
猜你喜欢
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
相关资源
最近更新 更多