控制ggplot中不是颜色指南的图例元素的颜色

Mat*_*agg 7 r ggplot2

当使用带有黑色背景的ggplot2主题时,是否可以控制颜色指南以外的指南的图例颜色,以便不用黑色绘制内容?如果是这样,怎么样?

library(ggplot2) # needs to be 0.9.3 for this theme
data(iris)       # included with ggplot2

theme_black<- function (base_size = 16, base_family = ""){
    theme_minimal() %+replace% 
        theme(
              line = element_line(colour = "white", size = 0.5, linetype = 1, 
                        lineend = "butt"), 
              rect = element_rect(fill = "white", 
                        colour = "white", size = 0.5, linetype = 1), 
              text = element_text(family = base_family, 
                        face = "plain", colour = "white", size = base_size,
                        angle = 0, lineheight = 0.9, hjust = 0, vjust = 0),
              plot.background = element_rect(colour = 'black', fill = 'black'),
              plot.title = element_text(size = rel(1.2)),
              panel.border = element_rect(fill = NA, colour = "white"), 
              panel.grid.major = element_line(colour = "grey20", size = 0.2), 
              panel.grid.minor = element_line(colour = "grey5", size = 0.5),
              strip.background = element_rect(fill = "grey30", colour = "grey30")
             )
    }

ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, shape=Species,
       colour=Petal.Length))+geom_point()+theme_black()+
       scale_colour_gradient(low = "purple", high = "white")
Run Code Online (Sandbox Code Playgroud)

如您所见,图例的形状部分的默认颜色尚未更改,因此它是不可见的,无法分辨哪个物种是哪个:

在此输入图像描述

我现在唯一的解决方案是改变legend.background颜色,但这是浪费墨水和丑陋.

use*_*1_G 1

一种方法是添加两个额外的geom_points,逻辑是:

为图例绘制白点,用没有图例的黑点覆盖它们,然后绘制没有图例的彩色点,例如

geom_point(colour="white",size=1) + 
geom_point(colour="black",size=3,show_guide=FALSE) +
geom_point(show_guide=FALSE)
Run Code Online (Sandbox Code Playgroud)