r - 堆积条形图上的R ggplot标签
问题描述
我有需要放入堆栈条形图的数据,但是当我添加计数的标签时,一些标签在类别之上,而一些在类别之下。我尝试修改 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
用作 的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")
编辑:该快速解决方案仅适用于您的特定示例。如果每个条形图有两个以上的类别,或者值分布更均匀,则它不会飞。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")
推荐阅读
- python - 使用元组作为索引来比较列表中的项目
- augmented-reality - 如何检查WebXR的命中测试结果中检测到的平面是否垂直?
- python - 从 influxdb 读取数据时丢失精度
- visual-studio-code - VS Code 中的 Prettier 有很多麻烦
- rest - 了解基本级别的 GET/POST/DELETE 请求?
- c - 使用中断或线程和全局变量进行优化
- android-jetpack-compose - 在 Jetpack Compose 中将 Material3 颜色应用于 OutlinedTextField
- java - vs代码:java.rmi.ConnectException:连接拒绝主机:127.0.0.1
- sql-server - 如何在 SQL Server 2018 中调试存储过程
- c# - C# Windows 资源管理器文件预览