Ggplot2 custom legendary figures

When we have both the shape and the color legend in the scatter chart, the shape of the color legend is misleading:

foo <- data.frame( length=runif(10), height=runif(10), group=as.factor(sample(3,10,rep=T)), quality=as.factor(sample(2,10,rep=T)) ) ggplot(foo, aes(x = length, y = height, color=group, shape=quality)) + geom_point(size=5) 

This will create a graph below. As you can see, the β€œcircle” shape is reserved for quality==1 objects, however, in the group legend, all 3 groups are represented as a circle - with different colors, this can be misleading.

It would be much better if the group legend was represented by a form that has not yet been reserved for a specific purpose, for example, to fill the entire legend element with a specific color.

Do you have any simple idea how to solve this?

enter image description here

+4
source share
1 answer

You can change the properties of a legend manually using guides :

 ggplot(foo, aes(x = length, y = height, color=group, shape=quality)) + geom_point(size=5) + guides(colour = guide_legend(override.aes = list(shape = 15))) 

Just play around with the shape parameter to find the right shape.

enter image description here

+15
source

Source: https://habr.com/ru/post/1446993/


All Articles