In ggplot2, how to choose which geome appears in the legend?

Some geometry hides the key to other geometries in the legend (especially boxplot)

How to choose geometry in the legend?

For instance:.

qplot(data=CO2, x=Type, y=uptake, colour=Plant, shape=Treatment)+ geom_boxplot() 

enter image description here

Switching the order of geometers helps

 qplot(data=CO2, x=Type, y=uptake, colour=Plant, shape=Treatment, geom="boxplot")+ geom_point() 

enter image description here

But I would like the legend to be found with

 qplot(data=CO2, x=Type, y=uptake, colour=Plant, shape=Treatment) 

enter image description here

Do I need to extract the legend of one plot and paste it into another using something like gridExtra?

+4
source share
1 answer

You can suppress the legend for boxplot by adding show_guide=FALSE to the geom_boxplot() call. You still get the legend of the dots.

 qplot(data=CO2, x=Type, y=uptake, colour=Plant, shape=Treatment)+ geom_boxplot(show_guide=FALSE) 

enter image description here

If you haven’t drawn the dots yet (i.e. you just had a boxplot, but you wanted the legend to appear with a dot symbol and not with a boxplot symbol), this is harder, although I think it is possible.

+6
source

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


All Articles