[英]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. 我有一个时间序列的数据,其中测量是1e6和1e8之间的所有整数:每月网站点击量。 I want to use ggplot2 to chart these with points and lines, but mapping the measurements to a log scale.
我想使用ggplot2用点和线来绘制这些图表,但是将测量值映射到对数刻度。 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. 当我这样做时,ggplot似乎将比例从1e6设置为1e8。 I want it to scale from 0 to 1e8.
我希望它从0到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之前转换命中来获得我想要的图片,但这会更改轴上的标签:
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. 我也尝试过
scale_y_log10
各种组合,但没有运气。
So, how do I set the Y axis range when using a log scale in ggplot2? 那么,在ggplot2中使用对数刻度时,如何设置Y轴范围?
Much of ggplot2 is simply clearer to me if one doesn't use qplot
. 如果不使用
qplot
, ggplot2的大部分内容对我来说更加清晰。 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. 我假设你并不是真的意味着你的轴最小为0,因为在对数尺度上,嗯,是有问题的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.