AnjaM
AnjaM

Reputation: 3039

Coding three conditions in scatter plot in ggplot2

I'd like to make a ggplot2 scatter plot (geom_point) where the points code for three different characteristics. So I thought of using shape, colour and fill (by using shapes 21 to 25 that can be filled). Although the plot itself looks fine, the legend doesn't show the fill colours (all the points appear black). Here is an MWE, note the black symbols for carb:

p <- ggplot(mtcars, aes(wt, mpg, shape = factor(gear),
                    fill = factor(carb), colour = factor(cyl)))
p + geom_point(size = 3) + scale_shape_manual(values = 21:25)

Is there a way to change the fill-legend so that it shows the real colours?

Upvotes: 2

Views: 888

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226532

I think this works, changing the shape in the fill legend to 21:

p + geom_point(size = 3) + 
    scale_shape_manual(values = 21:25)+
      scale_fill_discrete(guide=guide_legend(override.aes=list(shape=21)))

Upvotes: 1

Related Questions