user10264295
user10264295

Reputation:

Percentage Label for R without decimals

In the process of creating a boxplot, I have percentages on the y-axis. However it shows up, for example, as 20.0% and I would prefer 20%. Does anybody know how to correct for this?

box<-ggplot(boxy,aes(x=type,y=value))+
    geom_boxplot()+
    scale_y_continuous(labels=percent)+ #where I am trying to fix the axis
    theme()
)

The answer found here: How do I change the number of decimal places on axis labels in ggplot2? does not make sense to me because of the notation of the function itself. Also, it is less intuitive than declaring the number of decimals in the scale part of ggplot

Data:

type<-c(rep("One",10),rep("Two",10))
value<-c(91,15,55,7,2,19,72,8,52,61,93,48,20,44,33,84,80,88,26,23)
boxy<-data.frame(type,value)

Upvotes: 0

Views: 3031

Answers (1)

Roman
Roman

Reputation: 17668

In your case you can simply paste the "%"

ggplot(boxy,aes(x=type,y=value))+
  geom_boxplot()+
  scale_y_continuous(labels=function(x) paste0(x,"%"))

enter image description here

As you can read here ?scale_y_continuous you can provide a function which "takes the breaks as input and returns labels as output". Input breaks (x), add "%", output labels.

Upvotes: 4

Related Questions