使用 ggplot2 和 dplyr(而不是 plyr)的人口金字塔图

Population pyramid plot with ggplot2 and dplyr (instead of plyr)

我正在尝试从 post Simpler population pyramid in ggplot2

中重现简单的人口金字塔

使用 ggplot2dplyr(而不是 plyr)。

这是带有 plyr 和种子

的原始示例
set.seed(321)
test <- data.frame(v=sample(1:20,1000,replace=T), g=c('M','F'))

require(ggplot2)
require(plyr)    
ggplot(data=test,aes(x=as.factor(v),fill=g)) + 
  geom_bar(subset=.(g=="F")) + 
  geom_bar(subset=.(g=="M"),aes(y=..count..*(-1))) + 
  scale_y_continuous(breaks=seq(-40,40,10),labels=abs(seq(-40,40,10))) + 
  coord_flip()

工作正常。

但是我怎样才能用 dplyr 生成同样的图呢?该示例在 subset = .(g == 语句中使用 plyr

我用 dplyr::filter 尝试了以下操作,但出现错误:

require(dplyr)
ggplot(data=test,aes(x=as.factor(v),fill=g)) + 
  geom_bar(dplyr::filter(test, g=="F")) + 
  geom_bar(dplyr::filter(test, g=="M"),aes(y=..count..*(-1))) + 
  scale_y_continuous(breaks=seq(-40,40,10),labels=abs(seq(-40,40,10))) + 
  coord_flip()

Error in get(x, envir = this, inherits = inh)(this, ...) : 
  Mapping should be a list of unevaluated mappings created by aes or aes_string

您通过在 geom_bar 中指定参数 data 来避免错误:

ggplot(data = test, aes(x = as.factor(v), fill = g)) + 
  geom_bar(data = dplyr::filter(test, g == "F")) + 
  geom_bar(data = dplyr::filter(test, g == "M"), aes(y = ..count.. * (-1))) + 
  scale_y_continuous(breaks = seq(-40, 40, 10), labels = abs(seq(-40, 40, 10))) + 
  coord_flip() 

在使用最新版本的 ggplot2 制作人口金字塔时,您可以避免同时使用 dplyrplyr

如果你有年龄-性别群体的数量,那么使用答案here

如果您的数据是个人级别的(就像您的一样),请使用以下内容:

set.seed(321)
test <- data.frame(v=sample(1:20,1000,replace=T), g=c('M','F'))
head(test)
#    v g
# 1 20 M
# 2 19 F
# 3  5 M
# 4  6 F
# 5  8 M
# 6  7 F

library("ggplot2")
ggplot(data = test, aes(x = as.factor(v), fill = g)) + 
  geom_bar(data = subset(test, g == "F")) + 
  geom_bar(data = subset(test, g == "M"), 
           mapping = aes(y = - ..count.. ),
           position = "identity") +
  scale_y_continuous(labels = abs) +
  coord_flip()

要使用个人数据或微数据构建年龄金字塔,您可以使用:

test <- data.frame(v=sample(1:100, 1000, replace=T), g=c('M','F'))

ggplot(data = test, aes(x = v, fill = g)) + 
  geom_histogram(data = subset(test, g == "F"), binwidth = 5, color="white", position = "identity") +
  geom_histogram(data = subset(test, g == "M"), binwidth = 5, color="white", position = "identity", 
                 mapping = aes(y = - ..count.. )) +
  scale_x_continuous("Age", breaks = c(seq(0, 100, by=5))) +
  scale_y_continuous("Population", breaks = seq(-30, 30, 10), labels = abs) +
  scale_fill_discrete(name = "Sex") +
  coord_flip() +
  theme_bw()
 

更改 geom_histogram() 中的 binwidth 可以将您的数据分组到更广泛的类别中。

将 binwidth 更改为 10 并调整轴中断:

ggplot(data = test, aes(x = v, fill = g)) + 
  geom_histogram(data = subset(test, g == "F"), binwidth = 10, color="white", position = "identity") +
  geom_histogram(data = subset(test, g == "M"), binwidth = 10, color="white", position = "identity", 
                 mapping = aes(y = - ..count.. )) +
  scale_x_continuous("Age", breaks = c(seq(0, 100, by = 10))) +
  scale_y_continuous("Population", breaks = seq(-100, 100, 10), labels = abs) +
  scale_fill_discrete(name = "Sex") +
  coord_flip() +
  theme_bw()