在ggduo scatterplot矩阵中显示相关索引

时间:2018-06-13 13:37:11

标签: r ggplot2

我有这样的事情:

library (GGally)
df = data.frame(runif(100),
                rnorm(100),
                rgamma(100,1,2),
                rt(100,1),
                rf(100,1,2))

ggduo(df,columnsX = 1:2, columnsY = 3:5,
      types = list(continuous = "points"))

ggduo(df,columnsX = 1:2, columnsY = 3:5,
      types = list(continuous = "cor"))

第一个图显示散点图矩阵,第二个图显示变量之间的相关性。

然后我想在散点图中显示相关性。我想我可以通过将几个散点图与cowplot合并来实现,但可以在ggduo内进行吗?

修改:我发布了相关问题here

1 个答案:

答案 0 :(得分:0)

根据该文件,它将显示相关性。

library (GGally)
df = data.frame(runif(100),
                rnorm(100),
                rgamma(100,1,2),
                rt(100,1),
                rf(100,1,2))

# from help
PointsWithCor <- function(data, mapping, ..., method = "pearson") {
  x <- eval(mapping$x, data)
  y <- eval(mapping$y, data)
  cor <- cor(x, y, method = method)
  ggally_points(data, mapping, ...) +
    ggplot2::geom_label(
      data = data.frame(
        x = min(x, na.rm = TRUE),
        y = max(y, na.rm = TRUE),
        lab = round(cor, digits = 3)
      ),
      mapping = ggplot2::aes(x = x, y = y, label = lab),
      hjust = 0, vjust = 1,
      size = 5, fontface = "bold",
      inherit.aes = FALSE # do not inherit anything from the ...
    )
}

# plot
ggduo(df,columnsX = 1:2, columnsY = 3:5,
      types = list(continuous = PointsWithCor)))