使用R中的ggplot2表达三个变量之间的关系

B.L*_*ANG 2 r ggplot2

我有这样的数据框

structure(list(cli_exp = c(1L, 1L, 2L, 1L, 1L, 0L, 2L, 0L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 2L, 2L, 0L, 1L, 0L, 
1L, 1L, 2L, 0L, 1L), vcs_exp = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 
1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 2L, 1L, 
1L, 0L, 0L, 0L, 2L, 1L, 0L), web_exp = c(2L, 2L, 2L, 1L, 0L, 
0L, 1L, 2L, 0L, 0L, 3L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 2L, 1L, 1L, 
1L, 1L, 0L, 0L, 1L, 1L, 2L, 0L, 0L)), .Names = c("cli_exp", "vcs_exp", 
"web_exp"), row.names = c(NA, 30L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

我想用ggplot2来表达这三个变量之间的关系,并尝试了简单的点图

ggplot(data = data) +
    geom_point(mapping = aes(x = web_exp, y = vcs_exp, color = cli_exp))
Run Code Online (Sandbox Code Playgroud)

但显然,有许多重叠的数据点,不适合点显示.有没有更好的方法?

Tun*_*ung 5

我会从包中使用ggpairsGGally

tmp_df <- structure(list(cli_exp = c(1L, 1L, 2L, 1L, 1L, 0L, 2L, 0L, 1L, 
                1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 2L, 2L, 0L, 1L, 0L, 
                1L, 1L, 2L, 0L, 1L), vcs_exp = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 
                1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 2L, 1L, 
                1L, 0L, 0L, 0L, 2L, 1L, 0L), web_exp = c(2L, 2L, 2L, 1L, 0L, 
                0L, 1L, 2L, 0L, 0L, 3L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 2L, 1L, 1L, 
                1L, 1L, 0L, 0L, 1L, 1L, 2L, 0L, 0L)), .Names = c("cli_exp", "vcs_exp", 
                "web_exp"), row.names = c(NA, 30L), class = "data.frame")

library(GGally)
ggpairs(tmp_df, 
    upper = list(continuous = wrap("cor", size = 10)), 
    lower = list(continuous = "smooth"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

编辑:pairs从基础R使用

pairs(tmp_df)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

pairs.panels从psych包中使用

library(psych)
pairs.panels(tmp_df, 
         method = "pearson",
         density = TRUE,  
         ellipses = TRUE 
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述