在ggduo scatterplot矩阵中显示相关索引(带着色)

时间:2018-06-14 09:27:09

标签: r ggplot2

基于this帖子,现在我已经

library (GGally)

# 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 ...
    )
}

# data frame
df = data.frame(runif(100),
                rnorm(100),
                rgamma(100,1,2),
                rt(100,1),
                rf(100,1,2),
                as.factor(round(runif(100,0,1))))
colnames(df) = c("a","b","c","d","e","f")

# points + cor, but only one cor index
ggduo(df,columnsX = 1:2, columnsY = 3:5,
      mapping = aes(colour = f),
      types = list(continuous = PointsWithCor))

但它产生一个散点图矩阵,在所有x和所有y中都有相关性。我想用相同的方式显示相关的颜色,以便在散点图中着色点。

我认为它需要修改函数以在映射中使用colour属性,但不知道如何操作。有人可以给我一个建议吗?

enter image description here

编辑:
要在@aosmith的答案中对齐图像中的相关标签,

# from help but modified
PointsWithCor <- function(data, mapping, ..., method = "pearson") {
  df <- data.frame(x = eval(mapping$x, data), y = eval(mapping$y, data), c = eval(mapping$colour, data))

  xPos = min(df$x)
  yPos = max(df$y)

  sumdf <- df %>%
    group_by(c) %>%
    summarise(
      lab = round(cor(x, y),3),
      x = xPos,
      y = yPos*min(as.numeric(c))/max(as.numeric(df$c))
    )

  ggally_points(data, mapping, ...) +
    ggplot2::geom_label(
      data = sumdf,
      mapping = ggplot2::aes(x = x, y = y, label = lab, color = c),
      hjust = 0, vjust = 1,
      size = 5, fontface = "bold",
      inherit.aes = FALSE # do not inherit anything from the ...
    )
}

1 个答案:

答案 0 :(得分:2)

这是一种方法。我找到了按组估算标签值和轴位置的关键。我使用 dplyr 中的帮助函数进行分组和汇总。

否则,这与您所做的相似,使用情节中的mapping。我将映射(xycolour)存储在data.frame中,以便我可以进行汇总。

您可能希望处理轴位置放置。你会看到min x和max y并不适用于所有这些。您可能决定以不同的方式计算它们。

这是我做的功能:

library(GGally)
library(dplyr)

points_with_cor_color = function(data, mapping, ..., method = "pearson") {
     dat = data.frame(x = data[, as.character(mapping$x)],
                      y = data[, as.character(mapping$y)],
                      color = data[, as.character(mapping$colour)])

     sumdat = dat %>%
          group_by(color) %>%
          summarise(lab = round(cor(x, y, method = method), 3),
                    x = min(x, na.rm = TRUE), 
                    y = max(y, na.rm = TRUE) )

     ggally_points(data, mapping, ...) +
          ggplot2::geom_label(
               data = sumdat,
               mapping = ggplot2::aes(x = x, y = y, label = lab, color = color),
               hjust = 0, vjust = 1,
               size = 5, fontface = "bold", # do not inherit anything from the ...
               inherit.aes = FALSE
          )
}

以下是使用ggduo()的绘图代码。

ggduo(df,columnsX = 1:2, columnsY = 3:5,
      mapping = ggplot2::aes(color = f),
     types = list(continuous = points_with_cor_color))

enter image description here