我如何显示具有数千个值的Geom_bar标签为“ k&quot”而不是全部数字?
因此,我想出了如何使用scale_y_continuun()
函数的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')
以上代码返回以下视觉效果:
例如。
另外,是否可以添加连接每个栏的线路以视觉显示差异?这是我想象的,但真的不知道如何将其放入文字中:
> 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))
So I figured out how to display thousands with "K" for the y-axis with the scale_y_continuous()
function but I don't know how I could go about doing the same for each of my labels.
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')
The above code returns the following visual:
As you can see, the numbers for each labels show the entire raw numbers but I want it to be round up to the nearest thousand and show 446K instead of 445635, as an example.
Also, is it possible to add lines connecting each of the bars to show the differences visually? This is what I'm imagining but don't really know how to put it into words:
> 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))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
label_number
返回函数:因此您应该能够使用:
label_number
returns a function:so you should be able to use:
对于行,您可以添加
For the lines you can add