在R中创建具有相同轴的多个散点图 [英] creating multiple scatter plots with same axes in R

查看:196
本文介绍了在R中创建具有相同轴的多个散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在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轴相同。例如,这个:
$ b $
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给出了一个格子的解决方案。这看起来最接近我想要的,因为你不必为每个分散值明确地预先计算滴答位置,但作为一个新用户,我无法弄清楚如何使得晶格看起来像一个普通的阴谋。我得到的最接近的是:

 > xyplot(y〜x | group,data = dat,type ='p',
between = list(y = 2,x = 2),
layout = c(2,2) 1,
scales = list(y = list(relation ='same'),alternating = FALSE))

这会产生:



我怎样才能让这个看起来像R基地?我不想在每个子图的顶部显示这些 group 字幕,或者在每个分散图的顶部和右侧悬挂未标记的刻度,我只想要每个x和y的分散标签。我也没有为X和Y寻找共享标签 - 每个子图都有自己的X和Y标签。并且轴标签在每个散点图中必须相同,尽管在这里选择的数据是没有意义的。



除非有一种简单的方法可以使网格看起来像R基地,这听起来像是答案是没有办法做我想做的R(令人惊讶的),没有预先计算每个子图中每个tick的确切位置,这需要事先迭代数据。

解决方案



rpy2的示例:

  from rpy2.robjects.lib import ggplot2 
from rpy2.robjects import r,公式

iris = r('iris')

p = ggplot2.ggplot(iris)+ \
ggplot2.geom_point(ggplot2.aes_string(x = (公式('〜Species'),ncol = 2,nrow = 2)+ \
ggplot2。 GBaseObject(r('ggplot2 :: coord_fixed')())#高宽比
#coord_fixed()从接口中缺少,
#因此是黑客。这应该被固定在rpy2-2.3.3

p.plot()



<阅读评论以前的答案我看到,你可能意味着完全独立的
地块。使用R的默认绘图系统, par(mfrow(c(2,2)) par(mfcol(c(2,2)) )将是最简单的方法,并保持纵横比,轴的范围和tickmarks以通常固定方式一致。



在R中绘制的最灵活的系统可能是 grid 。它看起来并不像看起来那么糟糕,它被认为是一个场景图形,使用rpy2,ggplot2和grid :

  from rpy2.robjects.vectors从rpy2.robjects.lib导入FloatVector 

import grid
grid.newpage()
lt = grid.layout(2,2)#2x2布局
vp = grid.viewport(layout = lt)
vp.push()


#轴和tickmarks的限制必须事先知道或计算
xlims = FloatVector((4,9))
xbreaks = FloatVector((4,6, 8))
ylims = FloatVector(( - 3,3))
ybreaks = FloatVector(( - 2,0,2))

#第一个面板
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, break = ybreaks)
p.plot(vp = vp_p)
#第三个面板
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文档,以及之后的ggplot2和网格文档。


I'm trying to plot four scatter plots in 2 x 2 arrangement in R (I'm actually plotting via rpy2). I'd like each to have an aspect ratio of 1 but also be on the same scale, so identical X and Y ticks for all the subplots so that they can be compared. I tried to do this with par:

par(mfrow=c(2,2))
# scatter 1
plot(x, y, "p", asp=1)
# scatter 2
plot(a, b, "p", asp=1)
# ...

Edit:

Here's a direct example of what I have now:

> par(mfrow=c(2,2))
> for (n in 1:4) { plot(iris$Petal.Width, rnorm(length(iris$Petal.Width)), "p", asp=1) }

which creates the right type of scatter but with different scales. Setting ylim and xlim to be the same in each call to plot above does not fix the problem. You still get very different tick marks and tick numbers on each axis, which makes the scatter unnecessarily difficult to interpret. I want the X and Y axes to be identical. For example, this:

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

Generates the wrong result:

What's the best way to ensure that the same axes are used in all subplots?

All I was looking for is a parameter like axis=same or something like that to par(mfrow=...), which sounds like the default behavior for lattice, to make the axes shared and identical in every subplot.

lgautier gave nice code with ggplot, but it requires the axes to be known in advance. I want to clarify that I wanted to avoid iterating through the data in each subplot and computing myself the correct ticks to be plotted. If that has to be known in advance, then the ggplot solution is much more complex than just plotting with plot and explicitly

agstudy gave a solution with lattice. This looks closest to what I what I want in that you don't have to explicitly precompute the tick positions for each scatter, but as a new user I'm unable to figure out how to make lattice look like an ordinary plot. The closest I've gotten is this:

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

which yields:

How can I make this look like the R base? I don't want these group subtitles on the top of each subplot, or ticks hanging unlabeled on the top and right hand side of each scatter, I just want each x and y of the scatter to be labeled. I'm also not looking for a shared label for the X and Y -- each subplot gets its own X and Y labels. And the axis labels have to be the same in each scatter although with the data chosen here it doesn't make sense.

Unless there's an easy way to make trellis look like the R base, it sounds like the answer is that there's no way to do what I'm trying to do in R (surprisingly), without precomputing the exact places of each tick in each subplot, which requires iterating through the data in advance.

解决方案

ggplot2 might be have the highest pretty / easy ratio if beginning.

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

Reading the comments to a previous answer I see that you might mean completely separate plots. With the default plotting system for R, par(mfrow(c(2,2)) or par(mfcol(c(2,2))) would the easiest way to go, and keep aspect ratio, ranges for the axes, and tickmarks consistent through the usual way those are fixed.

The most flexible system to plot in R might be grid. It is not as bad as it seems, think of is as a scene graph. With rpy2, ggplot2, and grid:

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)

More documentation in the rpy2 documentation about graphics, and after in the ggplot2 and grid documentations.

这篇关于在R中创建具有相同轴的多个散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆