微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 GGPlot 在 R 中创建人口金字塔图

如何解决使用 GGPlot 在 R 中创建人口金字塔图

我尝试按照此处给出的示例进行操作:


n1 <- ggplot(nigeria,aes(x = Age,y = Population,fill = Gender)) + 
  geom_bar(subset = .(Gender == "Female"),stat = "identity") + 
  geom_bar(subset = .(Gender == "Male"),stat = "identity") + 
  scale_y_continuous(breaks = seq(-15000000,15000000,5000000),labels = paste0(as.character(c(seq(15,-5),seq(5,15,5))),"m")) + 
  coord_flip() + 
  scale_fill_brewer(palette = "Set1") + 
  theme_bw()

n1

但是我的数据设置有点不同。在我的数据中,男性和女性位于不同的列中。有没有办法使用以下数据创建人口金字塔:

pop_1950 = data.frame (age_groups = c("0-4","5 - 12","13 - 18","19 - 24","25 - 34","35  - 44","45 - 54","55 - 64","65 - 74","75 - 84","85 - 94","95+"),females = c(151272.13,207176.23,138778.36,115109.06,192698.05,18232.01,156810.06,124283.91,105981.35,48945.70,7273.47,301.96),males = c(158878.66,215774.86,148482.68,123611.00,194782.15,19387.82,163137.82,126669.64,104974.21,46382.39,5146.77,170.79))

解决方法

您需要将数据转换为长格式,因此您可以使用“性别”和“数字”来代替男性和女性的列。

pop_1950 %>% pivot_longer(cols = c('females','males'),names_to = 'Sex',values_to = 'Number') %>% ggplot(...)

将数据转换为长格式通常对 ggplot2 很有用。

,

您可以像下面这样尝试reshape2::melt

reshape2::melt(pop_1950,id.vars = "age_groups",value.name = "Number",variable.name = "Sex"
)

给出

   age_groups     Sex    Number
1         0-4 females 151272.13
2      5 - 12 females 207176.23
3     13 - 18 females 138778.36
4     19 - 24 females 115109.06
5     25 - 34 females 192698.05
6    35  - 44 females  18232.01
7     45 - 54 females 156810.06
8     55 - 64 females 124283.91
9     65 - 74 females 105981.35
10    75 - 84 females  48945.70
11    85 - 94 females   7273.47
12        95+ females    301.96
13        0-4   males 158878.66
14     5 - 12   males 215774.86
15    13 - 18   males 148482.68
16    19 - 24   males 123611.00
17    25 - 34   males 194782.15
18   35  - 44   males  19387.82
19    45 - 54   males 163137.82
20    55 - 64   males 126669.64
21    65 - 74   males 104974.21
22    75 - 84   males  46382.39
23    85 - 94   males   5146.77
24        95+   males    170.79
,

我们也可以使用 gather

 library(dplyr)
 library(tidyr)
 pop_1950 %>%
      gather(Sex,Number,females,males)
,

在做了一些实验后,我想出了以下适用于我的数据的解决方案

ggplot(pop_1950) +
  geom_col(aes(x=age_groups,y=females,fill = "red")) +
  geom_col(aes(x=age_groups,y=-males,fill = "blue")) + 
  scale_y_continuous(breaks = seq(-200000,200000,50000),labels = paste0(as.character(c(seq(200,-50),seq(50,200,50))))) + 
  coord_flip() + 
  scale_fill_discrete(name = "Gender",labels = c("Female","Male"))+
  xlab("Age")+
  ylab("Population (000's)")+
  theme_minimal()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。