Reputation: 10530
This is a question about the ggplot2
package (author: Hadley Wickham). I have existing ggplot objects with distinct colors (resp shapes, linetype, fill...) that I would like to map to a single color, e.g. black. What is the recommended approach?
Clarification: I have to work with these ggplot objects: I cannot re-make them
A ggplot with variables grouped as factors: this is the plot object p I need to work with
p <- ggplot(mtcars, aes(x = mpg, y = wt, group = factor(cyl), colour = factor(cyl))) +
geom_point(size = 5)
Several approaches I know of:
scale_colour_grey
hackp + scale_colour_grey(start = 0, end = 0) + # gives correct, useless legend
guides(color = FALSE)
The shorter p + scale_colour_grey(0,0)
does not work, you have to be explicit about start
and end
.
scale_colour_manual
with rep()
hackp + scale_colour_manual(values = rep("black",3)) # gives correct, useless legend
The simpler scale_colour_manual(values = "black")
does not work. This was probably the most intuitive approach. Having to specify the length of the vector makes it less attractive an approach.
geom_point()
recalledp + geom_point(colour = "black") + # gives incorrect legend
guides(color = FALSE)
It is well documented that the following is not allowed:
p + scale_colour_manual(colour = "black")
Error in discrete_scale(aesthetic, "manual", pal, ...) :
unused argument (colour = "black")
Upvotes: 3
Views: 2653
Reputation: 93871
If you just want to set the points to black and get rid of the color legend, I think you can just to this:
p + scale_colour_manual(values=rep("black",length(unique(mtcars$cyl))),
guide=FALSE)
Upvotes: 4
Reputation: 146050
Removing the color mapping directly seems to work:
p_bw = p
p_bw$mapping$colour = NULL
gridExtra::grid.arrange(p, p_bw)
Upvotes: 5