scales icon indicating copy to clipboard operation
scales copied to clipboard

Feature: global options for decimal.mark and big.mark

Open larmarange opened this issue 2 years ago • 1 comments
trafficstars

Hi,

would it be relevant to have a way to globally define (at the beginning of a script) global options regarding decimal and big marks, that then will be taken into account by all label_*() functions if nothing else is specified?

It could simplify the writing of statistical reports on other languages than English.

Best regards

larmarange avatar Dec 24 '22 10:12 larmarange

I support this!

I use options(OutDec = ",") to make sure I don't have to style everything every time I make a plot, with ggplot2. It works good, the axis do get commas, UNLESS I use a scales-function, e.g. scales::label_percent.

In example I use label_number as not to mix other things it would be good to have as global settings (suffix = " %").

library(ggplot2)
library(dplyr)
library(scales)

base_size <- theme_get()$text$size

p <- structure(
    list(gruppe = structure(
        c(1L,  2L, 3L),
        levels = c("Kjønn",
                   "Alder",
                   "Totalt"),
        class = c("ordered", "factor")
    ),
    value = c(
        5.4286181119209,
        8.9476515842644,
        5.4265163612462
    ),
    label = c(
        5.4,
        8.9,
        5.4
    )),
    row.names = c(NA,-3L),
    class = c("tbl_df",
              "tbl", "data.frame")
) %>% 
    ggplot(aes(x = value, y = gruppe)) +
    geom_col(position = "dodge") +
    geom_text(aes(label = label), 
              hjust = 0, 
              size = base_size * 0.3)

# if your options(OutDec = ".") this should print with decimal mark = .
p

# in other parts of the world, for some reason or the other doesn't matter, but we have to use comma as decimal marker
options(OutDec = ",")

# so the plot prints with commas in the numbers
p

# notice the axis when you add scales formatting of the axis
p +
    scale_x_continuous(labels = label_number())

EspenRosenquist avatar May 25 '23 12:05 EspenRosenquist