GGPLOT 将堆叠条形图中的标签与数据子集对齐

时间:2021-03-17 12:51:17

标签: r ggplot2 bar-chart

library(tidyverse)
library(ggplot2)
library(scales)

df <- data.frame("class" = rep("class_1", 13), 
                 "grade" = c("A+", "A", "A-",
                             "B+", "B", "B-",
                             "C+", "C", "C-",
                             "D+", "D", "D-", "F"),
                 "n" = c(24, 29, 28, 9, 12, 5, 1, 2, 5, 1, 1, 2, 4)) %>% 
  mutate(Percent = n/sum(n))


df %>% 
  ggplot(aes(x=class, y=Percent, fill=grade)) +
  geom_bar(position = "stack", stat = "identity", colour = "grey43") +
  scale_y_continuous(labels = scales::percent) +
  xlab("") +
  ylab("Percent")+
  geom_text(data = subset(df, Percent > 0.02),
            aes(y = Percent, label = paste0(round(Percent*100, 1),"%")),
            stat = "identity",
            size = 4,
            position = position_stack(vjust = 0.5),
            fontface = 2,
            family = "Courier New")

如果我不对数据进行子集化,则条形图的外观如下: enter image description here

这是我对数据进行子集化后的条形图的样子: enter image description here

正如您在子集代码中看到的:

data = subset(df, Percent > 0.02)

我正在尝试消除小于 2% 的条形段的标签,以免堵塞微小条形段中的所有标签。但是当我这样做时,其他条形段中的标签会移动。

我假设 position = position_stack(vjust = 0.5) 将标签放在了段的中间,但显然情况并非如此。标签位置似乎相互依赖彼此

如何让我的标签独立于可能已被淘汰的标签进行定位?

我尝试了 dplyr::filter(),但标签的行为方式与使用 subset() 时相同。

请注意,我正在尝试使用标签解决这个特定问题。我对组合级别不感兴趣,例如与forcats::fct_lump()

我也不想消除任何“等级”的因素水平。例如,我想展示一些学生在课堂上打了“D”,但出于审美原因,我想删除百分比标签。

1 个答案:

答案 0 :(得分:3)

作为对数据设置子集的替代方法,您可以将文本标签包含在 ifelse 语句中,该语句将小值替换为空字符串。

示例如下:

library(tidyverse)
library(ggplot2)
library(scales)

df <- data.frame("class" = rep("class_1", 13), 
                 "grade" = c("A+", "A", "A-",
                             "B+", "B", "B-",
                             "C+", "C", "C-",
                             "D+", "D", "D-", "F"),
                 "n" = c(24, 29, 28, 9, 12, 5, 1, 2, 5, 1, 1, 2, 4)) %>% 
  mutate(Percent = n/sum(n))


df %>% 
  ggplot(aes(x=class, y=Percent, fill=grade)) +
  geom_bar(position = "stack", stat = "identity", colour = "grey43") +
  scale_y_continuous(labels = scales::percent) +
  xlab("") +
  ylab("Percent")+
  geom_text(
    data = df,
    aes(y = Percent, 
        label = ifelse(Percent > 0.02, paste0(round(Percent*100, 1),"%"), "")),
    stat = "identity",
    size = 4,
    position = position_stack(vjust = 0.5),
    fontface = 2,
    family = "Courier New"
  )
#> Warning in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
#> font family not found in Windows font database

reprex package (v0.3.0) 于 2021 年 3 月 17 日创建