Home > Enterprise >  transform ggplot axes tick labels
transform ggplot axes tick labels

Time:10-25

I am trying to rescale gg plot axis labels with my function. For example,

sp <- ggplot(cars, aes(x = speed, y = dist))   geom_point()
sp

enter image description here

How can I rescale the x-axis labels with the given function exp(exp(x/100))? Specifically, instead of the current labels 5, 10, 15, 20, and 25, how can I display them as 2.86, 3.02, 3.20, 3.39, 3.61, at the locations of 5, 10, 15, 20, 25?

The values (2.86, 3.02, 3.20, 3.39, 3.61) come from the following transformation:

exp(exp(5/100))
exp(exp(10/100))
exp(exp(15/100))
exp(exp(20/100))
exp(exp(25/100))

I wish to let R do the calculation and change the displayed values on the axis, instead of us calculating the values and manually change the labels.

Thanks!

CodePudding user response:

use

 sp   scale_x_continuous(labels = ~round(exp(exp(./100)),2))

enter image description here

  • Related