Legend not clear with colour aesthetic and many levels of geom_point

32 views
Skip to first unread message

Sam Albers

unread,
Nov 28, 2014, 1:23:24 PM11/28/14
to ggplot2
Hello there,

I am wondering it is possible to modify and ggplot2 legend such that the small grey boxes on the letter hand side on the plot that have points in them with the example below can be changed to fill the entire box with the corresponding legend colour while still maintaining points in the plot. I tried using the fill aesthetic hoping that might work but to no avail.

Basically I have a plot that has several factor levels and the little tiny point in the legend don't show up very nicely so I want to make the colour a bit clear.

Here is the example code:

ggplot(iris, aes(x=Sepal.Length,y=Petal.Length, colour=Species)) +
  geom_point(aes(fill=Species))


Thanks in advance!

Sam

Dennis Murphy

unread,
Nov 28, 2014, 4:17:45 PM11/28/14
to Sam Albers, ggplot2
Hi:

The fill aesthetic in geom_point() only works for a few point shapes
(21-25), but it wouldn't change the result in the legend. This is not
exactly what you asked for, but one alternative is to increase the
point size in the legend, which you can do using guide_legend().
Extending your example,

library(ggplot2)
ggplot(iris, aes(x=Sepal.Length,y=Petal.Length, colour=Species)) +
geom_point() +
guides(colour = guide_legend(override.aes = list(size = 5)))

To manually fill the legend boxes post-hoc would be a non-trivial
task, although it may be possible with some combination of gtable and
grid graphics.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sam Albers

unread,
Dec 3, 2014, 7:13:41 PM12/3/14
to Dennis Murphy, ggplot2
Sorry for the late reply but building on Dennis's example, I could do something like this:


ggplot(iris, aes(x=Sepal.Length,y=Petal.Length, colour=Species)) +
  geom_point() +
  guides(colour = guide_legend(override.aes = list(size = 9, shape=15)))

Thanks for the help.

Sam
Reply all
Reply to author
Forward
0 new messages