Didn"t forge 2013-01-30 12:49 采纳率: 25%
浏览 218
已采纳

把一些传奇故事关在情节里

Suppose I have a ggplot with more than one legend.

mov <- subset(movies, length != "")
(p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
  geom_point()
)

I can turn off the display of all the legends like this:

(p1 <- p0 + theme(legend.position = "none"))

Passing show_guide = FALSE to geom_point (as per this question) turns off the shape legend.

(p2 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
  geom_point(show_guide = FALSE)
)

But what if I want to turn off the colour legend instead? There doesn't seem to be a way of telling show_guide which legend to apply its behaviour to. And there is no show_guide argument for scales or aesthetics.

(p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
  scale_colour_discrete(show_guide = FALSE) +
  geom_point()
)
# Error in discrete_scale

(p4 <- ggplot(mov, aes(year, rating, shape = mpaa)) +
  aes(colour = length, show_guide = FALSE) +
  geom_point()
)
#draws both legends

This question suggests that the modern (since ggplot2 v0.9.2) way of controlling legends is with the guides function.

I want to be able to do something like

p0 + guides(
  colour = guide_legend(show = FALSE) 
)

but guide_legend doesn't have a show argument.

How do I specify which legends get displayed?

转载于:https://stackoverflow.com/questions/14604435/turning-off-some-legends-in-a-ggplot

  • 写回答

2条回答 默认 最新

  • 笑故挽风 2013-01-30 12:55
    关注

    You can use guide=FALSE in scale_..._...() to suppress legend.

    For your example you should use scale_colour_continuous() because length is continuous variable (not discrete).

    (p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
       scale_colour_continuous(guide = FALSE) +
       geom_point()
    )
    

    Or using function guides() you should set FALSE for that element/aesthetic that you don't want to appear as legend, for example, fill, shape, colour.

    p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) +
      geom_point()    
    p0+guides(colour=FALSE)
    

    UPDATE

    Both provided solutions work in new ggplot2 version 2.0.0 but movies dataset is no longer present in this library. Instead you have to use new package ggplot2movies to check those solutions.

    library(ggplot2movies)
    data(movies)
    mov <- subset(movies, length != "")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错