【问题标题】:suppress legend from specific geom in multiple geom plot在多个几何图中抑制来自特定几何的图例
【发布时间】:2014-05-20 15:38:15
【问题描述】:

我有一个情节(如下),我试图控制它的图例。问题是我同时拥有geom_line(aes(linetype = season))geom_bar(aes(linetype = season))。我想显示geom_line 的图例(如下图1),而不是geom_bar 的图例(如下图2)。

如果我尝试使用scale_linetype(guide = F),它会关闭两者的图例。有没有办法显示geom_line 线型而不是geom_bar 线型的图例?下面是生成图的代码。

为什么?最终,我希望条形的轮廓与线条相匹配,并带有一个很好显示的图例。如您所见,情节 2 几乎就在那里,但传说是,嗯,难以辨认

# code for plot 1
ggplot() +
  geom_line(data = temp, aes(pos, res, linetype = season)) +
  geom_bar(data = temp2, aes(depth, res, fill = season), 
           stat = "identity", position = "dodge", color = "black") +
  scale_fill_manual(values = c("white", "black"), guide = F) 

# code for plot 2
ggplot() +
  geom_line(data = temp, aes(pos, res, linetype = season)) +
  geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season), 
           stat = "identity", position = "dodge", color = "black") +
  scale_fill_manual(values = c("white", "black"), guide = F)  

【问题讨论】:

  • 试试show_guide = FALSE
  • 是的,就是这样。无法将此标记为 joran 已回答,因为它是评论。谢谢!
  • 你自己写答案,然后你可以将它标记为已回答,并获得代表。

标签: r ggplot2


【解决方案1】:

由 joran 在下面的评论中回答。在geom_bar调用中使用了show_guide,代码如下所示

# plot 3
ggplot() +
  geom_line(data = temp, aes(pos, res, linetype = season)) +
  geom_bar(data = temp2, aes(depth, res, linetype = season, fill = season), 
           stat = "identity", position = "dodge", color = "black",
           show_guide = F) +
  scale_fill_manual(values = c("white", "black")) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多