【问题标题】:Color and shape aesthetics in facet_grid when margins presented呈现边距时 facet_grid 中的颜色和形状美学
【发布时间】:2018-08-19 23:21:40
【问题描述】:

考虑以下数据:

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) 的因素创造了一个额外的水平,这对我来说毫无意义。

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

【问题讨论】:

    标签: r ggplot2 margins facet-grid aesthetics


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 感谢您的回答。希望我不必为此添加额外的变量,但我想这是目前唯一的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多