ggplot2 中的背靠背/人口图

时间:2021-05-19 04:32:54

标签: r ggplot2 plot geom-bar

我正在尝试使用 ggplot2 生成背靠背条形图(又名人口金字塔图)。下面是一个例子:

copied from PopulationPyramid.net

有多个在 S/O 上创建这些图的示例(例如,hereherehere),但这些示例中提供的解决方案都不适用于我.我想知道这是否是由于前一段时间提出了这些问题,中间有多个 ggplot 更新。

以下是我正在处理的数据示例:

dt = structure(list(age_grp = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 
4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), .Label = c("13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22"), class = "factor"), 
    sex = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 
    2L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L), .Label = c("Female", 
    "Male"), class = "factor"), percent = c(0.0407730571638261, 
    0.0536480686695279, 0.0652368914621218, 0.0268240343347639, 
    1.22319171491478, 1.07296137339056, 4.02360515021459, 4.89276685965914, 
    16.4967811158798, 19.5710674386366, 24.4638342982957, 20.118025751073, 
    17.9401451520835, 17.1673819742489, 13.0473782924244, 14.7532188841202, 
    13.412017167382, 10.6009948625948, 12.8755364806867, 8.15461143276523
    )), row.names = c(NA, -20L), class = c("data.table", "data.frame"
))

我第一次尝试使用以下代码制作金字塔图:

ggplot(dt, aes(x = age_grp, y = percent, fill = sex)) +
  geom_bar(data = subset(dt, sex == "Female"), stat = "identity") +
  geom_bar(data = subset(dt, sex == "Male"), stat = "identity") +
  coord_flip()

并产生了这个情节:

Bad plot

您可以看到男性填充刚刚与女性填充重叠,而不是女性填充被翻转。

为了解决这个问题,我使用了以下解决方法:

ggplot(dt, aes(x = age_grp, y = percent, fill = sex)) +
  geom_bar(data = subset(dt, sex == "Female"), aes(y = -percent), stat = "identity") +
  geom_bar(data = subset(dt, sex == "Male"), stat = "identity") +
  coord_flip()

这产生了一个接近我所追求的情节:

enter image description here

然而,现在的问题是“百分比”y 轴现在对于女性来说是负数。如何使男性和女性的 y 轴都为正?我确信这是一个简单的解决方法,但我遗漏了一些非常简单的东西!

非常感谢任何帮助

1 个答案:

答案 0 :(得分:1)

我错过了 this post! 中的解决方案

解决办法如下:

ggplot(dt, aes(x = age_grp, y = percent, fill = sex)) +
  geom_bar(data = subset(dt, sex == "Female"), aes(y = -percent), stat = "identity") +
  geom_bar(data = subset(dt, sex == "Male"), stat = "identity") +
  scale_y_continuous(breaks=seq(-20,20,5),labels=abs(seq(-20,20,5))) + 
  coord_flip()

只需要使用 scale_y_continuous 重新格式化 y 轴的标签