R带误差条的散点图矩阵 [英] R scatterplot matrix with error bars

查看:233
本文介绍了R带误差条的散点图矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何R软件包/方法/函数提供了将散点图矩阵绘制为这里 scatterplot.matrix 包的函数,找到 here )并按照已经询问和回答的方式绘制x和y错误列表here



一个例子:

  set.seed(123)$ b (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()

产生下面的图(X与Y带有误差条):



同时

 图书馆(汽车)
spm(〜X + Y + Z,data = df)

产生一个散点图矩阵,例如因为这:



现在我的预期的输出将是这样一个散点图的矩阵(除了 car 之外的任何其他包也可以)也可以显示errorb ARS。 (请注意,并非所有变量都有错误,例如 Z 不)。此外,由 spm 函数完成的拟合等是一个很好的噱头,但对我的意义来说这不是必需的。

解决方案

数据

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

代码

 库(ggplot2)
库(gtools)
valCols < -c(X,Y,Z)
errCols< - setNames(c(errX,errY,NA),valCols)
combn< (函数(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 ()+
geom_errorbar()+ geom_errorbarh()+
facet_grid() (var1〜var2,drop = FALSE))

解释



首先,您必须以某种方式转换数据,例如 ggplot2 喜欢它。也就是说,你的x轴和y轴分别加一列,加上每一列的误差线。



我在这里使用的是函数 permutations library(gtools),它返回(在这种情况下)所有2个元素排列。对于这些排列中的每一个,我从原始数据集中选择相应的列并添加相关的错误列(如果存在)。如果列名遵循值和错误栏列的特定模式,您可以使用 regex 自动确定它们,如下所示:

  valCols<  - 名称(df)[grepl(^ [AZ] $,名称(df))] 

最后,我添加列 var1 var2 描述选择哪些变量:

  head(mdf)
#val1 val2 err1 err2 var1 var2
#1 -0.56047565 -1.0678237 0.12240818 0.08529284 XY
#2 -0.23017749 -0.2179749 0.03598138 -0.05901430 XY
#3 1.55870831 -1.0260044 0.04007715 0.17902513 XY
#4 0.07050839 -0.7288912 0.01106827 0.17562670 XY
#5 0.12928774 -0.6250393 -0.05558411 0.16431622 XY
#6 1.71506499 -1.6866933 0.17869131 0.13772805 XY



<通过这种方式转换数据使生成散点图矩阵变得相当容易。使用这种方法,还可以修改对角线面板,如下面的示例所示:

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

p>



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.

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

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. (Note that not all of my variables have errors, e.g. Z does not). Also the fitting etc that is done here by the spm function is a nice gimmick but not necessary for my means.

解决方案

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. That is, one column each for your x- and y-axis respectively plus one column each for the error bars.

What I used here, is function permutations from library(gtools), which returns (in this case) all 2 element permutations. 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:

valCols <- names(df)[grepl("^[A-Z]$", names(df))]

Finally, I add the columns var1and var2 describing which variables were selected:

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

这篇关于R带误差条的散点图矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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