如何显示 geom_bar 标签,其值为千到 "K" 而不是完整数字?
How do I display geom_bar labels with values of thousands to "K" instead of the full numbers?
所以我想出了如何使用 scale_y_continuous()
函数在 y 轴上显示带有“K”的数千个,但我不知道如何为我的每个标签做同样的事情。
ggplot(ctd_num_rides,
aes(x=factor(day_of_week,level=
c('Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday','Saturday',
'Sunday')),
y=number_of_rides,
fill=member_casual)) +
geom_bar(stat='identity',width=.65,position='dodge') +
scale_y_continuous(labels = label_number(suffix = " K", scale = 1e-3)) +
geom_text(aes(label=number_of_rides),
position=position_dodge(width=0.9), vjust=-0.25) +
labs(title='Number of Rides by Each User Type & Day of Week',
fill='User Type') +
xlab('Day of Week') +
ylab('Number of Rides')
以上代码returns可视化如下:
如您所见,每个标签的数字显示了完整的原始数字,但我希望它四舍五入到最接近的千位并显示 446K 而不是 445635,例如。
此外,是否可以添加连接每个条形的线条以直观地显示差异?这是我想象的,但真的不知道如何用语言表达:
> dput(ctd_num_rides)
structure(list(member_casual = c("casual", "casual", "casual",
"casual", "casual", "casual", "casual", "member", "member", "member",
"member", "member", "member", "member"), day_of_week = c("Friday",
"Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday",
"Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday",
"Wednesday"), number_of_rides = c(358203L, 289029L, 558617L,
477032L, 298061L, 270548L, 284868L, 453281L, 445635L, 442741L,
388042L, 485843L, 498682L, 506969L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), groups = structure(list(
member_casual = c("casual", "member"), .rows = structure(list(
1:7, 8:14), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
label_number
returns一个函数:
library(scales)
label_number(scale=1e-3, suffix='K')(123456.789)
## 123K
label_number(scale=1e-3, suffix='K')(123567.890)
## 124K
所以你应该可以使用:
geom_text(aes(label=label_number(scale=1e-3, suffix='K')(number_of_rides)))
对于你可以添加的行
+ stat_summary(aes(group = member_casual, color = member_casual),
fun = "identity", geom = "line")
所以我想出了如何使用 scale_y_continuous()
函数在 y 轴上显示带有“K”的数千个,但我不知道如何为我的每个标签做同样的事情。
ggplot(ctd_num_rides,
aes(x=factor(day_of_week,level=
c('Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday','Saturday',
'Sunday')),
y=number_of_rides,
fill=member_casual)) +
geom_bar(stat='identity',width=.65,position='dodge') +
scale_y_continuous(labels = label_number(suffix = " K", scale = 1e-3)) +
geom_text(aes(label=number_of_rides),
position=position_dodge(width=0.9), vjust=-0.25) +
labs(title='Number of Rides by Each User Type & Day of Week',
fill='User Type') +
xlab('Day of Week') +
ylab('Number of Rides')
以上代码returns可视化如下:
如您所见,每个标签的数字显示了完整的原始数字,但我希望它四舍五入到最接近的千位并显示 446K 而不是 445635,例如。
此外,是否可以添加连接每个条形的线条以直观地显示差异?这是我想象的,但真的不知道如何用语言表达:
> dput(ctd_num_rides)
structure(list(member_casual = c("casual", "casual", "casual",
"casual", "casual", "casual", "casual", "member", "member", "member",
"member", "member", "member", "member"), day_of_week = c("Friday",
"Monday", "Saturday", "Sunday", "Thursday", "Tuesday", "Wednesday",
"Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday",
"Wednesday"), number_of_rides = c(358203L, 289029L, 558617L,
477032L, 298061L, 270548L, 284868L, 453281L, 445635L, 442741L,
388042L, 485843L, 498682L, 506969L)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -14L), groups = structure(list(
member_casual = c("casual", "member"), .rows = structure(list(
1:7, 8:14), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -2L), .drop = TRUE))
label_number
returns一个函数:
library(scales)
label_number(scale=1e-3, suffix='K')(123456.789)
## 123K
label_number(scale=1e-3, suffix='K')(123567.890)
## 124K
所以你应该可以使用:
geom_text(aes(label=label_number(scale=1e-3, suffix='K')(number_of_rides)))
对于你可以添加的行
+ stat_summary(aes(group = member_casual, color = member_casual),
fun = "identity", geom = "line")