简体   繁体   中英

Wrong fill values in a ggplot2 legend

I have a geom_point plot over a ggmap with separate styling for shape and fill of the points and with colour kept constant. Here's the code:

ggmap(m) + 
geom_point(data = d, 
           aes(x = Longitude, y = Latitude, fill = Phylum, shape = Placecount), 
               colour = 'black', size = 2) +
scale_shape_manual(values = c(21,22))

The result is satisfactory except for the legend, where 'Phylum' is wrongly associated with black throughout.

在此处输入图片说明

You can manually adjust which the shape values are used in your fill legend (or any legend) with + guides(fill = guide_legend(override.aes = list(shape = 21))) . It looks like the default is shape = 1, which doesn't have support for fill, so you are just getting the solid black shapes.

Here is an example with mtcars:

ggplot(mtcars) +
    geom_point(aes(hp, mpg, fill = as.factor(cyl), shape = as.factor(am))) +
    scale_shape_manual(values = c(21,22)) +
    guides(fill = guide_legend(override.aes = list(shape = 21)))

plot with override_aes() :

在此处输入图片说明

plot without:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM