【问题标题】:How do I show a group variable along with the numeric mid point on the axis of my plot?如何在绘图轴上显示组变量以及数字中点?
【发布时间】:2021-02-14 01:03:39
【问题描述】:

我有一些数据,这是其中的一个子集:

MyDataToSO <- data.frame(Age = c(2, 7, 12, 16, 21),
                     AgeGroup = c("0-4 years", "5-9 years", "10-14 years", "15-17 years", "18-24 years"),
                     Proportion = c(0.963, 0.965, 0.925, 0.701, 0.422))

我希望绘制数据,以便在 x 轴上获得相关的 AgeGroup 显示在 Age 刻度线下方。 Age 值是 AgeGroup 类别的中点。

我有我想要的情节,除了在 x 轴的相关部分下添加 AgeGroup 波段:

ggplot(data = MyDataToSO, aes(x = Age, y = Proportion)) +
geom_point() +
geom_point(data = subset(MyDataToSO, Age %in% c(16,21)), color = "green")
scale_x_continuous(breaks=seq(0, 30, by = 10)) +
labs(x = "Age group", y = "Proportion")

该图表有效,在正确的位置显示了相关的Age,但没有迹象表明Age 的值来自年龄组。

我认为通过在 x 轴上添加第二个标签来显示这一点会很有用,因此生成的 x 轴看起来有点像:

|
|______________________________...
      |         |         |    ...
      2         7         12   ...
|__________|_________|_________|...
 "0-4 years  5-9 years  10-14 years"...

我需要稍微调整一下字体大小才能使其正常工作。我还想让年龄组线条比正常打印更轻(例如,不透明比正常少 25%)。我在年龄组标签周围加上了引号,以阻止 SO 将每个数字显示为橙色数字。

如何将此信息添加到我的图表中?我搜索了二级标签,但只找到了与二级轴相关的问题。如您所见,所需的分组信息存储在AgeGroup 中,因此我“只”需要从那里提取相关值。

编辑:我加载了ggh4x 包,ggplot 代码现在是这样的:

ggplot(data = MyDataToSO, aes(interaction(Age, AgeGroup), Proportion)) +
geom_point() +
geom_point(data = subset(MyDataToSO, Age %in% c(16,21)), color = "green")
scale_x_continuous(breaks=seq(0, 30, by = 10)) +
guides(x = "axis_nested") +
labs(x = "Age group", y = "Proportion")

但它给出了一个错误,因为 x 轴是连续的。

编辑 2:绿点是插值。我现在有 17 到 20 岁的插值。但这些重复相同的 AgeGroup 标签。这是个问题吗?

【问题讨论】:

    标签: r ggplot2 axis-labels


    【解决方案1】:

    另一种方法是添加注释,关闭剪辑,并在轴文本和轴标题之间放置更多空间,如下所示:

    ggplot(data = MyDataToSO, aes(x = Age, y = Proportion)) +
      geom_point() +
      geom_point(data = subset(MyDataToSO, Age %in% c(16,21)), color = "green") +
    scale_x_continuous(breaks=seq(0, 30, by = 10)) +
      labs(x = "Age group", y = "Proportion") +
      annotate("rect", fill = "gray80",
               xmin = c(0, 5, 10, 15, 18),
               xmax = c(5, 10, 15, 18, 24) - 0.2,
               ymin = 0.28, ymax = 0.32) +
      annotate("text", size = 3,
               x = MyDataToSO$Age + 0.5,
               y = 0.3, label = MyDataToSO$AgeGroup) +
      coord_cartesian(ylim = c(0.4, 1), clip = "off") +
      theme(axis.title.x = element_text(margin = margin(t = 25, r = 0, b = 0, l = 0)))
    

    编辑:根据我对附加评论的理解,现在将 15:21 分开。

    MyDataToSO <- data.frame(Age = c(2, 7, 12, 15:21),
                             AgeGroup = c("0-4 years", "5-9 years", "10-14 years", 15:21),
                             Proportion = c(0.963, 0.965, 0.925, 0.701, .740, .677, .610, .540, .470, .401))
    
    
    ggplot(data = MyDataToSO, aes(x = Age, y = Proportion)) +
      geom_point() +
      geom_point(data = subset(MyDataToSO, Age %in% c(16,21)), color = "green") +
      scale_x_continuous(breaks=seq(0, 30, by = 10)) +
      labs(x = "Age group", y = "Proportion") +
      annotate("rect", fill = "gray80",
               xmin = c(0, 5, 10, 15:21) - 0.4,
               xmax = c(5, 10, 15, 16:22) - 0.6,
               ymin = 0.28, ymax = 0.32) +
      annotate("text", size = 3,
               x = MyDataToSO$Age,
               y = 0.3, label = MyDataToSO$AgeGroup) +
      coord_cartesian(ylim = c(0.4, 1), clip = "off") +
      theme(axis.title.x = element_text(margin = margin(t = 25, r = 0, b = 0, l = 0)))
    

    【讨论】:

    • 嗨 Jon,绿点是来自更大数据集的插值。我现在决定插入多个年龄(15-24 岁)。我在 x 轴标签上弄得一团糟,因为组现在重复了一些年龄而不是其他年龄。是否有此方法的扩展来解释某些具有重复组数据的行?例如,现在 16 到 21 岁的年龄有比例 (.740, .677, .610, .540, .470, .401) - 我更改了插值方法,16 和 21 的值发生了变化,但确切的值在这里没关系。
    • 不确定我是否理解。您想要多行重叠的组注释,还是某些年份的更详细的插值,或者其他什么?
    • 啊,我有。你的解释还是解决了!在 annotate("text" 下,我专门设置了 x 值和 y (x = c(2.5, 7.5, 12.5, 16.5, 21.5), y = -0.18, label = c("0-4", "5 -9”、“10-14”、“15-17”、“18-24”)。如果没有你,我永远不会成功地解决这一切。:)(我的年级高达 60-64,所以我不得不删掉“年”部分,否则文本最终会太小。
    【解决方案2】:

    ggh4x 包具有扩展 ggplot2 以更自动的方式执行此操作的功能(https://cran.r-project.org/web/packages/ggh4x/vignettes/PositionGuides.html,向下滚动到“嵌套关系”)。

    【讨论】:

    • 我无法将连续 x 变量嵌套在离散类别中。我得到“错误:提供给连续比例的离散值”。我已经处理过 x 轴是连续的,对年龄的离散处理会给我不正确的间距。您能否扩展您的答案,展示我将如何将包应用于我的问题?
    【解决方案3】:

    一种快速简便的方法是创建一个列表或变量,在其中附加来自MyDataToSO$AgeMyDataToSO$AgeGroup 的值,并用两个回车符(即\n)分隔。您将该列表/变量传递给 scale_x_continuous 的“标签”指令。

    library(tidyverse)
    
    MyDataToSO <- tibble(Age = c(2, 7, 12, 16, 21),
                         AgeGroup = c("0-4 years", "5-9 years", "10-14 years", "15-17 years", "18-24 years"),
                         Proportion = c(0.963, 0.965, 0.925, 0.701, 0.422)) %>% 
     mutate(custom_labels = paste0(Age, "\n\n", AgeGroup)) ## This is where you create the custom labels
    
    ggplot(data = MyDataToSO, aes(x = Age, y = Proportion)) +
        geom_point() +
        geom_point(data = subset(MyDataToSO, Age %in% c(16,21)), color = "green") + 
    scale_x_continuous(breaks=seq(0, 30, by = 10)) +
        labs(x = "Age group", y = "Proportion") +
        scale_x_continuous(breaks = c(MyDataToSO$Age), ## Here you pass the relevant ages. Should be aligned with the custom_labels
                           labels = c(MyDataToSO$custom_labels)) ## Here you pass the custom label balues
    

    【讨论】:

      猜你喜欢
      • 2018-06-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多