-3

I created a histogram with ggplot2 in R and need a logarithmically spaced x-axis, but would like to keep my linear values.

Is this possible?

My formula so far is:

ggplot(f0peruttnq, aes(f0)) + geom_histogram(alpha=0.3, fill='white', colour='black') 

Slightly off-topic: I also tried to superimpose a normal curve on top of my histogram, but geom_density() doesn't seem to work if I want to keep my counts instead of density values on the x-axis. As I tried + stat_function( fun = dnorm ) nothing changed at all!

Thanks in advance for any useful tips!

It's working now!

Formula I used:

ggplot(data, aes(V2)) + geom_histogram(alpha=0.3, fill='white', colour='black')+scale_x_log10(breaks=c(50,100,150,200,250),labels=c(50,100,150,200,250)) 

Thanks for your patience :-)

3
  • 2
    Please post a reproducible example: stackoverflow.com/questions/5963269/… You'll get better help, and we'll be less grumpy :-) Commented Aug 6, 2011 at 9:41
  • Hm...basically I want a graph that looks like it was created from log values, just that there are linear values on the x-axis! This is my histogram: www7.pic-upload.de/06.08.11/rzr5ftih1a.jpg. It was created from log values and these values are on the x-axis. I want the same histogram, but created from linear values! The shape of the histogram should look like in the picture, but the labelling of the x-axis should be given in linear values! My formula so far is: ggplot(f0peruttnq, aes(f0))+geom_histogram(alpha=0.3, fill='white', colour='black') Commented Aug 6, 2011 at 9:52
  • Please edit your question to add in the formula that you have so far (push the {} code button with your code highlighted to mark your code as code) and the output of dput(f0peruttnq) and dput(f0). Commented Aug 6, 2011 at 10:09

2 Answers 2

5

Do you mean to not transform your data but only the plot? If so, it's easy:

Modified example from the ggplot2 help pages:

library(ggplot2) dat <- subset(movies, votes > 1000) m <- qplot(rating, votes, data=dat, na.rm = T) bks <- seq(min(dat$rating),max(dat$rating)) m + scale_x_log10(breaks=bks,labels=bks) 

plot

Sign up to request clarification or add additional context in comments.

9 Comments

I don't understand what you're asking for then. Do you want the scale to be wrong, since it would have linear labels and log values? Or do you want the X axis to be log transformed?
I want the x-axis to be log transformed with linear labels. If that doesn't work, then I want the scale to be wrong.
Like it is now? What do you mean by linear labels? 10,000 instead of 10^4 ?
The y-axis looks fine now, but I'd like to keep my values in Hz, which range from 60-400. So, yes!
Help us help you. Please edit your original question (click the "Edit" button under it) and provide the information I asked for above.
|
0

I'm not sure that this is possible with ggplot2 but if you're not absolutely tied to that library, I'm 99% sure you can do it with the axis() function of the standard graphics library.

With all due respect, and with no intention of creating a discussion here (we can take it to chat if you want), but isn't doing this misleading? The labels on the x-axis should reflect their actual values. Since you are log-transforming the data, the axis labels should reflect that transformation. To do otherwise would be scientifically dishonest.


Edited for an example of using the graphics library...not as pretty as ggplot2 but it does what you want (note: I was wrong about having to edit the histogram's breaks):

> data <- log(rnorm(10000, 100, 10)) #simulate some data that looks like yours > hist(data) # view a normal histogram of the data with log values on the x-axis > tick_locations = c(4.2, 4.4, 4.6, 4.8, 5.0) # copy the tick locations from the normal plot > tick_labels = exp(tick_locations) # reverse the log transformation; you can also create this manually > hist(data, xaxt = "n") # plot without the x-axis > axis(1, at = tick_locations, labels = tick_labels) # add the x-axis with the de-transformed values 

4 Comments

To clarify about using the graphics package: In your initial call to plot, use the argument xaxt="n" to suppress plotting the x-axis. Then call axis(side=1, at=vector_of_tick_positions, labels=vector_of_tick_labels) along with whatever other arguments you need.
Ah, sorry I referred to plot() but it should also work with hist(). You'll also have to specify where the bins are located though through the hist() function's argument breaks.
Doinng tick_labels = exp(tick_locations) results in long floating-point values. Probably better to do it manually like tick_labels = c(66.7, 81.5, 99.5, 121.5, 148.4)
Thanks! Works, but in contrast to the ggplot example from gsk3, there's no logarithmical spacing on the x-axis!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.