R 在堆叠条形图上绘制标签
我有需要放入堆栈条形图的数据,但当我添加计数的标签时,一些标签在类别上方,一些在类别下方。我试图修改geom_文本函数的位置参数,但没有效果 下面是一个可复制的示例,显示了该类别上方“下方”类别座位的标签以及酒吧内“上方”类别座位的标签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(
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")