Home > database >  geom_text how to display no decimals when rounded to nearest thousands using scale=1e-3
geom_text how to display no decimals when rounded to nearest thousands using scale=1e-3

Time:05-12

My code is as below:

ggplot(ctd_monthly_num_rides,
       aes(x=factor(month,level=
                      c('January','February','March',
                        'April', 'May','June',
                        'July','August','September',
                        'October','November','December')),
           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=label_number(scale=1e-3,suffix='K')(number_of_rides)),
            position=position_dodge(width=0.5),vjust=-0.25,size=3.25)  
  labs(title='Number of Rides by Each User Type & Day of Week',
       fill='User Type')  
  xlab('')  
  ylab('Number of Rides')

This generates the following visual: enter image description here

I don't wish to display the decimals after rounding it since it looks a little difficult to look and follow the chart. I simply want it to show 442.06K to 442K, for example. I tried messing with digits= but that doesn't seem to help much...

CodePudding user response:

Try this..

  geom_text(
      aes(label= paste0(round(number_of_rides,0),"K")),
      position=position_dodge(width=0.5),
      vjust=-0.25,
      size=3.25) 

CodePudding user response:

A simple way to do this is to use number from scales package

paste0(scales::number(442.06), 'K')
  • Related