【问题标题】:bold an item in axis label after using reorder_within使用 reorder_within 后将轴标签中的项目加粗
【发布时间】:2020-09-22 21:06:53
【问题描述】:

我正在使用 R 中 tidytext 包中的 reorder_within() 函数来绘制不同频率的图。一个类似的例子来自here

library(tidytext)

top_names %>%
    group_by(decade) %>%
    top_n(15) %>%
    ungroup %>%
    mutate(decade = as.factor(decade),
           name = reorder_within(name, n, decade)) %>%
    ggplot(aes(name, n, fill = decade)) +
    geom_col(show.legend = FALSE) +
    facet_wrap(~decade, scales = "free_y") +
    coord_flip() +
    scale_x_reordered() +
    scale_y_continuous(expand = c(0,0)) +
    labs(y = "Number of babies per decade",
         x = NULL,
         title = "What were the most common baby names in each decade?",
         subtitle = "Via US Social Security Administration")

输出图像是:

如何在每个方面将单个项目加粗?例如,如果我想在每个构面的轴标签上加粗大卫的名字,我该怎么做?

【问题讨论】:

标签: r ggplot2 tidytext


【解决方案1】:

与@tamtam 链接的答案中的解决方案相比,这是一个需要最小开销的解决方案:您可以使用ggtext::element_markdown 将ggplot 中的任何文本解释为降价。只需添加axis.text.y = ggtext::element_markdown(),所有具有"**Name**" 形式的变量名称都以粗体显示:

library(ggplot2)
library(dplyr)
library(tidytext)

test_data <- data.frame(
  decade = rep(c("1950", "1960", "1970", "1980"), each = 3),
  name = rep(c("Max", "**David**", "Susan"), 4),
  n = c(2, 1, 4, 5, 3, 1, 5, 7, 10, 4, 5, 3)
)


test_data %>%
  group_by(decade) %>%
  ungroup %>%
  mutate(decade = as.factor(decade),
         name = reorder_within(name, n, decade)) %>%
  ggplot(aes(name, n, fill = decade)) +
  geom_col(show.legend = FALSE) +
  facet_wrap(~decade, scales = "free_y") +
  coord_flip() +
  scale_x_reordered() +
  scale_y_continuous(expand = c(0,0)) +
  labs(y = "Number of babies per decade",
       x = NULL,
       title = "What were the most common baby names in each decade?",
       subtitle = "Via US Social Security Administration") +
  theme(axis.text.y = ggtext::element_markdown())

reprex package (v0.3.0) 于 2020 年 9 月 22 日创建

【讨论】:

    【解决方案2】:

    除了@starja 的出色示例之外,您还可以尝试使用glue 包在名称上构建条件,然后使用函数进行排序。关键是将标签从ggtext 格式化为element_markdown()。这里使用整个数据的代码:

    library(tidyverse)
    library(ggtext)
    library(glue)
    library(babynames)
    #Data
    top_names <- babynames %>%
      filter(year >= 1950,
             year < 1990) %>%
      mutate(decade = (year %/% 10) * 10) %>%
      group_by(decade) %>%
      count(name, wt = n, sort = TRUE) %>%
      ungroup
    #Plot
    top_names %>%
      group_by(decade) %>%
      top_n(15) %>%
      ungroup %>%
      mutate(name=ifelse(name=='David',glue("**{name}**"),name)) %>%
      mutate(decade = as.factor(decade),
             name = reorder_within(name, n, decade)) %>%
      ggplot(aes(name, n, fill = decade)) +
      geom_col(show.legend = FALSE) +
      facet_wrap(~decade, scales = "free_y") +
      coord_flip() +
      scale_x_reordered() +
      scale_y_continuous(expand = c(0,0)) +
      labs(y = "Number of babies per decade",
           x = NULL,
           title = "What were the most common baby names in each decade?",
           subtitle = "Via US Social Security Administration")+
      theme(axis.text.y = element_markdown())
    

    输出:

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2022-11-25
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      • 2018-02-10
      相关资源
      最近更新 更多