考虑以下数据:
n <- 1000
data <- data.frame(id = 1:n,
a = sample(c("a1", "a2"), n, replace = T),
b = sample(c("b1", "b2", "b3"), n, replace = T),
x = rnorm(n),
y = rnorm(n))
我正在网格中为带有边距的 a 和b的每个组合创建一个散点图。
library(ggplot2)
ggplot(data, aes(x = x, y = y)) +
geom_jitter(aes(color = b, shape = a)) +
facet_grid(a ~ b, margins = T)
这为这两个因素创造了一个额外的水平,称为(全部(,这对我来说毫无意义。
我想获得即使在边缘图上也通过颜色和形状区分点的效果。
一个解决方案是创建一个变量来独立进行着色和刻面:
n <- 1000
data <- data.frame(id = 1:n,
a = sample(c("a1", "a2"), n, replace = T),
b = sample(c("b1", "b2", "b3"), n, replace = T),
x = rnorm(n),
y = rnorm(n)) %>%
mutate(a_ = factor(a),
b_ = factor(b))
library(ggplot2)
ggplot(data, aes(x = x, y = y)) +
geom_jitter(aes(color = b, shape = a)) +
facet_grid(a_ ~ b_, margins = T)