facet_grid 呈现边距时的颜色和形状美学
Color and shape aesthetics in facet_grid when margins presented
考虑以下数据:
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)
这为两个因素创建了一个额外的级别,称为 (all),这对我来说毫无意义。
即使在边缘图上,我也想获得通过颜色和形状区分点的效果。
一个解决方案是创建一个变量来独立进行着色和刻面:
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)
考虑以下数据:
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)
这为两个因素创建了一个额外的级别,称为 (all),这对我来说毫无意义。
即使在边缘图上,我也想获得通过颜色和形状区分点的效果。
一个解决方案是创建一个变量来独立进行着色和刻面:
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)