人口金字塔图与ggplot2和dplyr(而不是plyr)

时间:2015-08-08 18:44:03

标签: r plot ggplot2 dplyr plyr

我正在尝试从帖子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()

Pyramid plot with ggplot2 and plyr

工作正常。

但是如何用dplyr生成相同的情节呢?该示例在plyr语句中使用subset = .(g ==

我在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

3 个答案:

答案 0 :(得分:5)

使用最新版本的dplyr制作人口金字塔时,您可以避免使用plyrggplot2

如果您计算了年龄 - 性别群体的大小,请使用答案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()

enter image description here

答案 1 :(得分:4)

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

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() 

答案 2 :(得分:0)

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

shinyApp(ui = ui, server = server)

在geom_histogram()中更改binwidth可以将数据分为更广泛的类别。

enter image description here

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

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()
 

enter image description here