PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

[FIXED] How to delete decimals from labels in plots made with the scales package in R?

 August 07, 2022     decimal, format, ggplot2, r, scale     No comments   

Issue

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


Solution

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



Answered By - benson23
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing