我正在尝试在 R 中以 2 x 2 的排列方式绘制四个散点图(我实际上是通过 rpy2 绘制的)。我希望每个的纵横比都为 1,但也具有相同的比例,因此所有子图的 X 和 Y 刻度相同,以便可以比较它们。我试图用 par
来做到这一点:
par(mfrow=c(2,2))
# scatter 1
plot(x, y, "p", asp=1)
# scatter 2
plot(a, b, "p", asp=1)
# ...
编辑:
这是我现在拥有的直接示例:
> par(mfrow=c(2,2))
> for (n in 1:4) { plot(iris$Petal.Width, rnorm(length(iris$Petal.Width)), "p", asp=1) }
它创建了正确类型的散点图,但具有不同的比例。设置
ylim
和 xlim
每次调用 plot
时都相同上面没有解决问题。您仍然会在每个轴上看到非常不同的刻度线和刻度数,这使得散布图难以解释。我希望 X 轴和 Y 轴相同。例如,这个:
for (n in 1:4) { plot(iris$Petal.Width, rnorm(length(iris$Petal.Width)), "p", asp=1, xlim=c(-4, 6), ylim=c(-2, 4)) }
生成错误的结果:
确保在所有子图中使用相同轴的最佳方法是什么?
我正在寻找的只是一个参数,如
axis=same
或类似的东西到par(mfrow=...)
,这听起来像是 lattice
的默认行为,使轴在每个子图中共享且相同。lgautier 用 ggplot 提供了很好的代码,但它需要提前知道轴。我想澄清一下,我想避免遍历每个子图中的数据并自己计算要绘制的正确刻度。如果必须提前知道,那么 ggplot 解决方案比仅使用
plot
绘图要复杂得多。并明确agstudy 给出了一个带有 lattice 的解决方案。这看起来最接近我想要的,因为您不必显式地预先计算每个散点的刻度位置,但作为一个新用户,我无法弄清楚如何使格子看起来像一个普通的图。我得到的最接近的是:
> xyplot(y~x|group, data =dat, type='p',
between =list(y=2,x=2),
layout=c(2,2), aspect=1,
scales =list(y = list(relation='same'), alternating=FALSE))
产生:
我怎样才能让它看起来像 R 基础?我不想要这些
group
每个子图顶部的字幕,或者在每个散点的顶部和右侧悬挂未标记的刻度,我只想标记散点的每个 x 和 y。我也不是在寻找 X 和 Y 的共享标签——每个子图都有自己的 X 和 Y 标签。并且每个散点中的轴标签必须相同,尽管此处选择的数据没有意义。除非有一种简单的方法可以使格子看起来像 R 基础,否则听起来答案是没有办法做我在 R 中尝试做的事情(令人惊讶),而不预先计算每个子图中每个刻度的确切位置,这需要提前遍历数据。
最佳答案
如果开始,ggplot2 可能具有最高的漂亮/简单比率。
rpy2 示例:
from rpy2.robjects.lib import ggplot2
from rpy2.robjects import r, Formula
iris = r('iris')
p = ggplot2.ggplot(iris) + \
ggplot2.geom_point(ggplot2.aes_string(x="Sepal.Length", y="Sepal.Width")) + \
ggplot2.facet_wrap(Formula('~ Species'), ncol=2, nrow = 2) + \
ggplot2.GBaseObject(r('ggplot2::coord_fixed')()) # aspect ratio
# coord_fixed() missing from the interface,
# therefore the hack. This should be fixed in rpy2-2.3.3
p.plot()
阅读对先前答案的评论,我发现您可能意味着完全分开
情节。使用 R 的默认绘图系统,
par(mfrow(c(2,2))
或 par(mfcol(c(2,2)))
将是最简单的方法,并通过通常固定的方式保持纵横比、轴范围和刻度线一致。在 R 中绘制的最灵活的系统可能是
grid
.它并不像看起来那么糟糕,把它想象成一个场景图。使用 rpy2、ggplot2 和网格:from rpy2.robjects.vectors import FloatVector
from rpy2.robjects.lib import grid
grid.newpage()
lt = grid.layout(2,2) # 2x2 layout
vp = grid.viewport(layout = lt)
vp.push()
# limits for axes and tickmarks have to be known or computed beforehand
xlims = FloatVector((4, 9))
xbreaks = FloatVector((4,6,8))
ylims = FloatVector((-3, 3))
ybreaks = FloatVector((-2, 0, 2))
# first panel
vp_p = grid.viewport(**{'layout.pos.col':1, 'layout.pos.row': 1})
p = ggplot2.ggplot(iris) + \
ggplot2.geom_point(ggplot2.aes_string(x="Sepal.Length",
y="rnorm(nrow(iris))")) + \
ggplot2.GBaseObject(r('ggplot2::coord_fixed')()) + \
ggplot2.scale_x_continuous(limits = xlims, breaks = xbreaks) + \
ggplot2.scale_y_continuous(limits = ylims, breaks = ybreaks)
p.plot(vp = vp_p)
# third panel
vp_p = grid.viewport(**{'layout.pos.col':2, 'layout.pos.row': 2})
p = ggplot2.ggplot(iris) + \
ggplot2.geom_point(ggplot2.aes_string(x="Sepal.Length",
y="rnorm(nrow(iris))")) + \
ggplot2.GBaseObject(r('ggplot2::coord_fixed')()) + \
ggplot2.scale_x_continuous(limits = xlims, breaks = xbreaks) + \
ggplot2.scale_y_continuous(limits = ylims, breaks = ybreaks)
p.plot(vp = vp_p)
rpy2 documentation about graphics 中的更多文档,然后在 ggplot2 和网格文档中。
关于r - 在R中创建具有相同轴的多个散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904748/