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

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

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

ggplot(data = data) +
    geom_point(mapping = aes(x = web_exp, y = vcs_exp, color = cli_exp))

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

最佳答案

我会使用 ggpairs来自 GGally

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

enter image description here

编辑:使用基 R 中的

pairs(tmp_df)

enter image description here

使用 psych 包中的 pairs.panels

library(psych)
pairs.panels(tmp_df, 
         method = "pearson",
         density = TRUE,  
         ellipses = TRUE 
)

enter image description here

关于r - 在 R 中使用 ggplot2 表达三个变量之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48836521/

相关文章:

r - 如何在 slidify 中打印表格?

r - 使用 tidyverse 和 sapply 在列表上创建一个函数

R 热图着色取决于高于或低于零的值

r - 如何更改 ggplot2 中的默认主题?

r - 有没有一种方法可以使用geom_raster在ggplot2中的矩形周围创建边框?

r - 将不同数据集中的 geom_text 添加到 geom_bar

r - 如何在绘图 (ggplot2) 的文本注释中放置 +/- 加减运算符?

r - SF : Generate random points with maximal distance condition

r - 在 ggplot2 中使用带有百分号 (%) 的 plotmath

r - 更改组合 ggplot 中的条形颜色