2

I am working with the R programming language.

I am trying to disable scientific notation from all plots produced using the ggplot library.

Normally, I use the following command to disable scientific notation :

options(scipen=999)

However, this above code does not seem to disable the scientific notation for plots such as the one below (e.g. in the legend):

g1 = ggplot() +
    geom_sf(data = st_trans,
            aes(fill = cut_number(min_distance, 9)),
            alpha = 0.8,
            colour = 'white',
            size = 0.3) +
    scale_fill_brewer(palette = "PuBu",
                      name = "Minimum Distance") +
    labs(x = NULL, y = NULL,
         title = "Minimum Distance",
         subtitle = "Source: Your Data",
         caption = "Your Caption") +
    theme(panel.background = element_blank(),
          line = element_blank()) +
    coord_sf(datum = NA) +
    scale_x_continuous(labels = scales::comma) +
    scale_y_continuous(labels = scales::comma)

enter image description here

Does anyone know if there is some "master command" that can disable the scientific notation in all plots produced using ggplot?

Thanks!

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

3

After a bit of digging it looks like the issue is with cut_number(), rather than with ggplot. The cut_number() function is a wrapper for base::cut.default and it has a 'default' scientific notation (i.e. it ignores whatever scipen you set). Luckily cut_number() accepts a "dig.lab" argument so you can change it to suit.

E.g. setting dig.lab to 50 (the max) should fix the issue:

library(ggplot2)

options(scipen = 999)

df <- data.frame(x = seq(1, 100000, 10000),
                 y = seq(5, 500000, 50000))

ggplot(df, aes(x = x, y = cut_number(y, 4))) +
  geom_point()


ggplot(df, aes(x = x, y = cut_number(y, 4, dig.lab = 50))) +
  geom_point()

Created on 2023-04-24 with reprex v2.0.2

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46
  • @ jared_mamrot: thank you so much for your answer! this works! just a question - for the dig.lab argument, is there some golden rule to remember? for example, if the biggest number I have is 999999 ... should I set dig.lab = 50? thank you so much! – stats_noob Apr 24 '23 at 06:52
  • If the biggest number you have is 999999 (6 digits) you can use `dig.lab = 7` and you'll get 'normal' notation. I tried "dig.lab = 100" and the function said "the max is 50", so I'm just going to use "50" in my own scripts in future, but if you want anything over N digits to be scientific notation, use "dig.lab = N". Does that make sense? – jared_mamrot Apr 24 '23 at 06:57