只是使用
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")
编辑:快速解决方案只适用于您的特定示例。如果每个栏有两个以上的类别,或者这些值分布更均匀,则不会飞。即。:
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)
),用于
几何文本()
把标签放在它们所属的地方:
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=“标签在哪里”)