简体   繁体   中英

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?

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

在此输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM