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

1

11

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"))
Sexual answered 11/7, 2017 at 20:44 Comment(6)
To improve the question, please consider adding plotting code with example data, showing your plot without the jittering. Ideally, we should be able to easily run your code.Toni
use wrap ... lower = list(continuous=wrap("points", position="jitter"))Grimes
That worked, thank you very much!Sexual
you're welcome - please feel free to write up . (ps to get more control of the jitter you can use lower = list(continuous=wrap("points", position=position_jitter(height=3, width=3)))Grimes
I figured out the position_jitter() call, but thank you! What do you mean "write up"?Sexual
@Sexual ; i meant to write it in the answer section belowGrimes
F
7

@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.

Florindaflorine answered 7/12, 2018 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.