Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Disabling Scientific Notation In ggplot

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading