[英]R scatterplot matrix with error bars
Is there any R package/method/function that provides the functionality to plot a matrix of scatterplots as here ( scatterplot.matrix
function of the car
package, found here ) AND to plot x and y errorbars as has been asked and answered here . 有没有[R包/方法/函数提供的功能为绘制散点图矩阵这里 (
scatterplot.matrix
的功能car
包,发现这里 ),并绘制x和y errorbars作为已被要求,并回答了这里 。
An example: 一个例子:
set.seed(123)
df <- data.frame(X = rnorm(10), errX = rnorm(10)*0.1, Y = rnorm(10), errY = rnorm(10)*0.2, Z = rnorm(10))
require(ggplot2)
ggplot(data = df, aes(x = X, y = Y)) + geom_point() +
geom_errorbar(aes(ymin = Y-errY, ymax = Y+errY)) +
geom_errorbarh(aes(xmin = X-errX, xmax = X+errX)) + theme_bw()
produces the following plot (X vs Y with errorbars): 生成以下图(带有误差线的X与Y):
while 而
library(car)
spm(~X+Y+Z, data=df)
produces a scatterplot matrix such as this: 生成如下的散点图矩阵:
Now my expected output would be such a matrix of scatterplots (any other package than car
will be fine as well) where I can also display errorbars. 现在,我的预期输出将是这样的散点图矩阵(除了
car
以外的任何其他软件包也可以),在这里我还可以显示错误栏。 (Note that not all of my variables have errors, eg Z
does not). (请注意,并非我的所有变量都有错误,例如
Z
没有)。 Also the fitting etc that is done here by the spm
function is a nice gimmick but not necessary for my means. 另外,由
spm
函数在此处完成的拟合等也是不错的头,但对于我的手段而言并非必需。
Data 数据
set.seed(123)
df <- data.frame(X = rnorm(10), errX = rnorm(10)*0.1,
Y = rnorm(10), errY = rnorm(10)*0.2,
Z = rnorm(10))
Code 码
library(ggplot2)
library(gtools)
valCols <- c("X", "Y", "Z")
errCols <- setNames(c("errX", "errY", NA), valCols)
combn <- permutations(length(valCols), 2, valCols)
mdf <- do.call(rbind,
apply(combn, 1, function(ind) {
df[["NA.Column"]] <- NA
errC <- errCols[ind]
errC[is.na(errC)] <- "NA.Column"
vals <- setNames(data.frame(df[, ind]), paste0("val", seq_along(ind)))
errs <- setNames(data.frame(df[, errC]), paste0("err", seq_along(errC)))
ret <- cbind(vals, errs)
ret$var1 <- factor(ind[1], levels = valCols)
ret$var2 <- factor(ind[2], levels = valCols)
ret
}))
(p <- ggplot(mdf, aes(x = val1, y = val2,
ymin = val2 - err2, ymax = val2 + err2,
xmin = val1 - err1, xmax = val1 + err1)) +
geom_point() +
geom_errorbar() + geom_errorbarh() +
facet_grid(var1 ~ var2, drop = FALSE))
Explanation 说明
First, you have to transform your data in a way, such that ggplot2
likes it. 首先,您必须以某种方式转换数据,以便
ggplot2
喜欢它。 That is, one column each for your x- and y-axis respectively plus one column each for the error bars. 也就是说,x轴和y轴分别为一列,误差线分别为一列。
What I used here, is function permutations
from library(gtools)
, which returns (in this case) all 2 element permutations. 我在这里使用的是来自
library(gtools)
函数permutations
,它返回(在这种情况下)所有2个元素置换。 For each of these permutations, I select the corresponding column from the original data set and add the related error columns (if existing). 对于每个排列,我从原始数据集中选择相应的列,并添加相关的错误列(如果存在)。 If the column names follow a certain pattern for value and error bar columns, you can use
regex
to determine these automatically like in: 如果列名称遵循值和错误栏列的特定模式,
regex
可以使用regex
来自动确定它们,例如:
valCols <- names(df)[grepl("^[A-Z]$", names(df))]
Finally, I add the columns var1
and var2
describing which variables were selected: 最后,我添加列
var1
和var2
描述选择了哪些变量:
head(mdf)
# val1 val2 err1 err2 var1 var2
# 1 -0.56047565 -1.0678237 0.12240818 0.08529284 X Y
# 2 -0.23017749 -0.2179749 0.03598138 -0.05901430 X Y
# 3 1.55870831 -1.0260044 0.04007715 0.17902513 X Y
# 4 0.07050839 -0.7288912 0.01106827 0.17562670 X Y
# 5 0.12928774 -0.6250393 -0.05558411 0.16431622 X Y
# 6 1.71506499 -1.6866933 0.17869131 0.13772805 X Y
Having the data transformed this way makes it rather easy to generate the scatter plot matrix. 以这种方式转换数据使得生成散点图矩阵变得相当容易。 With this approach it is also possible to modify the diagonal panel as shown in the follwing example:
使用这种方法,还可以修改对角线面板,如以下示例所示:
p + geom_text(aes(ymin = NULL, ymax = NULL, xmin = NULL, xmax = NULL),
label = "X",
data = data.frame(var1 = "X", var2 = "X",
val1 = 0, val2 = 0))
Plot 情节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.