假设我有一个包含100个主题的记录(Value
)的数据框
(Subject
),用三种不同的方法测量
(Method
)。现在我想针对每个方法绘制Value
彼此,所以在这种情况下“基地 - 新”,“基地边缘”和“新边缘”。怎么样
我可以在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)
答案 0 :(得分:1)
因此,虽然这并不是您正在寻找的内容,但它很接近:我建议使用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)
给出了:
现在你可以添加例如自由领域的传奇。我的出发点是对this question of mine
的回答