Search code examples
rggplot2percentagefloating-accuracy

remove percentage sign from scale::percent() in R


This is my code.

my_boxplot <- ggplot(mtcars,aes(x=as.factor(cyl),y=mpg)) + 
  geom_boxplot(aes(fill=cyl,color=cyl)) + xlab('Cylinders') + ylab('Miles per Gallon %')+
  scale_y_continuous(labels = function(x) scales::percent(x, accuracy = 0.01))
my_boxplot

This code shows the y-axis as percentages and I want to keep the accuracy to 2 decimal places and remove the percentage sign.

I tried this too. It only removes the percentage sign but does not set the accuracy.

scale_y_continuous(labels=function(x) paste0(x*100)) Does anybody know how to keep the accuracy to desired decimal places and remove percentage sign from percentage values? Thanks for any help!


Solution

  • try :

    my_boxplot <- ggplot(mtcars,aes(x=as.factor(cyl),y=mpg)) + 
      geom_boxplot(aes(fill=cyl,color=cyl)) + xlab('Cylinders') + ylab('Miles per Gallon %')+
      scale_y_continuous(labels = function(x) format(x, digits=2, nsmall=2))
    my_boxplot
    

    enter image description here