如何为列 ggplot 包含 geom_label?

How to include geom_label for a column ggplot?

你好,我有一些数据如下所示:

   `Marketing Variables`         LICENCE_STATUS count   pct val   
   <chr>                         <chr>          <lgl> <int> <chr> 
 1 RESIDENT_MARKETING_ALLOWED    ACTIVE         FALSE  7612 46.61%
 2 RESIDENT_MARKETING_ALLOWED    ACTIVE         TRUE   8720 53.39%
 3 RESIDENT_MARKETING_ALLOWED    CANCELLED      FALSE   418 52.05%
 4 RESIDENT_MARKETING_ALLOWED    CANCELLED      TRUE    385 47.95%
 5 RESIDENT_MARKETING_ALLOWED    EXPIRED        FALSE 78352 81.85%
 6 RESIDENT_MARKETING_ALLOWED    EXPIRED        TRUE  17373 18.15%
 7 THIRD_PARTY_MARKETING_ALLOWED ACTIVE         FALSE 10629 65.1% 
 8 THIRD_PARTY_MARKETING_ALLOWED ACTIVE         TRUE   5698 34.9% 
 9 THIRD_PARTY_MARKETING_ALLOWED CANCELLED      FALSE   571 70.93%
10 THIRD_PARTY_MARKETING_ALLOWED CANCELLED      TRUE    234 29.07%
11 THIRD_PARTY_MARKETING_ALLOWED EXPIRED        FALSE 85166 88.92%
12 THIRD_PARTY_MARKETING_ALLOWED EXPIRED        TRUE  10612 11.08%

我的目标是使用以下代码制作多面柱形图:

ggplot(test) +
  geom_col(aes(x = `Marketing Variables`, y = pct, fill = count), position = "fill") +
  coord_flip() +
  theme_minimal()+
  facet_wrap(~LICENCE_STATUS)

然而,问题是我想为每个柱添加变量 valgeom_label()。我希望显示的 val 是该变量的 TRUE 计数。

举个例子:

第一列是:

这里我们看到 count = TRUEval 为 34.9%。所以我希望 34.9% 是条形中的 34.9% 深度,或者基本上是蓝色与红色相遇的地方。

注意,我不希望 FALSE 的值出现在任何地方;是的,只有 true 和 false 是真实数据中 count 的选项。

这是数据:

structure(list(`Marketing Variables` = c("RESIDENT_MARKETING_ALLOWED", 
"RESIDENT_MARKETING_ALLOWED", "RESIDENT_MARKETING_ALLOWED", "RESIDENT_MARKETING_ALLOWED", 
"RESIDENT_MARKETING_ALLOWED", "RESIDENT_MARKETING_ALLOWED", "THIRD_PARTY_MARKETING_ALLOWED", 
"THIRD_PARTY_MARKETING_ALLOWED", "THIRD_PARTY_MARKETING_ALLOWED", 
"THIRD_PARTY_MARKETING_ALLOWED", "THIRD_PARTY_MARKETING_ALLOWED", 
"THIRD_PARTY_MARKETING_ALLOWED"), LICENCE_STATUS = c("ACTIVE", 
"ACTIVE", "CANCELLED", "CANCELLED", "EXPIRED", "EXPIRED", "ACTIVE", 
"ACTIVE", "CANCELLED", "CANCELLED", "EXPIRED", "EXPIRED"), count = c(FALSE, 
TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, 
TRUE), pct = c(7612L, 8720L, 418L, 385L, 78352L, 17373L, 10629L, 
5698L, 571L, 234L, 85166L, 10612L), val = c("46.61%", "53.39%", 
"52.05%", "47.95%", "81.85%", "18.15%", "65.1%", "34.9%", "70.93%", 
"29.07%", "88.92%", "11.08%")), row.names = c(NA, -12L), groups = structure(list(
    `Marketing Variables` = c("RESIDENT_MARKETING_ALLOWED", "RESIDENT_MARKETING_ALLOWED", 
    "RESIDENT_MARKETING_ALLOWED", "THIRD_PARTY_MARKETING_ALLOWED", 
    "THIRD_PARTY_MARKETING_ALLOWED", "THIRD_PARTY_MARKETING_ALLOWED"
    ), LICENCE_STATUS = c("ACTIVE", "CANCELLED", "EXPIRED", "ACTIVE", 
    "CANCELLED", "EXPIRED"), .rows = structure(list(1:2, 3:4, 
        5:6, 7:8, 9:10, 11:12), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

我正在专门寻找带有 `geom_label()'

的解决方案

EDIT 我无法使用链接解决方案的原因是出现以下错误:

Error in layer(data = data, mapping = mapping, stat = stat, geom = GeomText,  : 
  object 'val' not found

当我添加以下内容时会出现这种情况:

geom_text(label = val, y = pct, size = 2.5, position = position_stack(vjust = 0.5))+

您可以像这样实现您想要的结果:

  1. 至于 geom_label 中的 position="fill" 条也设置了。
  2. 如果 count=TRUE 只添加 val 使用 ifelseNA 分配给 FALSE
  3. 要消除有关删除丢失的警告,请使用 na.rm=TRUE
library(ggplot2)

ggplot(test, aes(x = pct, y = `Marketing Variables`, fill = count)) +
  geom_col(position = "fill") +
  geom_label(aes(label = ifelse(count, val, NA_character_)), position = "fill", na.rm = TRUE, show.legend = FALSE) +
  theme_minimal() +
  facet_wrap(~LICENCE_STATUS)