【问题标题】:How to make a scatterplot rectangular matrix: (y1, y2, ...) ~ (x1, x2, ...) in R: ggplot2, lattice or base如何制作散点图矩形矩阵:(y1, y2, ...) ~ (x1, x2, ...) in R: ggplot2, lattice or base
【发布时间】:2022-09-28 05:16:08
【问题描述】:

我正在寻找一种方法,以类似于基本pairs() 函数或GGally::ggpairs() 的方式,针对几个xs 中的每一个构建几个y 变量中每个变量的散点图矩形矩阵,但是只要对于那些y~x

理想情况下,这应该支持(a)按组着色点; (b) 添加注释,例如线性回归线。

这是基本思想的一个快速示例

> data(Rohwer, package = \"heplots\")
> str(Rohwer)
\'data.frame\':   69 obs. of  10 variables:
 $ group: int  1 1 1 1 1 1 1 1 1 1 ...
 $ SES  : Factor w/ 2 levels \"Hi\",\"Lo\": 2 2 2 2 2 2 2 2 2 2 ...
 $ SAT  : int  49 47 11 9 69 35 6 8 49 8 ...
 $ PPVT : int  48 76 40 52 63 82 71 68 74 70 ...
 $ Raven: int  8 13 13 9 15 14 21 8 11 15 ...
 $ n    : int  1 5 0 0 2 2 0 0 0 3 ...
 $ s    : int  2 14 10 2 7 15 1 0 0 2 ...
 $ ns   : int  6 14 21 5 11 21 20 10 7 21 ...
 $ na   : int  12 30 16 17 26 34 23 19 16 26 ...
 $ ss   : int  16 27 16 8 17 25 18 14 13 25 ...

我将在这里绘制 3 ys 和 3 xs:

yvars <- c(\"SAT\", \"PPVT\", \"Raven\" )
xvars <- c(\"n\", \"s\", \"ns\", \"na\", \"ss\")
xvars <- c(\"n\", \"s\", \"ns\")  # smaller example
gp <- \"SES\"

op <- par(mfrow = c(length(yvars), length(xvars)),
          mar = c(4, 4, 1, 1)+.1)
for(y in yvars) {
  for (x in xvars) {
    plot(Rohwer[, x], Rohwer[, y],
         xlab=x, ylab=y)
    abline(lm(Rohwer[, y] ~ Rohwer[, x]))
  }
}
par(op)

但当然,我想消除所有 y 标签,除了第 1 列和最后一行之外的行标签,以及能够为每个绘图的内容添加进一步的增强:不同的点符号/颜色组,单独的回归线,...

我已经查看了基本 R pairs()GGally::ggpairs()lattice::splom(),但没有看到任何方法可以做我想做的事。

    标签: r ggplot2 scatter-plot lattice


    【解决方案1】:

    使用ggplot2 实现结果的一种选择是使用GGally::ggmatrix

    library(heplots)
    
    data(Rohwer, package = "heplots")
    
    yvars <- c("SAT", "PPVT", "Raven" )
    xvars <- c("n", "s", "ns")
    gp <- "SES"
    
    grid <- expand.grid(x = xvars, y = yvars, stringsAsFactors = FALSE)
    
    library(ggplot2)
    library(GGally)
    
    plot_fun <- function(x, y) {
      ggplot(Rohwer, aes(.data[[x]], .data[[y]], color = .data[[gp]])) +
        geom_point() +
        geom_smooth(method = "lm", se = FALSE, formula = y ~ x)
    }
    
    p <- Map(plot_fun, grid$x, grid$y)
    
    ggmatrix(p, nrow = 3, ncol = 3, xAxisLabels = xvars, yAxisLabels = yvars)
    

    或者作为第二个选项,您可以转换为长格式并使用分面,恕我直言,这样可以更轻松地进一步自定义您的 plo:

    更新为了获得正确的订单,我们可以使用例如转换为factors额外的mutate 步骤。

    library(tidyr)
    library(dplyr)
    
    Rohwer_long <- Rohwer %>%
      pivot_longer(cols = all_of(xvars), names_to = "xvar", values_to = "x") %>%
      pivot_longer(cols = all_of(yvars), names_to = "yvar", values_to = "y") %>%
      mutate(xvar = factor(xvar, xvars), yvar = factor(yvar, yvars))
      
    ggplot(Rohwer_long, aes(x, y, color = SES)) +
      geom_point() +
      geom_smooth(method = "lm", se = FALSE, formula = y ~ x) +
      facet_grid(yvar ~ xvar, scales = "free")
    

    【讨论】:

    • 这是两个很好的答案。我不知道GGally::ggmatrix,也不知道如何通过 X 和 Y 变量旋转更长的时间。
    • 这将按字母顺序重新排序行和列变量,但我更愿意将它们保持在xvarsyvars 的顺序中。我会进一步调查;也许forcats 会有所帮助。此外,在rohwer_long 步骤中,为了清楚起见,我添加了:dplyr::select(-group, -na, -ss) |&gt;
    • 对不起。转换为长期放弃订单。但是我们可以添加一个mutate 步骤来转换为具有正确顺序的因子。看我的更新。
    猜你喜欢
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多