Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - ggplot2 change x-axis values to non-log values

Tags:

r

ggplot2

axis

I am plotting some payment distribution information and I aggregated the data after scaling it to log-normal (base-e). The histograms turn out great but I want to modify the x-axis to display the non-log equivalents.

My current axis displays [0:2.5:10] values Alternatively, I would like to see values for exp(2.5), exp(5), etc.

Any suggestions on how to accomplish this? Anything I can add to my plotting statement to scale the x-axis values? Maybe there's a better approach - thoughts?

Current code:

ggplot(plotData, aes_string(pay, fill = pt)) + geom_histogram(bins = 50) + facet_wrap(~M_P)

enter image description here

Answered...Final plot: enter image description here

like image 867
Nikolkj Avatar asked Feb 08 '16 04:02

Nikolkj


People also ask

How do I change the X axis values in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I change my axis to log in R?

R functions to set a logarithmic axis: p + scale_x_log10(), p + scale_y_log10() : Plot x and y in log 10 scale, respectively. p + coord_trans(x = “log2”, y = “log2”): Transformed cartesian coordinate system.


1 Answers

Not sure if this is exactly what you are after but you can change the text of the x axis labels to whatever you want using scale_x_continuous.

Here's without:

ggplot(data = cars) + geom_histogram(aes(x = speed), binwidth = 1)

Here's with:

ggplot(data = cars) + geom_histogram(aes(x = speed), binwidth = 1) +
  scale_x_continuous(breaks=c(5,10,15,20,25), labels=c(exp(5), exp(10), exp(15), exp(20), exp(25)))
like image 132
Mist Avatar answered Oct 02 '22 22:10

Mist