如何从单个数值向量制作具有多个方面的ggplot2散点图?

How to make ggplot2 scatter plot with multiple facets from a single numeric vector?

假设我有一个包含 100 个主题的记录 (Value) 的数据框 (Subject),这是用三种不同的方法测量的 (Method)。现在我想从每个方法中绘制 Value 彼此,所以在这种情况下 "base-new"、"base-edge" 和 "new-edge"。如何 我可以根据单个数字变量在 ggplot2 中使用 facet_wrap?

dummy <- data.frame(Value = c(rnorm(100, mean = 35, sd = 2),
                              rnorm(100, mean = 47, sd = 2),
                              rnorm(100, mean = 28, sd = 1)),
                    Method = c(rep("base", times = 100),
                               rep("new", times = 100),
                               rep("edge", times = 100)),
                    Subject = rep(paste0("M", seq_len(100)), times = 3))
str(dummy)

## 'data.frame':    300 obs. of  3 variables:
##  $ Value  : num  32.9 32.2 37 36.6 33 ...
##  $ Method : Factor w/ 3 levels "base","edge",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Subject: Factor w/ 100 levels "M1","M10","M100",..: 1 13 24 35 46 57 68 79 90 2 ...

此代码无效,仅用于说明我的内容 想做:

library("ggplot2")
ggplot(dummy, aes(Value)) +
    geom_point() +
    facet_wrap(~ Method)

编辑

这将是我使用基础 R:

的解决方案
opar <- par()
par(mfrow = c(1, 3))
plot(dummy[dummy$Method == "base", "Value"],
     dummy[dummy$Method == "new", "Value"],
     xlab = "base", ylab = "new")
plot(dummy[dummy$Method == "base", "Value"],
     dummy[dummy$Method == "edge", "Value"],
     xlab = "base", ylab = "edge")
plot(dummy[dummy$Method == "new", "Value"],
     dummy[dummy$Method == "edge", "Value"],
     xlab = "new", ylab = "edge")
par(opar)

所以虽然这不是您要找的东西,但它很接近:我建议用 facet_grid 代替矩阵图:

您的数据需要稍微不同的格式:

set.seed(1234)
dummy <- data.frame(Value = c(rnorm(100, mean = 35, sd = 2),
                              rnorm(100, mean = 47, sd = 2),
                              rnorm(100, mean = 28, sd = 1)),
                    Method = c(rep("base", times = 100),
                               rep("new", times = 100),
                               rep("edge", times = 100)),
                    Subject = rep(paste0("M", seq_len(100)), times = 3))
dummy2 = rbind(cbind.data.frame(x = dummy$Value[1:100], xmet = rep("base", 100), y = dummy$Value[101:200], ymet = rep("new", 100)),
               cbind.data.frame(x = dummy$Value[1:100], xmet = rep("base", 100), y = dummy$Value[201:300], ymet = rep("edge", 100)),
               cbind.data.frame(x = dummy$Value[101:200], xmet = rep("new", 100), y = dummy$Value[201:300], ymet = rep("edge", 100)))

你的绘图完成了:

library("ggplot2")
ggplot(dummy2, aes(x = x, y = y)) +
  geom_point() +
  facet_grid(ymet ~ xmet)

给出:

现在您可以添加例如自由领域的传奇人物。我的出发点是对

的回答