Back-to-back/population 在 ggplot2 中绘制

Back-to-back/population plot in ggplot2

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

在 S/O(例如 , here and here)上有多个创建这些图的示例,但这些示例中提供的解决方案中有 none 对我有用。我想知道这是否是由于这些问题在不久前被问及中间有多个 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()

并制作了这个情节:

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

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

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

这产生了一个接近我想要的情节:

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

非常感谢任何帮助

我错过了 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 轴的标签