Home > Blockchain >  How to delete decimals from labels in plots made with the scales package in R?
How to delete decimals from labels in plots made with the scales package in R?

Time:01-30

I made a plot where the fill bar is on a logarithmic scale. By default, labels appear in the format 1e 01, 1e 02, etc. I chose to modify the labels using the scales package, using the label_number() function. However, the values 0.1, 10, 1000 and 100000 appear with extra decimal places. Is there any way to show these numbers without the additional decimal places?

I would like a sequence like this: 0.001, 0.1, 10, 1000, and 100000.

library(ggplot2)
library(scales)

X <- 0
Y <- 0
Z <- 10

DF <- data.frame(X, Y, Z)

ggplot(data = DF,
       aes(x = X,
           y = Y,
           fill = Z))  
  geom_tile()  
  scale_fill_distiller(palette = "YlGnBu",
                       trans = 'log10',
                       limits = c(0.001, 100000),
                       labels = label_number(big.mark = ""))

enter image description here

CodePudding user response:

Take a look at ?label_number, you can specify arguments from base::format(). Here the trick is drop0trailing = T.

big.mark = "" is used to remove empty space in the thousandth position.

ggplot(data = DF,
       aes(x = X,
           y = Y,
           fill = Z))  
  geom_tile()  
  scale_fill_distiller(palette = "YlGnBu",
                       trans = 'log10',
                       limits = c(0.001, 100000),
                       labels = label_number(drop0trailing = T, big.mark = ""))

enter image description here

  •  Tags:  
  • Related