Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you add jitter to a scatterplot matrix in ggpairs?

Tags:

r

ggplot2

ggally

I want to add jitter to a scatterplot matrix. The question was addressed on the following page (and nowhere else) on stackoverflow:

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

But both solutions to the jitter problem which were suggested there involve deprecated code (plotmatrix and params):

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

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

I would have simply commented asking for an update there so as to not create a new question, but that appears to require reputation points, and I'm new to the site. My apologies if I've done something wrong in posting the question.

EDIT:

Here's what the data looks like:

> str(EHRound4.subset)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   301 obs. of  22 variables:
$ Subject#         : int  1 2 3 4 6 7 8 13 14 16 ...
$ Condition        : Factor w/ 2 levels "CDR","Mturk": 1 1 1 1 1 1 1 1 
1 1 ...
$ Launch4          : int  5 8 8 5 8 5 3 8 5 6 ...
$ NewSong4         : int  6 8 8 6 8 6 8 8 8 7 ...
$ StudCom5         : int  6 5 8 3 1 3 4 8 7 7 ...
$ Textbook5        : int  8 1 8 3 1 7 8 8 8 8 ...    

And here's several attempts at getting jitter.

> ggpairs(EHRound4.subset, columns = 3:6, 
ggplot2::aes(colour=Condition), lower = list(geom_jitter(alpha = .2)))

> ggpairs(EHRound4.subset, columns = 3:6, 
ggplot2::aes(colour=Condition, alpha=.2), lower = list(geom_jitter()))

> ggpairs(EHRound4.subset, columns = 3:6, 
ggplot2::aes(colour=Condition, alpha=.2, position="jitter"))
like image 240
emory989 Avatar asked Jul 11 '17 20:07

emory989


1 Answers

@user20650 answered the question in comments below the question. For completeness, here it is in the form of an answer:

Use wrap, such as:

library(GGally)
ggpairs(y, lower = list(continuous=wrap("points", position=position_jitter(height=3, width=3))))

By using position = position_jitter() instead of just position = "jitter" (which also works) the additional jitter parameters can also be controlled.

like image 145
Fato39 Avatar answered Oct 29 '22 01:10

Fato39