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))
CodePudding user response:
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)))
CodePudding user response:
For the lines you can add
stat_summary(aes(group = member_casual, color = member_casual),
fun = "identity", geom = "line")