Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

r - How can I set axis ranges in ggplot2 when using a log scale?

I have a time series of data where the measurements are all integers between 1e6 and 1e8: website hits per month. I want to use ggplot2 to chart these with points and lines, but mapping the measurements to a log scale. Something like this:

qplot(month, hits, data=hits.per.month, log="y")

When I do that, ggplot seems to set the scale from 1e6 to 1e8. I want it to scale from 0 to 1e8. The natural way of doing this seems to have no affect on the output:

qplot(month, hits, data=hits.per.month, log="y", ylim=c(0, 100000000))

I can get the picture I want by transforming hits before it reaches qplot, but that changes the labels on the axis:

qplot(month, log10(hits), data=hits.per.month, log="y", ylim=c(0, 8))

I also tried various combinations with scale_y_log10, but had no luck.

So, how do I set the Y axis range when using a log scale in ggplot2?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Much of ggplot2 is simply clearer to me if one doesn't use qplot. That way you aren't cramming everything into a single function call:

df <- data.frame(x = 1:10,
                 y = seq(1e6,1e8,length.out = 10))

ggplot(data = df,aes(x = x, y =y)) + 
    geom_point() + 
    scale_y_log10(limits = c(1,1e8))

enter image description here

I'm going to assume you didn't really mean a y axis minimum of 0, since on a log scale that, um, is problematic.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...