如何在ggpairs中为散点图矩阵添加抖动?

时间:2017-07-11 20:44:03

标签: r ggplot2 ggally

我想将抖动添加到散点图矩阵中。在stackoverflow上的下一页(以及其他地方)解决了这个问题:

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

但是那里提出的抖动问题的解决方案都涉及不推荐的代码(plotmatrix和params):

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

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

我会简单地评论要求更新,以便不创建新问题,但这似乎需要声誉点,而且我是网站的新手。如果我在发布问题时做错了,我很抱歉。

编辑:

这是数据的样子:

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

这里有几次尝试获得抖动。

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

1 个答案:

答案 0 :(得分:0)

@ user20650在问题下方的评论中回答了该问题。为了完整起见,这里以答案的形式:

使用wrap,例如:

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

通过使用position = position_jitter()而不是position = "jitter"(也可以使用),还可以控制其他抖动参数。