Consider the following data:
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))
I am creating a scatter plot in a grid for each combination of a and b with margins.
library(ggplot2)
ggplot(data, aes(x = x, y = y)) +
geom_jitter(aes(color = b, shape = a)) +
facet_grid(a ~ b, margins = T)
This creates an extra level for both factors called (all), which for me is pointless.
I would like to obtain the effect where points are distinguished by color and shape even on marginal plots.
A solution is to create a variable to do the coloring and faceting independently:
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)