How to produce a meaningful draftsman/correlation plot for discrete values
Asked Answered
H

4

6

One of my favorite tools for exploratory analysis is pairs(), however in the case of a limited number of discrete values, it falls flat as the dots all align perfectly. Consider the following:

y <- t(rmultinom(n=1000,size=4,prob=rep(.25,4)))
pairs(y)

It doesn't really give a good sense of correlation. Is there an alternative plot style that would?

Haemin answered 11/2, 2014 at 1:26 Comment(1)
Two R packages are described in vita.had.co.nz/papers/gpp.htmlTrophoblast
D
8

If you change y to a data.frame you can add some 'jitter' and with the col option you can set the transparency level (the 4th number in rgb):

y <- data.frame(y)
pairs(sapply(y,jitter), col = rgb(0,0,0,.2))

enter image description here

Or you could use ggplot2's plotmatrix:

library(ggplot2)
plotmatrix(y) + geom_jitter(alpha = .2)

enter image description here

Edit: Since plotmatrix in ggplot2 is deprecated use ggpairs (GGally package mentioned in @hadley's comment above)

library(GGally)
ggpairs(y, lower = list(params = c(alpha = .2, position = "jitter")))

enter image description here

Downandout answered 11/2, 2014 at 2:36 Comment(0)
C
4

Here are a couple of options using ggplot2:

library(ggplot2)

## re-arrange data (copied from plotmatrix function)
prep.plot <- function(data) {
    grid <- expand.grid(x = 1:ncol(data), y = 1:ncol(data))
    grid <- subset(grid, x != y)
    all <- do.call("rbind", lapply(1:nrow(grid), function(i) {
        xcol <- grid[i, "x"]
        ycol <- grid[i, "y"]
        data.frame(xvar = names(data)[ycol], yvar = names(data)[xcol], 
                   x = data[, xcol], y = data[, ycol], data)
    }))
    all$xvar <- factor(all$xvar, levels = names(data))
    all$yvar <- factor(all$yvar, levels = names(data))
    return(all)
}

dat <- prep.plot(data.frame(y))

## plot with transparent jittered points
ggplot(dat, aes(x = x, y=y)) +
    geom_jitter(alpha=.125) +
    facet_grid(xvar ~ yvar) +
    theme_bw()

## plot with color representing density
ggplot(dat, aes(x = factor(x), y=factor(y))) +
    geom_bin2d() +
    facet_grid(xvar ~ yvar) +
    theme_bw()
Cryptonymous answered 11/2, 2014 at 2:53 Comment(0)
A
4

Here is an example using corrplot:

M <- cor(y)
corrplot.mixed(M)

enter image description here

You can find more examples in the intro

http://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html

Allout answered 11/2, 2014 at 3:21 Comment(0)
D
0

I don't have enough credits yet to comment on @Vincent 's post - when doing

library(GGally)
ggpairs(y, lower = list(params = c(alpha = .2, position = "jitter"))) 

I get

Error in stop_if_params_exist(obj$params) : 
  'params' is a deprecated argument.  Please 'wrap' the function to supply arguments. help("wrap", package = "GGally")

So it seems, based on the indicated help page, that it would need to be in this case here:

ydf <- as.data.frame(y)
regularPlot <- ggpairs(ydf, lower = list(continuous = wrap(ggally_points, alpha = .2, position = "jitter")))
regularPlot
Donnydonnybrook answered 17/1, 2023 at 16:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.