代码之家  ›  专栏  ›  技术社区  ›  user3357059

在堆积条形图上绘制标签

  •  1
  • user3357059  · 技术社区  · 6 年前

    我有需要放入堆栈条形图的数据,但是当我添加计数的标签时,有些标签在类别之上,有些在类别之下。我试图修改geomèu text函数的位置参数,但没有效果。

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

    下面是输出图表。 enter image description here

    1 回复  |  直到 6 年前
        1
  •  6
  •   HAVB    6 年前

    只是使用 n/2 y geom_text() ,它总是落在吧台的“内部”:

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

    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

    arrange(desc(Direction)) %>% group_by(DueDate) %>% mutate(pos = cumsum(n) - n/2) ),用于 几何文本() 把标签放在它们所属的地方:

    set.seed(999)
    data.frame(方向=样本(rep(c(“南”、“西”、“东”、“北”)),6000,replace=真),
    DueDate=样本(
    as.Date(“2015-06-30”),由=“1个月”),
    6000,替换=真),
    stringsAsFactors=真)%>%
    分组依据(方向,截止日期)%>%
    取消分组%>%
    排列(描述(方向))%>%
    分组依据(截止日期)%>%
    ggplot()+
    几何条形图(aes(x=截止日期,y=n,填充=方向),stat=“identity”)+
    ,vjust=0,size=2)+
    连续缩放(标签=缩放::逗号)+
    主题\u bw()+
    实验室(title=“标签在哪里”)
    

    enter image description here