R 如何从一个数值向量生成具有多个面的ggplot2散点图?
假设我有一个数据框,其中包含100个主题的记录(R 如何从一个数值向量生成具有多个面的ggplot2散点图?,r,ggplot2,facet,R,Ggplot2,Facet,假设我有一个数据框,其中包含100个主题的记录(Value) (受试者),采用三种不同的方法进行测量 (方法)。现在我想根据每个方法绘制值 相互之间,因此在本例中为“基础新边”、“基础边”和“新边”。怎么 我可以在ggplot2中基于单个数值变量使用 facet\u wrap dummy <- data.frame(Value = c(rnorm(100, mean = 35, sd = 2), rnorm(100, mean =
Value
)
(受试者
),采用三种不同的方法进行测量
(方法
)。现在我想根据每个方法绘制值
相互之间,因此在本例中为“基础新边”、“基础边”和“新边”。怎么
我可以在ggplot2
中基于单个数值变量使用
facet\u 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 ...
编辑
这将是我使用baseR
的解决方案:
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)
opar因此,虽然这并不完全是您想要的,但它非常接近:我建议使用facet\u 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)))
其中:
现在,您可以在自由字段中添加图例。我的出发点是对的回答,你能解释一下“使用facet\u wrap彼此重聚”是什么意思吗?我无法想象这一点。也许你只需要ggplot(dummy,aes(Subject,Value))+geom\u point()+facet\u wrap(~Method)
?我已经为绘图添加了一个示例,使用base R作为我想要实现的说明。我认为最简单的方法是将数据重新构造为列x
、y
和方法组合
,其中后者将是新的_base@mts我想我能看到你的想象,但那会让我感到困惑请给我一个新问题,即为所有面标记轴,不是吗?@mattw-true,我在下面的回答中建议了一个解决方法,我不确定它是否适合您的需要。这已经足够接近了。谢谢!
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)