ggplot2:aspect.ratio 胜过 coord_equal 或 coord.fixed

ika*_*sky 5 r coordinates ggplot2

我希望收到一个具有相等坐标的方形图,无论我的数据点云的形状如何。

这是我的问题的原始说明。

xy <- data.frame(x = rnorm(100),
                 y = rnorm(100, mean = 1, sd = 2))
gg <- ggplot(xy,aes(x = x, y = y))+
        geom_point()
gg + coord_equal() #now we have square coordinate grid
gg + theme(aspect.ratio = 1) #now the plot is square

# if we use both, aspect.ratio overpowers coord_equal
gg + coord_equal() + theme(aspect.ratio = 1)  
Run Code Online (Sandbox Code Playgroud)

有什么办法可以让绘图和坐标网格都平方吗?当然,剧情中也会有一些空白的地方。我不介意这个。

另外,我想知道确保 x 轴和 y 轴上的相同标签的最简单方法。

hea*_*ien 5

您可以通过手动设置轴的比例来强制绘图为正方形:

gg +
scale_x_continuous(limits=c(min(xy$x,xy$y), max(xy$x,xy$y))) +
scale_y_continuous(limits=c(min(xy$x,xy$y), max(xy$x,xy$y))) +
theme(aspect.ratio=1)
Run Code Online (Sandbox Code Playgroud)