1
votes

Working with RStudio 0.98.1103, I am creating two versions of exactly the same graph: One with colors and one without. Since both graphs are exactly the same (apart from the coloring) I want to avoid typing nearly the same commands again. Hence, I create the colored plot, save it, manipulate it to make it black-grey-white and save the reduced version:

library(ggplot2)

bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight)) +
  geom_line(aes(color=group)) + theme(legend.position="none")
bp_bw <- bp + theme_bw() +
  geom_line() + theme(legend.position="none")
ggsave("bp_bw.png", bp_bw)

Although bp looks quite normal, bp_bw doesn't. There is still a blury color shining behind the black bars (red - green - blue):

enter image description here

Closeup:

enter image description here

How can I get rid of this colors, i.e. remove all color completely from bp? Only restriction: I have to create the colored graphs first (although of course a different order would work).

2
I don't see the blurry color color you're talking about? The picture you attached looks pretty normal to me as does the plot I see when I run the code myself. Can you be a bit more specific about what you're trying to eliminate? - Señor O
I couldn't replicate it either. When I save both plots as png, there is no visible color even when I zoom in a lot. How are you saving them? - Heroka
@SeñorO and Heroka: I've added a closeup. The color is really there. - MERose
@MERose no worries. I don't know why this is happening and I cannot replicate it. I tried with multiple file-formats, but especially with png I still get sharp lines when I zoom. Maybe a tangent, but does the issue persist when you open/view the image in another program? - Heroka
Odd - I am getting sharp lines when I save it as well. - Señor O

2 Answers

3
votes

I think a better solution is to create a base and only add the coloring part when needed:

bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight)) +
  theme_bw() + theme(legend.position="none")

bp_col <- bp + geom_line(aes(color=group))
bp_bw <- bp + geom_line()
1
votes

This (more-or-less) makes sense. Your bp_bw code doesn't get rid of the old colored lines, it just adds black lines on top. Anti-aliasing as the image is displayed/saved lets some of the color through on the edges.

My recommendation is to modify the color scale rather than overplot black on top:

bp_bw2 = bp + scale_color_manual(values = rep("black", 20)) + theme_bw()

This will change the colors to all black rather than plotting black on top of colors. The rep("black", 20) is kind of a hack. Apparently values aren't recycled by scale_color_manual, but extra values aren't used so you need to give it a vector at least as long as the number of colors.

This also has the advantage of not needing to repeat the geom call, and if you had previously defined a color scale this will overwrite it. If you want to be more general you could also add a scale_fill_manual(), and you probably want to specify guide = FALSE so that you don't get a very unhelpful legend.

You also might want to check out scale_colour_grey, just because it's B&W doesn't mean all the colors have to be the same.