【问题标题】:show.legend = FALSE not working for two geom_pointsshow.legend = FALSE 不适用于两个 geom_points
【发布时间】:2021-06-05 23:59:53
【问题描述】:

当我合并两个geom_points 时,我似乎无法使用show.legend = FALSE 压制图例。单独来看似乎很奇怪,他们工作正常。

Plot 1 with 4 unique shapes using interaction

Plot 2 with 2 unique shapes

Plot 3 with both geom_points gives 6 unique shapes when show.legend = FALSE for plot 2

最终,我只想要情节 1 的传说。我已将 theme_bw() 放在它的开头和其他所有内容,但似乎没有任何效果......帮助!我的代码如下,

  theme_bw() +
  geom_point(data1,
             mapping = aes(x = x, y = y, shape = interaction(a, b)),
             size = 6,
             alpha = 1,
             position = position_nudge(ifelse(data1$b == "b1", -0.3, 0.3)),
             show.legend = TRUE) +
  geom_point(data2,
             mapping = aes(x=x, y=y, shape = a),
             alpha = 0.6,
             size = 2.5,
             position = position_dodge(width = 0.15),
             show.legend = FALSE) +
   labs(x = "x", y = "y") +
   scale_x_discrete(labels = c("cond1","cond2", "cond3"))

【问题讨论】:

    标签: r ggplot2 plot data-visualization


    【解决方案1】:

    对于尝试此操作的任何人,上述方法都行不通,但一位伙伴使用一种 hacky 方式使其工作(即过滤数据并创建另一个 geom_point)。具体来说,

    geom_point(data2 %>% filter(a == "a1"),
                 mapping = aes(x=x, y=y),
                 shape = "circle",
                 alpha = 0.6,
                 size = 2.5,
                 position = position_nudge(-0.15),
                 show.legend = FALSE) +
    geom_point(data2 %>% filter(a == "a2"),
                 mapping = aes(x=x, y=y),
                 shape = "square",
                 alpha = 0.6,
                 size = 2.5,
                 position = position_nudge(0.15),
                 show.legend = FALSE)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-16
      • 2018-10-30
      • 2020-05-21
      • 2014-02-26
      • 2019-01-25
      • 2014-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多