1

Complete noob to R/RStudio/tidyverse. Using R 3.4.0 "You Stupid Darkness" / RStudio 1.0.136. Trying to format y axis to display thousands to 1 decimal place. I'm using : scale_y_continuous(labels = scales::unit_format("k", 1e-3)) but displays as a whole number. How can I display 1 decimal place so instead of 30k, I get 30.1k?

Thx

1
  • 1
    can you add a MWE? Jun 17, 2017 at 4:27

2 Answers 2

6

If you need something more flexible, I suggest using your own custom function and plugging it into scale_y_continuous like this:

library(ggplot2)

# custom formatting function
scaleFUN <- function(x) sprintf("%.1fk", x/1000)

# setup diamonds dataset to display something in the thousands
diamonds2 <- diamonds
diamonds2$price <- diamonds2$price * 100

# make your plot and label using the custom scaleFUN function
ggplot(diamonds2, aes(x = carat, y = price)) + 
  geom_point() + 
  scale_y_continuous(name = 'Price ($K)', 
                     labels = scaleFUN)
3

According to ?scales::unit_format, you can set accuracy = 0.1:

scale_y_continuous(labels = scales::unit_format(
  unit = "k", 
  scale = 1e-3,
  accuracy = 0.1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.