R ggplot 堆叠条形图上的标签

3
我有一些需要放入堆叠条形图的数据,但是当我添加计数标签时,有些标签在类别上方,有些在类别下方。我尝试修改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")

以下是输出图表。 在这里输入图像描述

1
请注意:除非您首先使用 set.seed(),否则使用 sample 生成的数据并不是严格可再现的。 - neilfws
2个回答

6

只需将geom_text()y位置设为n/2,它就会始终落在柱内:

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")

这里有一个通用解决方案,它向数据框添加了一个“position”列(arrange(desc(Direction)) %>% group_by(DueDate) %>% mutate(pos = cumsum(n) - n/2)),可与geom_text()一起使用,并将标签放置在其应该出现的位置:

enter image description here

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")

enter image description here


2
你可以尝试以下方法:
geom_text(aes(label = n), position = position_stack(vjust = 0.5)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接