ggplot2中的简单人口金字塔

2024-05-23 14:10:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用ggplot2创建一个人口金字塔。这个问题是问before的,但我认为解决方法必须简单得多。

test <- (data.frame(v=rnorm(1000), g=c('M','F')))
require(ggplot2)
ggplot(data=test, aes(x=v)) + 
    geom_histogram() + 
    coord_flip() + 
    facet_grid(. ~ g)

生成此图像。在我看来,创建人口金字塔所缺少的唯一步骤是反转第一个面的x轴,使其从50变为0,同时保持第二个面不变。有人能帮忙吗?

Population pyramid


Tags: 方法testdatarequireframeaeshistogram人口
3条回答

一般金字塔密码

  1. 避免在水平轴上设置标签打断
  2. 只使用一个geom_*()而不使用子设置数据,如果要在方面图中创建多个棱锥体,这尤其有用。
  3. 使用geom_col()而不是geom_bar(),后者具有更好的默认值stat
  4. 具有相等的男性和女性水平轴;limits = max(d$pop) * c(-1,1),以便在x轴上比较性别和相等标签。

正在创建数据。。。

set.seed(100)
a <- seq(from = 0, to = 90, by = 10)
d <- data.frame(age = paste(a, a + 10, sep = "-"),
                sex = rep(x = c("Female", "Male"), each = 10),
                pop = sample(x = 1:100, size = 20))
head(d)
#     age    sex pop
# 1  0-10 Female  74
# 2 10-20 Female  89
# 3 20-30 Female  78
# 4 30-40 Female  23
# 5 40-50 Female  86
# 6 50-60 Female  70

绘图代码。。。

library(ggplot2)
ggplot(data = d, 
       mapping = aes(x = age, y = ifelse(test = sex == "Male", yes = -pop, no = pop), 
                     fill = sex)) +
    geom_col() +
    coord_flip() +
    scale_y_continuous(labels = abs, limits = max(d$pop) * c(-1,1)) +
    labs(y = "Population")

enter image description here

注意,如果你的数据是在个人层面上的,而不是按年龄性别组总结的,那么答案here也很一般。

扩展@gjabel的职位,这里是一个更干净的人口金字塔,同样使用ggplot2。

popPy1 <- ggplot(data = venDemo, 
   mapping = aes(
      x = AgeName, 
      y = ifelse(test = sex == "M",  yes = -Percent, no = Percent), 
      fill = Sex2,
      label=paste(round(Percent*100, 0), "%", sep="")
   )) +
geom_bar(stat = "identity") +
#geom_text( aes(label = TotalCount, TotalCount = TotalCount + 0.05)) +
geom_text(hjust=ifelse(test = venDemo$sex == "M",  yes = 1.1, no = -0.1), size=6, colour="#505050") +
#  scale_y_continuous(limits=c(0,max(appArr$Count)*1.7)) +
# The 1.1 at the end is a buffer so there is space for the labels on each side
scale_y_continuous(labels = abs, limits = max(venDemo$Percent) * c(-1,1) * 1.1) +
# Custom colours
scale_fill_manual(values=as.vector(c("#d23f67","#505050"))) +
# Remove the axis labels and the fill label from the legend - these are unnecessary for a Population Pyramid
labs(
  x = "",
  y = "",
  fill="", 
  family=fontsForCharts
) +
theme_minimal(base_family=fontsForCharts, base_size=20) +   
coord_flip() +
# Remove the grid and the scale
theme( 
  panel.grid.major = element_blank(), 
  panel.grid.minor = element_blank(),
  axis.text.x=element_blank(), 
  axis.text.y=element_text(family=fontsForCharts, size=20),
  strip.text.x=element_text(family=fontsForCharts, size=24),
  legend.position="bottom",
  legend.text=element_text(size=20)
)

popPy1

Population Pyramid

这是一个没有刻面的解决方案。首先,创建数据帧。我使用了从1到20的值来确保所有值都不是负的(对于人口金字塔,您不会得到负的计数/年龄)。

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

然后分别为每个g值组合两个geom_bar()调用。对于F计数按原样计算,但是对于M计数乘以-1得到相反方向的bar。然后使用scale_y_continuous()获取轴的漂亮值。

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

更新

由于参数subset=.在最新的ggplot2版本中被弃用,因此可以用函数subset()匹配相同的结果。

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

enter image description here

相关问题 更多 >