默认情况下,ggplot2
将根据您的数据进行扩展。但大多数scale_xxx
函数提供了一个limits
您可以微调的参数。使用你上面的例子
p <- qplot(x, y) +
scale_x_continuous(trans = "log2", limits=c(8,128)) +
scale_y_continuous(trans = "log2", limits=c(8,128)) +
coord_equal()
p + theme( aspect.ratio=1 )
您也不需要提供theme
如果你用过coord_fixed()
代替coord_equal()
.
p <- qplot(x, y) +
scale_x_continuous(trans = "log2", limits=c(8,128)) +
scale_y_continuous(trans = "log2", limits=c(8,128)) +
coord_fixed( ratio=1 )
p
EDIT #1
没看到你们其中一个随机x点小于8。修改代码
p <- qplot(x, y) +
scale_x_continuous(trans = "log2", limits=c(4,128)) +
scale_y_continuous(trans = "log2", limits=c(4,128)) +
coord_fixed( ratio=1)
p
EDIT #2(根据问题中的额外改写)
如果您希望根据您的数据缩放范围,只需使用range()
基本 R 中的函数。因此使用上述变量...
xy.limits <- range( c(x,y) )
p <- qplot(x, y) +
scale_x_continuous(trans = "log2", limits=xy.limits) +
scale_y_continuous(trans = "log2", limits=xy.limits) +
coord_fixed( ratio=1)
p
现在,如果您希望限制为 2 的干净倍数,那么您将必须根据计算值进行一些数据操作xy.limits
,它是长度为 2 的数值向量。