Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在堆叠条形图上绘制标签_R_Ggplot2_Geom Text - Fatal编程技术网

R 在堆叠条形图上绘制标签

R 在堆叠条形图上绘制标签,r,ggplot2,geom-text,R,Ggplot2,Geom Text,我有需要放入堆栈条形图的数据,但当我添加计数的标签时,一些标签在类别上方,一些在类别下方。我试图修改geom_文本函数的位置参数,但没有效果 下面是一个可复制的示例,显示了该类别上方“下方”类别座位的标签以及酒吧内“上方”类别座位的标签 library(tidyverse) data.frame(AgeGroup = sample(c(rep("Over",10),"Under"), 6000, replace = TRUE), DueDate = sample( seq(

我有需要放入堆栈条形图的数据,但当我添加计数的标签时,一些标签在类别上方,一些在类别下方。我试图修改geom_文本函数的位置参数,但没有效果

下面是一个可复制的示例,显示了该类别上方“下方”类别座位的标签以及酒吧内“上方”类别座位的标签

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

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

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))%%>%groupby(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")

需要注意的是:使用
sample
导出的数据不能严格复制,除非您先使用
set.seed()
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")