我正在尝试使用对数标度轴和两个轴上相同的坐标以及长宽比绘制数据。
通过使用下面的代码,我得到一个矩形图:
set.seed(1)
x <- rnorm(30, 50, 20)
y <- x + 20 + rnorm(30, 0, 10)
p <- qplot(x, y) +
scale_x_continuous(trans = "log2") +
scale_y_continuous(trans = "log2") +
coord_equal()
现在,如果我尝试使用theme()
功能将宽高比设为1:1,它将破坏等坐标系:
p + theme(aspect.ratio = 1)
是否有办法获得相等的缩放比例和纵横比?
默认情况下,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