【问题标题】:How can I correctly position the geom_text labels to these geom_col data?如何将 geom_text 标签正确定位到这些 geom_col 数据?
【发布时间】:2018-06-13 19:23:10
【问题描述】:

我无法让标签正确地适合此图表中每个躲避的条:

我觉得我快到了,但不太清楚如何让标签完美地定位在每个躲避的条上。

代码:

ggplot() +
  geom_col(data = leads_over_chats, aes(x = date, y = count, fill = type),
           colour = "black",
           position = "dodge") +
  labs(title = "Leads Over Chats\n(May 2018)",
       x = "Type",
       y = "Count") +
  geom_text(data = leads_over_chats, aes(x = date, y = count, label = count),
            hjust = -1.5,
            vjust = -0.5,
            size = 4,
            angle = 90,
            position=position_dodge(width = 2.25),
            colour = "black")

我正在尝试复制这个(来自 Kibana):

可重现的数据框

structure(list(type = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 1L, 1L, 1L, 1L, 1L,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= structure(c(1525129200, 1525215600, 
1525302000, 1525388400, 1525474800, 1525561200, 1525647600, 1525734000, 
1525820400, 1525906800, 1525993200, 1526079600, 1526166000, 1526252400, 
1526338800, 1526425200, 1526511600, 1526598000, 1526684400, 1526770800, 
1526857200, 1526943600, 1527030000, 1527116400, 1527202800, 1527289200, 
1527375600, 1527462000, 1527548400, 1527634800, 1527721200, 1526166000, 
1526252400, 1526338800, 1526425200, 1526598000, 1526684400, 1526770800, 
1526857200, 1526943600, 1527030000, 1527116400, 1527202800, 1527289200, 
1527375600, 1527462000, 1527548400, 1527634800, 1527721200, 1526252400, 
1526338800, 1526425200, 1526511600, 1526598000, 1526684400, 1526770800, 
1526857200, 1526943600, 1527030000, 1527116400, 1527202800, 1527289200, 
1527375600, 1527462000, 1527548400, 1527634800, 1527721200, 1526252400, 
1526338800, 1526425200, 1526511600, 1526598000, 1526684400, 1526770800, 
1526857200, 1526943600, 1527030000, 1527116400, 1527202800, 1527289200, 
1527375600, 1527462000, 1527548400, 1527634800, 1527721200, 1525129200, 
1525215600, 1525302000, 1525388400, 1525474800, 1525561200, 1525647600, 
1525734000, 1525820400, 1525906800, 1525993200, 1526079600, 1526166000, 
1526252400, 1526338800, 1526425200, 1526511600, 1526598000, 1526684400, 
1526770800, 1526857200, 1526943600, 1527030000, 1527116400, 1527202800, 
1527289200, 1527375600, 1527462000, 1527548400, 1527634800, 1527721200, 
1526166000, 1526252400, 1526338800, 1526425200, 1526511600, 1526598000, 
1526684400, 1526770800, 1526857200, 1526943600, 1527030000, 1527116400, 
1527202800, 1527289200, 1527375600, 1527462000, 1527548400, 1527634800, 
1527721200, 1526252400, 1526338800, 1526425200, 1526511600, 1526598000, 
1526684400, 1526770800, 1526857200, 1526943600, 1527030000, 1527116400, 
1527202800, 1527289200, 1527375600, 1527462000, 1527548400, 1527634800, 
1527721200, 1526252400, 1526338800, 1526425200, 1526511600, 1526598000, 
1526684400, 1526770800, 1526857200, 1526943600, 1527030000, 1527116400, 
1527202800, 1527289200, 1527375600, 1527462000, 1527548400, 1527634800, 
1527721200, 1525129200, 1525215600, 1525302000, 1525388400, 1525474800, 
1525561200, 1525647600, 1525734000, 1525820400, 1525906800, 1525993200, 
1526079600, 1526166000, 1526252400, 1526338800, 1526425200, 1526511600, 
1526598000, 1526684400, 1526770800, 1526857200, 1526943600, 1527030000, 
1527116400, 1527202800, 1527289200, 1527375600, 1527462000, 1527548400, 
1527634800, 1527721200, 1525129200, 1525215600, 1525302000, 1525388400, 
1525474800, 1525561200, 1525647600, 1525820400, 1525906800, 1526079600, 
1526166000, 1526252400, 1526338800, 1526425200, 1526511600, 1526598000, 
1526684400, 1526857200, 1526943600, 1527030000, 1527116400, 1527289200, 
1527462000, 1527548400, 1527634800, 1526252400, 1526338800, 1526425200, 
1526511600, 1526598000, 1526684400, 1526857200, 1526943600, 1527030000, 
1527116400, 1527202800, 1527289200, 1527462000, 1527548400, 1527634800, 
1525129200, 1525215600, 1525302000, 1525388400, 1525734000, 1525820400, 
1525906800, 1525993200, 1526252400, 1526425200, 1526511600, 1526598000, 
1526684400, 1526857200, 1527030000, 1527116400, 1527202800, 1527289200, 
1527548400, 1527721200, 1525129200, 1525215600, 1525302000, 1525388400, 
1525474800, 1525647600, 1525820400, 1525906800, 1525993200, 1526079600, 
1526252400, 1526857200, 1526943600, 1527030000, 1527116400, 1527202800, 
1527462000, 1527548400, 1527634800, 1527721200, 1526857200, 1526943600, 
1527030000, 1527116400, 1527202800, 1527462000, 1527548400, 1527634800, 
1527721200, 1526252400, 1526684400, 1526857200, 1526943600, 1527030000, 
1527202800, 1527462000, 1527548400, 1526252400, 1526857200, 1526943600, 
1527030000, 1527548400, 1525215600, 1525302000, 1525388400, 1525474800, 
1525647600, 1525820400, 1525993200, 1526079600, 1526252400, 1526338800, 
1526511600, 1526598000, 1526684400, 1526770800, 1526857200, 1526943600, 
1527030000, 1527116400, 1527289200, 1527462000, 1527634800, 1527721200, 
1526425200, 1526857200, 1526943600, 1527202800, 1526425200, 1527202800, 
1526425200, 1526943600, 1527202800, 1526857200, 1527202800, 1526338800, 
1526425200, 1526943600, 1527116400, 1527289200, 1527721200, 1525734000, 
1526338800, 1526425200, 1526943600, 1527116400, 1527202800, 1527289200, 
1527202800, 1525302000, 1525388400, 1525734000, 1525820400, 1525993200, 
1526511600, 1526857200, 1526943600, 1527116400, 1527462000, 1527548400, 
1525129200, 1525215600, 1525561200, 1525734000, 1525906800, 1526079600, 
1526252400, 1526857200, 1526943600, 1527375600, 1525302000, 1525474800, 
1525993200, 1526425200, 1527030000, 1525215600, 1525734000, 1526425200, 
1526857200, 1527548400, 1525734000, 1526943600, 1525906800, 1526943600, 
1526252400, 1526338800, 1525215600, 1525820400, 1526252400, 1527202800, 
1525215600, 1526338800, 1526511600, 1526857200, 1525129200, 1527116400
), class = c("POSIXct", "POSIXt"), tzone = ""), count = c(76L, 
82L, 64L, 59L, 70L, 30L, 42L, 54L, 61L, 77L, 83L, 79L, 92L, 120L, 
99L, 145L, 2L, 70L, 88L, 79L, 119L, 101L, 133L, 147L, 177L, 96L, 
93L, 108L, 137L, 112L, 107L, 15L, 89L, 68L, 85L, 34L, 45L, 44L, 
64L, 54L, 54L, 62L, 114L, 52L, 31L, 54L, 56L, 57L, 45L, 27L, 
20L, 33L, 1L, 25L, 29L, 21L, 29L, 26L, 41L, 54L, 51L, 32L, 33L, 
33L, 51L, 33L, 36L, 23L, 28L, 46L, 2L, 24L, 24L, 18L, 38L, 36L, 
47L, 44L, 39L, 24L, 37L, 27L, 50L, 29L, 39L, 6L, 10L, 2L, 11L, 
1L, 5L, 9L, 8L, 21L, 17L, 18L, 24L, 8L, 20L, 19L, 22L, 22L, 20L, 
21L, 16L, 26L, 23L, 23L, 19L, 36L, 17L, 14L, 31L, 33L, 28L, 28L, 
3L, 18L, 7L, 9L, 12L, 13L, 1L, 4L, 13L, 8L, 8L, 6L, 18L, 8L, 
4L, 3L, 15L, 13L, 16L, 5L, 11L, 9L, 5L, 10L, 9L, 6L, 7L, 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 1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-398L))

【问题讨论】:

  • @MrFlick 包括可重现的示例。问题中解释了所需的输出。我尝试了无数 group = x 变体,但无济于事。
  • 这里有这么多条和标签,你怎么能看到发生了什么?期望的输出应该是什么样子(我怎么知道我是否做对了)?
  • @MrFlick 问题已更新。我正在尝试复制我在 Kibana 中获得的图表。

标签: r position geom-text geom-col


【解决方案1】:

首先,您需要在type + date 分组中汇总您的数据。当前每个日期都有多个 count 记录。然后,我可能会为这种类型的视觉推荐在闪避条上使用折线图:

library(tidyverse)

df <- leads_over_chats

df %>%
  mutate(date = as.Date(date)) %>%
  group_by(type, date) %>%
  summarise(total_count = sum(count)) %>%
  ggplot(., aes(date, total_count, color = type)) +
  geom_line()

如果您真的想要一个闪避的条形图,您需要先将type 转换为factor,然后利用tidyr::complete 使所有条形保持相同的宽度:

df %>%
  mutate(date = as.Date(date),
         type = as.factor(type)) %>%
  group_by(type, date) %>%
  complete(type, date) %>%
  summarise(total_count = sum(count)) %>%
  ggplot(., aes(date, total_count, fill = type)) +
  geom_col(position = position_dodge())

鉴于您的评论,您可能还想考虑利用 patchwork 与“总和”图表,然后是突破。比如:

library(patchwork)

df_grouped_and_summed <-
  df %>%
  mutate(date = as.Date(date),
         type = as.factor(type)) %>%
  group_by(type, date) %>%
  complete(type, date) %>%
  summarise(total_count = sum(count))

p_created <- 
  ggplot(df_grouped_and_summed, aes(date, total_count)) +
  geom_col() +
  labs(x = "", y = "")

p_splits <-
  ggplot(df_grouped_and_summed %>% filter(type != "conversation-created"),
         aes(date, total_count, fill = type)) +
  geom_col() +
  facet_wrap(~ type, ncol = 1) +
  labs(x = "", y = "") +
  guides(fill = FALSE)

p_created + p_splits

最后,如果它只是一个突破,那么您也可以使用堆叠柱 - 但是,您会注意到(至少在提供的数据中)部分的总和不等于总数:

df_grouped_and_summed %>%
  filter(type != "conversation-created") %>%
  ggplot(., aes(date, total_count, fill = type)) +
  geom_col()

【讨论】:

  • 这是一个不错的解决方案,但我选择使用列,因为三个较小列的值都对应于conversation-created 列;最终,我想展示该列是如何构建的——考虑一下我将数据从conversation-created 列中拆分出来。
  • 是的,这正是我想要的,还有更多 - 非常感谢您的努力。我有一个一般性的后续问题:.ggplot(., aes(date, total_count, fill = type)) 中是什么意思?就没有完全加起来的数字而言,这很好,因为允许诸如原产地和本地区域之间的时区差异之类的事情有很小的余量。
  • 随时。 . 是使用 pipe 运算符 %&gt;% 的参数占位符。简而言之,f(x)可以改写为x %&gt;% f——f(x, y)可以改写为x %&gt;% f(y)。我可以把它写成ggplot(data = ., aes(date, total_count...etc.,甚至完全去掉这个点。为了清楚起见,我喜欢使用它。查看更多here。最后——确保你根据你选择的方法观察你的坐标轴比例!您可能需要手动设置限制/比例。
  • 太棒了,非常感谢你!另外,考虑到与我的原始代码结构不同的布局,如何向这些列添加文本标签?
  • 您应该能够将aes(..., label = total_count)` 添加到第一个ggplot(),然后只需添加+ geom_text()——但我会质疑添加文本值是否会给可视化添加任何内容。如果人们要求“价值”,那么他们可能只需要一个数据表而不是可视化——可视化应该让您以清晰简洁的方式快速消化信息。我也会说,“尝试一些事情”,然后如果您无法弄清楚或找到一个足够相似的问题来适应,请发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
  • 2021-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
相关资源
最近更新 更多