繁体   English   中英

调整 facet_share 人口金字塔的轴限制

[英]Adjust axis limits on facet_share population pyarmid

我使用 geom_bar 和 facet_share 创建了一个人口金字塔。 但是,数据标签不适合具有当前轴限制或面板大小的面板。

我试过强制面板大小,但这只会按比例增加所有内容的大小。 我也试过设置 ylim,但这会增加两个方向的轴。

我希望面板中有足够的空间让数据标签可见。

#Here is the data

AgeGroupDat <-
structure(list(sex = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = c("Male", "Female"), class = "factor"), Age_Group = c("00 - 20", "16 - 38",                                                                                         "18 - 34", "20 - 41", "32 - 54", "41 - 63", "48 - 69", "50 - 73",                                                                                                                                         "62 - 86", "76 - 98", "00 - 20", "16 - 38", "18 - 34", "20 - 41",                                                                                                                                  "32 - 54", "41 - 63", "48 - 69", "50 - 73", "62 - 86", "76 - 98"                                                                                 ), n = c(19318, 19050, 13948, 17416, 13893, 12495, 11038, 10813,                                                                                      4751, 612, -19915, -15443, -11091, -13599, -11969, -11525, -10442,                                                                                              -10119, -4124, -367)), row.names = c(NA, -20L), class = c("tbl_df",                                                                                                                                                   "tbl", "data.frame"))
#And here is the plot

AgeGroupDat %>% 
  ggplot(aes(x = Age_Group, y = n, fill = sex))+
  geom_bar(stat = "identity")+
  geom_text(data = subset(AgeGroupDat,
                          sex == "Female"),
            aes(label = abs(n)),
            hjust = -0.3,
            size = 3,
            #position = position_stack(vjust = -.01)
  )+ 
  geom_text(data = subset(AgeGroupDat,
                          sex == "Male"),
            aes(label = abs(n)),
            hjust = 1.2,
            size = 3,
            #position = position_stack()
  )+
  coord_flip()+
  facet_share(~sex, dir = "h", scales = "free", reverse_num = TRUE)+
  theme_classic()+
  scale_fill_manual(values = c("#9a0138","#000775"))+
    labs(title = "test")+
  labs(subtitle = "test")+
  labs(caption = "test")+
  labs(y = NULL)+
  labs(x = NULL)+
  labs(fill = "Member Sex")+
  theme(legend.position = "none")+
  ylim(-25000, 25000)

实现所需结果的一种选择是切换到ggh4x package,它通过facetted_pos_scales允许为每个面板单独设置比例。 但是,这样做需要手动删除其中一个y刻度的轴文本。 此外,为了很好地居中标签,我们必须通过panel.spacing.x = unit(0, "pt")删除面板间距,并为y轴文本设置相同的边距。

library(ggplot2)
library(ggh4x)

ggplot(AgeGroupDat, aes(y = Age_Group, x = n, fill = sex)) +
  geom_bar(stat = "identity") +
  geom_text(
    aes(label = abs(n), hjust = ifelse(sex == "Female", -.3, 1.3)),
    size = 3
  ) +
  scale_fill_manual(values = c(Female = "#9a0138", Male = "#000775"), drop = FALSE) +
  facet_wrap(~sex, scales = "free") +
  facetted_pos_scales(
    x = list(
      scale_x_continuous(
        labels = abs, expand = c(0, 2500, 0, 0),
        limits = c(-20000, 0)
      ),
      scale_x_continuous(
        expand = c(0, 0, 0, 2500),
        limits = c(0, 20000)
      )
    ),
    y = list(
      scale_y_discrete(position = "right"),
      scale_y_discrete(labels = NULL)
    )
  ) +
  labs(
    x = NULL, y = NULL, fill = "Member Sex",
    title = "test",
    subtitle = "test",
    caption = "test"
  ) +
  theme_classic() +
  theme(
    axis.text.y.right = element_text(margin = margin(0, 2.2, 0, 2.2)),
    legend.position = "bottom",
    panel.spacing.x = unit(0, "pt")
  )

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM