Вопрос

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?

Это было полезно?

Решение

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)))
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top