【发布时间】:2016-01-21 19:37:56
【问题描述】:
我正在尝试定义一个函数以在成对图的对角线上生成散点图。诀窍是我想在panel.splot 函数中指定垂直轴。最终,我认为这里的用途是在对角线上可视化时间序列。举一个简单的例子,虽然我想尝试使用 iris 数据集并使用 Species 作为垂直轴变量。下面的图产生了一个漂亮的图:
pairs(iris[,c(1:4)])
所以现在我需要为对角面板定义一个函数。这是我迄今为止的努力:
panel.splot <- function(x, yvar,...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(0, 2, usr[3:4]))
plot(yvar,x, xlim=c(min(x),max(x)))
}
但是,当我尝试运行它时,我收到一条错误消息,我不确定如何解释。
pairs(iris[,c(1:4)],diag.panel=panel.splot(x=x, yvar="Species"))
Error in plot.window(...) : invalid 'xlim' value
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
3: In min(x) : no non-missing arguments to min; returning Inf
4: In max(x) : no non-missing arguments to max; returning -Inf
我找不到另一个这样的例子。许多其他功能可以创建不同类型的绘图,但没有任何功能可以做到这一点。
为清楚起见,这是我想象的沿pairs() 调用的对角线生成的情节类型:
plot(iris$Species,iris$Petal.Width)
【问题讨论】:
-
我没用过
pairs,但是我曾经看过GGally::ggpairs;看一下最后一个示例(“自定义示例”),其中似乎很容易将任何图分配给子图网格中的任何坐标。我只是尝试使用custom_car[1,1] <- plot在对角线上绘制一个图。 -
在
panel.splot中,您正试图获取yvar的min和max,其中包含字符串Species。我不认为你是故意的。这不会解决您的问题,但它似乎是一个问题。 -
x中的pairs(iris[,c(1:4)],diag.panel=panel.splot(x=x, yvar="Species"))是什么 -
如果我在没有
x=x的情况下运行它,我会收到此错误:Error in xy.coords(x, y, xlabel, ylabel, log) : argument "x" is missing, with no default。 @steveb:你是对的。编辑了问题。 -
你想
plot('character string', numeric_vector),改成plot(iris[, 'Species'], x)并把new = TRUE加到par,这是你想要的吗?