简体   繁体   English

使用对数刻度时,如何在ggplot2中设置轴范围?

[英]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 . 如果不使用qplotggplot2的大部分内容对我来说更加清晰。 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.

相关问题 使用geom_tile时,如何控制ggplot2中分类数据的y轴刻度? - How can I control the y-axis scale for categorical data in ggplot2 when using geom_tile? 如何使用ggplot2在时间间隔上缩放轴? - How do I scale an axis for time intervals using ggplot2? 使用 scale_x_log10 时,如何在 geom_histogram 中设置 ggplot2 binwidth? - How do I set ggplot2 binwidth in geom_histogram when using scale_x_log10? ggplot2 中轴的对数刻度和更改域 - Log scale & change domain for axis in ggplot2 使用 ggplot2 的 plot_usmap 时,如何将比例设置为静态窃取动态 - How do I set the scale to be static insteal of dynamic when using ggplot2's plot_usmap 在ggplot2上使用百分比刻度时如何设置间隔? - How do I set breaks when using a percentage scale on ggplot2? 当我在 R ggplot2 中绘制缺失的日期时,我如何调整 X 轴的比例并将中断设为整数 - How I can adjust the scale of axis X with breaks as.integer when I graph missing dates in R ggplot2 当我使用 plot 连续颜色使用 ggplot2 时,如何修复图例中缺少的色标? - How can I fix missing color scale in the legend when I plot continuous color using ggplot2? 在ggplot2中,使用两种图形类型时如何缩放图例? - In ggplot2 how can I scale the legend when using two graph types? 如何使用ggplot2在y轴上缩放因子 - how to scale factors in y-axis using ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM