繁体   English   中英

ggplot2:使用stat_smooth时无法覆盖第二个图例美观度

[英]ggplot2: cannot override 2nd legend aesthetics when using stat_smooth

南瓜头盔(3.1.2),ggplot2_1.0.0。

library(ggplot2)
some_diamonds <- subset(diamonds, color %in% c('D', 'E', 'F'))
g <- ggplot(some_diamonds, aes(carat, depth, linetype=cut, colour=color)) +
    stat_smooth(se=F) + scale_colour_grey()

所需的颜色美感在color的图例中正确显示(并且至关重要的是,在图本身中),但是cut的颜色默认为stat_smooth的默认值(蓝色)。 通常的技巧在这种情况下似乎不起作用,例如:

g <- ggplot(some_diamonds, aes(carat, depth, linetype=cut, colour=color)) + 
   stat_smooth(se=F) 
g + scale_colour_grey() + guides(colour = guide_legend(override.aes = 
   list(colour='black')

实际上,我什至不知道如何访问第二个图例的任何元素。 这个

g + scale_colour_grey(guide=guide_legend(title="Title", order=2)) 

只需将第一个图例的标题更改为“标题”; 同样

g + scale_colour_grey() + 
  guides(colour=guide_legend(override.aes=list(colour='black'), order=2))

无论order的值如何(甚至省略),都覆盖cut而不是color

我想念什么?

您已经非常接近了! 在这里有用的只是简单地将guides()参数的名称更改为您要更改的功能的名称,即linetype

g + guides(linetype = guide_legend(title = "Variable: cut",  # changes the second legend
                                   override.aes = list(colour = "black")), 
           color = guide_legend(title = "Variable: color"))  # changes the first legend

更改图例颜色的情节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM