繁体   English   中英

堆积条形图上的 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位置,它就会始终落在条形“内部”:

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

在此处输入图片说明

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

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) ),到与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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM