0

I would like to plot in black and white with ggplot2 however I don't want to use shape (ie solid black vs open black outline) because I need the shape to describe another group.

library(ggplot2)
str(mtcars)
p <- ggplot(data = mtcars, aes(x = wt, y=mpg, col=factor (vs), shape= factor (cyl) ))
p + geom_point(size=10) +

theme_bw() +
  theme(legend.position="bottom", legend.title=element_blank(), legend.key = element_blank(),
        axis.text.x = element_text(size=17),
        axis.text.y = element_text(size=17),
        axis.title.x = element_text(size=20),
        axis.title.y     = element_text(size=20), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        legend.text      =element_text(size=22)
  ) + 
  scale_colour_manual(values = c("red", "blue"))

The plot looks like this. enter image description here

I would like to have 0 and 1 be plotted as black and white (black outline) however in this case its difficult since the shape has already been taken with factor (vs). Is there any other thing I can do? thanks.

2
  • You could still use color, but make 0 light grey and 1 black, and it would still print / display fairly well in B&W
    – bouncyball
    Jun 26, 2017 at 18:17
  • @bouncyball thanks; I think I will have to plot it this way if there is no other solution.
    – Ahdee
    Jun 26, 2017 at 18:21

2 Answers 2

4

If you want two separate legends for the two factors as you have in your example, you can use "fillable" shapes and the fill aesthetic instead of the color aesthetic. Shapes are shown here; the fillable ones are the ones in yellow, 21-25.

To get your legends to look how you want them, particularly the fill legend, you can override the shape via override.aes in guide_legend. Here I also fill the shape legend in black, but that isn't necessary if you don't mind the white legend.

ggplot(data = mtcars, aes(x = wt, y=mpg, fill = factor(vs), shape = factor (cyl) 
)) + 
     geom_point(size=10) +
     theme_bw() +
     scale_fill_manual(values = c("black", "white")) +
     scale_shape_manual(values = c(21, 24, 22) ) +
     guides(fill = guide_legend(override.aes = list(shape = 21) ),
            shape = guide_legend(override.aes = list(fill = "black" ) ) )

enter image description here

1
  • thank you! that works perfectly. I especially like the extra legend guide, would not have worked without. Cheers.
    – Ahdee
    Jun 26, 2017 at 18:51
0

Here's a solution:

str(mtcars)
p <- ggplot(data = mtcars, aes(x = wt, y=mpg, shape=paste0(vs,cyl) ))
p + geom_point(size=10) +

    theme_bw() +
    theme(legend.position="bottom", legend.title=element_blank(), legend.key = element_blank(),
          axis.text.x = element_text(size=17),
          axis.text.y = element_text(size=17),
          axis.title.x = element_text(size=20),
          axis.title.y     = element_text(size=20), 
          panel.grid.major = element_blank(), 
          panel.grid.minor = element_blank(),
          panel.background = element_blank(), 
          legend.text      =element_text(size=22)
    )+scale_shape_manual(values = c("04"=15,"06"=16,"08"=17,"14"=0,"16"=1,"18"=2))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.