堆积条形图上的 R ggplot 标签

R ggplot labels on stacked bar chart

我有一些数据需要放入堆叠条形图中,但是当我添加计数标签时,一些标签在类别上方,一些在类别下方。我尝试修改 geom_text 函数的位置参数无济于事。

下面是一个可重现的示例,显示了位于该类别上方的 'Under' 类别座位的标签以及位于酒吧内的 'Over' 类别座位的标签。

library(tidyverse)

data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE),
DueDate = sample( 
         seq( as.Date("2015-01-01"), 
              as.Date("2015-06-30"), by="1 month") ,  
         6000,replace = TRUE),
             stringsAsFactors = TRUE) %>%
group_by(AgeGroup,DueDate) %>%
  tally() %>% ungroup %>% 
  ggplot() +
  geom_bar(aes(x=DueDate, y=n, fill = AgeGroup),stat = "identity") +
  geom_text(aes(x=DueDate, y=n
            ,label = prettyNum(n,big.mark = ","))
        , vjust = 0,  size = 2) +
  scale_y_continuous(labels = scales::comma) +
  theme_bw() +
  labs(title="Where are the labels")

下面是输出图表。

只要用n/2作为geom_text()y位置,它就会一直落在"inside"栏:

library(tidyverse)

data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE),
           DueDate = sample( 
               seq( as.Date("2015-01-01"), 
                    as.Date("2015-06-30"), by="1 month") ,  
               6000,replace = TRUE),
           stringsAsFactors = TRUE) %>%
    group_by(AgeGroup,DueDate) %>%
    tally() %>% ungroup %>% 
    ggplot() +
    geom_bar(aes(x=DueDate, y=n, fill = AgeGroup),stat = "identity") +
    geom_text(aes(x=DueDate, y=n/2
                  ,label = prettyNum(n,big.mark = ","))
              , vjust = 0,  size = 2) +
    scale_y_continuous(labels = scales::comma) +
    theme_bw() +
    labs(title="Where are the labels")

编辑:该快速解决方案仅适用于您的特定示例。如果每个栏有两个以上的类别,或者如果值分布更均匀,则它不会飞。即:

set.seed(999)
data.frame(Direction = sample(rep(c("South", "West", "East", "North")), 6000, replace = TRUE),
           DueDate = sample( 
               seq( as.Date("2015-01-01"), 
                    as.Date("2015-06-30"), by="1 month") ,  
               6000,replace = TRUE),
           stringsAsFactors = TRUE) %>%
    group_by(Direction, DueDate) %>%
    tally() %>% 
    ungroup %>%
    arrange(desc(Direction)) %>% 
    group_by(DueDate) %>% 
    mutate(pos = cumsum(n) - n/2) %>% 
    ggplot() +
    geom_bar(aes(x=DueDate, y=n, fill = Direction),stat = "identity") +
    geom_text(aes(x=DueDate, y=pos, label = prettyNum(n,big.mark = ","))
              , vjust = 0,  size = 2) +
    scale_y_continuous(labels = scales::comma) +
    theme_bw() +
    labs(title="Where are the labels")

所以这是一个通用的解决方案,它向数据框 (arrange(desc(Direction)) %>% group_by(DueDate) %>% mutate(pos = cumsum(n) - n/2)) 添加一个 "position" 列,以与 geom_text() 一起使用并将标签准确地放置在它们所属的位置:

set.seed(999)
data.frame(Direction = sample(rep(c("South", "West", "East", "North")), 6000, replace = TRUE),
           DueDate = sample( 
               seq( as.Date("2015-01-01"), 
                    as.Date("2015-06-30"), by="1 month") ,  
               6000,replace = TRUE),
           stringsAsFactors = TRUE) %>%
    group_by(Direction, DueDate) %>%
    tally() %>% 
    ungroup %>%
    arrange(desc(Direction)) %>% 
    group_by(DueDate) %>% 
    mutate(pos = cumsum(n) - n/2) %>% 
    ggplot() +
    geom_bar(aes(x=DueDate, y=n, fill = Direction),stat = "identity") +
    geom_text(aes(x=DueDate, y=pos, label = prettyNum(n,big.mark = ","))
              , vjust = 0,  size = 2) +
    scale_y_continuous(labels = scales::comma) +
    theme_bw() +
    labs(title="Where are the labels")

你可以试试:

geom_text(aes(label = n), position = position_stack(vjust = 0.5)