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)
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