When using a ggplot2 theme with a black background, can one control the legend colour for guides other than the colour guide so that things are not plotted in black? If so, how?
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")
As you can see, the default colour for the shape part of the legend has not been changed, so it is invisible and one cannot tell which species is which:
The only solution I have right now is to change the legend.background colour, but this is a waste of ink and ugly.
The short answer seems to be that there is no perfect way to do this.
We can add hidden layers with shape guides as per @user1317221_G's answer, which I accepted. But it is extra computation and if we save as pdf, I expect these hidden layers will be present.
Alternatively, we can override the shape guide's colour, as @bdemarest suggests in comments:
+ guides(shape=guide_legend(override.aes=list(colour="white"))
But this still forces us to add theme-specific code beyond just the +theme_black()
I've implemented a slightly more elaborate version of this because I set a default theme for the session depending on whether I am making plots for paper (grey/white background) or screen (black background). So my approach is to run something along these lines early in the session:
set_theme(theme_black); defaultcol = "white" # for slides
# or
set_theme(theme_bw); defaultcol = "black" # for paper
followed by a ggplot() that includes this:
+ guides(shape=guide_legend(override.aes=list(colour=defaultcol))
This has the advantage of minimizing the need for theme-specific adjustments to plots, though it is not as good as being able to control the default ggplot2 colour with a theme.
One way would be to add two extra geom_point
s, the logic would be:
plot white points for the legend, cover them with black points with no legend, then plot your coloured points with no legend,e.g.
geom_point(colour="white",size=1) +
geom_point(colour="black",size=3,show_guide=FALSE) +
geom_point(show_guide=FALSE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With