2

考虑以下数据:

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))

我正在为带有边距的ab的每个组合在网格中创建一个散点图。

library(ggplot2)
  ggplot(data, aes(x = x, y = y)) +
  geom_jitter(aes(color = b, shape = a)) +
  facet_grid(a ~ b, margins = T)

这为两个称为(all)的因素创造了一个额外的水平,这对我来说毫无意义。

我想获得即使在边缘图上也可以通过颜色和形状来区分点的效果。

4

1 回答 1

0

一种解决方案是创建一个变量来独立进行着色和刻面:

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)
于 2018-08-20T08:03:37.947 回答