等坐标和正方形纵横比,对数刻度在ggplot中

问题描述 投票:0回答:1

我正在尝试使用对数标度轴和两个轴上相同的坐标以及长宽比绘制数据。

通过使用下面的代码,我得到一个矩形图:

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()

rectangular plot

现在,如果我尝试使用theme()功能将宽高比设为1:1,它将破坏等坐标系:

p + theme(aspect.ratio = 1)

square plot

是否有办法获得相等的缩放比例和纵横比?

r ggplot2 plot aspect-ratio
1个回答
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 )

enter image description here

如果您使用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
© www.soinside.com 2019 - 2024. All rights reserved.