强制散点图网格在 ggplot2 中为正方形
Force scatter plot grid to be square in ggpot2
我试图强制散点图的网格由正方形组成,其中 x 和 y 值具有不同的范围。
我试过强制整个plot为正方形(aspect.ratio=1),但这并没有解决不同范围的问题。然后我试图改变我的轴值的限制。
1)这是我首先尝试的:
p + theme(aspect.ratio = 1) +
coord_fixed(ratio=1, xlim = c(-0.050,0.050),ylim = c(-0.03,0.03))
2) 我通过使用每个轴的值范围更改了比率:
p + coord_fixed(ratio=0.06/0.10, xlim = c(-0.050,0.050), ylim = c(-0.03,0.03))
3) 然后我更改了 y 的限制以匹配 x:
p + theme(aspect.ratio = 1) +
coord_fixed(ratio=1, xlim = c(-0.050,0.050),ylim = c(-0.05,0.05))
1) 背景上的网格是由矩形组成的
2) 我希望这会自动更改刻度线的位置,以便给我一个由正方形组成的网格。还是三角形。
3) 它显然有效,因为我匹配了 x 和 y 的范围。但是图表中有很多空space。
还有什么我应该尝试的吗?
提前致谢。
我不确定您使用的是什么代码,它在块 1 和块 3 中丢失。但是使用 mtcars 数据集可以执行以下操作:
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_fixed(ratio = 1) +
scale_x_continuous(breaks = seq(10, 35, 1)) +
scale_y_continuous(breaks = seq(1, 6, 1))
最后两行清楚地表明 x-axis 上的 1 点等于 y-axis 上的 1 点。
在文档中您将进一步找到以下建议:
ensures that the ranges of axes are equal to the specified ratio by
adjusting the plot aspect ratio
如果您希望绘图是方形的并且您希望网格是方形的,您可以通过将 y 变量重新调整为与 x 变量相同的比例(反之亦然)来进行绘图,然后反转重新缩放以为重新缩放的轴生成正确的轴值标签。
这是一个使用 mtcars
数据框的示例,我们将使用 scales
包中的 rescale
函数。
首先让我们创建一个 mpg
对比 hp
的图,但是 hp
值重新调整为与 mpg
相同的比例:
library(tidyverse)
library(scales)
theme_set(theme_bw())
p = mtcars %>%
mutate(hp.scaled = rescale(hp, to=range(mpg))) %>%
ggplot(aes(mpg, hp.scaled)) +
geom_point() +
coord_fixed() +
labs(x="mpg", y="hp")
现在我们可以反转重新缩放以生成 hp
的正确值标签。我们在下面通过向 scale_y_continuous
的 labels
参数提供反转函数来做到这一点:
p + scale_y_continuous(labels=function(x) rescale(x, to=range(mtcars$hp)))
但请注意,重新缩放回原始 hp
比例会导致 non-pretty 中断。我们可以通过在 hp
尺度上生成漂亮的中断来解决这个问题,将它们重新缩放到 mpg
尺度以获得我们想要刻度线的位置,然后将其反转以获得标签值。然而,在那种情况下,如果我们想保持整个绘图面板是方形的,我们将不会得到方形网格:
p + scale_y_continuous(breaks = rescale(pretty_breaks(n=5)(mtcars$hp),
from=range(mtcars$hp),
to=range(mtcars$mpg)),
labels = function(x) rescale(x, from=range(mtcars$mpg), to=range(mtcars$hp)))
我试图强制散点图的网格由正方形组成,其中 x 和 y 值具有不同的范围。
我试过强制整个plot为正方形(aspect.ratio=1),但这并没有解决不同范围的问题。然后我试图改变我的轴值的限制。
1)这是我首先尝试的:
p + theme(aspect.ratio = 1) +
coord_fixed(ratio=1, xlim = c(-0.050,0.050),ylim = c(-0.03,0.03))
2) 我通过使用每个轴的值范围更改了比率:
p + coord_fixed(ratio=0.06/0.10, xlim = c(-0.050,0.050), ylim = c(-0.03,0.03))
3) 然后我更改了 y 的限制以匹配 x:
p + theme(aspect.ratio = 1) +
coord_fixed(ratio=1, xlim = c(-0.050,0.050),ylim = c(-0.05,0.05))
1) 背景上的网格是由矩形组成的
2) 我希望这会自动更改刻度线的位置,以便给我一个由正方形组成的网格。还是三角形。
3) 它显然有效,因为我匹配了 x 和 y 的范围。但是图表中有很多空space。
还有什么我应该尝试的吗?
提前致谢。
我不确定您使用的是什么代码,它在块 1 和块 3 中丢失。但是使用 mtcars 数据集可以执行以下操作:
library(ggplot2)
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
coord_fixed(ratio = 1) +
scale_x_continuous(breaks = seq(10, 35, 1)) +
scale_y_continuous(breaks = seq(1, 6, 1))
最后两行清楚地表明 x-axis 上的 1 点等于 y-axis 上的 1 点。
在文档中您将进一步找到以下建议:
ensures that the ranges of axes are equal to the specified ratio by adjusting the plot aspect ratio
如果您希望绘图是方形的并且您希望网格是方形的,您可以通过将 y 变量重新调整为与 x 变量相同的比例(反之亦然)来进行绘图,然后反转重新缩放以为重新缩放的轴生成正确的轴值标签。
这是一个使用 mtcars
数据框的示例,我们将使用 scales
包中的 rescale
函数。
首先让我们创建一个 mpg
对比 hp
的图,但是 hp
值重新调整为与 mpg
相同的比例:
library(tidyverse)
library(scales)
theme_set(theme_bw())
p = mtcars %>%
mutate(hp.scaled = rescale(hp, to=range(mpg))) %>%
ggplot(aes(mpg, hp.scaled)) +
geom_point() +
coord_fixed() +
labs(x="mpg", y="hp")
现在我们可以反转重新缩放以生成 hp
的正确值标签。我们在下面通过向 scale_y_continuous
的 labels
参数提供反转函数来做到这一点:
p + scale_y_continuous(labels=function(x) rescale(x, to=range(mtcars$hp)))
但请注意,重新缩放回原始 hp
比例会导致 non-pretty 中断。我们可以通过在 hp
尺度上生成漂亮的中断来解决这个问题,将它们重新缩放到 mpg
尺度以获得我们想要刻度线的位置,然后将其反转以获得标签值。然而,在那种情况下,如果我们想保持整个绘图面板是方形的,我们将不会得到方形网格:
p + scale_y_continuous(breaks = rescale(pretty_breaks(n=5)(mtcars$hp),
from=range(mtcars$hp),
to=range(mtcars$mpg)),
labels = function(x) rescale(x, from=range(mtcars$mpg), to=range(mtcars$hp)))