ggplot2 - R scatterplot matrix with error bars -


is there r package/method/function provides functionality plot matrix of scatterplots here (scatterplot.matrix function of car package, found here) , plot x , y errorbars has been asked , 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 following plot (x vs y errorbars): enter image description here

while

library(car) spm(~x+y+z, data=df) 

produces scatterplot matrix such this: enter image description here

now expected output such matrix of scatterplots (any other package car fine well) can display errorbars. (note not of variables have errors, e.g. z not). fitting etc done here spm function nice gimmick not necessary 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, have transform data in way, such ggplot2 likes it. is, 1 column each x- , y-axis respectively plus 1 column each error bars.

what used here, function permutations library(gtools), returns (in case) 2 element permutations. each of these permutations, select corresponding column original data set , add related error columns (if existing). if column names follow pattern value , error bar columns, can use regex determine these automatically in:

valcols <- names(df)[grepl("^[a-z]$", names(df))] 

finally, add columns var1and var2 describing variables 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 data transformed way makes rather easy generate scatter plot matrix. approach possible modify diagonal panel shown in 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

scatterplot matrix scatterplot matrix diagonal element


Comments