我如何显示具有数千个值的Geom_bar标签为“ k&quot”而不是全部数字?

发布于 2025-01-27 16:28:43 字数 2027 浏览 1 评论 0原文

因此,我想出了如何使用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:

enter image description here

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:

enter image description here

> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离不开的别离 2025-02-03 16:28:43

label_number返回函数:

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)))

label_number returns a function:

library(scales)
label_number(scale=1e-3, suffix='K')(123456.789)
## 123K
label_number(scale=1e-3, suffix='K')(123567.890)
## 124K

so you should be able to use:

geom_text(aes(label=label_number(scale=1e-3, suffix='K')(number_of_rides)))
小鸟爱天空丶 2025-02-03 16:28:43

对于行,您可以添加

+ stat_summary(aes(group = member_casual, color = member_casual),
fun = "identity", geom = "line")

For the lines you can add

+ stat_summary(aes(group = member_casual, color = member_casual),
fun = "identity", geom = "line")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文