如何使用log scale在ggplot2中设置轴范围? [英] How can I set axis ranges in ggplot2 when using a log scale?

查看:1024
本文介绍了如何使用log scale在ggplot2中设置轴范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个时间序列的数据,其中的测量值都是1e6和1e8之间的整数:每月网站点击次数。我想用ggplot2来绘制这些点和线,但将测量映射到一个对数刻度。像这样:

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

当我这样做时,ggplot似乎将比例从1e6设置为1e8。我希望它从0扩展到1e8。这样做的自然方式似乎对输出没有影响:

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

我可以得到我希望通过在达到qplot之前转换命中的图片,但是会更改轴上的标签:

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

我也尝试了各种与 scale_y_log10 的组合,但没有运气。 在ggplot2中使用对数刻度时,是否设置Y轴范围?

大部分 ggplot2 对我来说,如果不使用 qplot ,就会更加清晰。这样你就不会将所有东西都塞进一个函数调用中:
$ b $ pre $ 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))



并不意味着ay轴的最小值为0,因为在一个对数标度上,这个是有问题的。

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 a y axis minimum of 0, since on a log scale that, um, is problematic.

这篇关于如何使用log scale在ggplot2中设置轴范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆