-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 :-)

Bloomy
  • 2,103
  • 4
  • 17
  • 9
  • 2
    Please post a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example You'll get better help, and we'll be less grumpy :-) – Ari B. Friedman Aug 06 '11 at 09: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: http://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') – Bloomy Aug 06 '11 at 09: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)`. – Ari B. Friedman Aug 06 '11 at 10:09

2 Answers2

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

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • 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? – Ari B. Friedman Aug 06 '11 at 10:07
  • 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. – Bloomy Aug 06 '11 at 10:14
  • Like it is now? What do you mean by linear labels? 10,000 instead of 10^4 ? – Ari B. Friedman Aug 06 '11 at 10:16
  • The y-axis looks fine now, but I'd like to keep my values in Hz, which range from 60-400. So, yes! – Bloomy Aug 06 '11 at 10:19
  • Like now? See this question for more detail: http://stackoverflow.com/questions/4699493/transform-only-one-axis-to-log10-scale-with-ggplot2 – Ari B. Friedman Aug 06 '11 at 10:29
  • This is exactly what I want! The problem is, when I try `ggplot(f0peruttnq, aes(f0)) + geom_histogram(alpha=0.3, fill='white', colour='black')+ scale_x_log10( breaks=breaks,labels = breaks)`, I get the error `Error in match.fun(get(".transform", .))(values) : non-numerical argument for mathematical function` What do I have to do to transform my 10^x values to normal integers? – Bloomy Aug 06 '11 at 10:58
  • 2
    Help us help you. Please edit your original question (click the "Edit" button under it) and provide the information I asked for above. – Ari B. Friedman Aug 06 '11 at 11:06
  • I added it, sorry for the chaotic formatting, I didn't know how to change that! Also, I didn't get the beginning of the output, probably because I have thousands of values. – Bloomy Aug 06 '11 at 11:34
  • We'd need the whole thing. The purpose of dput() is that it lets us re-create your objects in our R, to play with them. We'd only need the first few rows though. Try `head(dput(f0peruttnq))` and `head(dput(f0))` – Ari B. Friedman Aug 06 '11 at 11:51
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
  • 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. –  Aug 06 '11 at 11:09
  • 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`. –  Aug 06 '11 at 11:12
  • 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)` –  Aug 06 '11 at 11:41
  • Thanks! Works, but in contrast to the ggplot example from gsk3, there's no logarithmical spacing on the x-axis! – Bloomy Aug 06 '11 at 13:59