Symbol Size in Legend Key

4,667 views
Skip to first unread message

astudent

unread,
May 18, 2011, 6:16:26 PM5/18/11
to ggplot2
Hi,

It seems that the default is that the size of the colors in the legend
is inherited from the size of the points point in the plot. For
example, in the attached code, the points in the legend are hard to
see. Is there a way to override this or to make the legend colors
more visible?

N = 10^4
a = seq(as.Date("1970-01-01"), by="day", length.out = N)
x = data.frame(date = a, x1 = 10/N*(1:N) + rnorm(N), x2 = 20/N*(1:N) +
rnorm(N))
x.m = melt(x, id.vars="date")
qplot(date, value,color=variable,data=x.m, size=I(1/2)) +
opts(legend.key.size = unit(c(1.2, 2.4), "lines"))

Thanks!

Dennis Murphy

unread,
May 18, 2011, 7:31:37 PM5/18/11
to astudent, ggplot2
Hi:

I don't think you'll be able to trick it with the opts() statement you
used, but this worked for me (pure ad hockery :)

qplot(date, value, color=variable, data=x.m, size=I(1/2)) +
geom_smooth(aes(color = variable), alpha = 0, size = 1, se = FALSE)

alpha represents alpha transparency - lower values make it more
transparent. alpha = 0 makes the line/curve disappear :) Using size =
1 thickens the line enough to overwrite the small dot in the legend.
By default, geom_smooth() will plot standard error envelopes around
the curve; se = FALSE suppresses them.

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

astudent

unread,
May 18, 2011, 8:00:00 PM5/18/11
to ggplot2
Thank you very much Dennis.

Did the alpha parameter work for you in hiding the smoother?

When I try your code, it does make the legend better, but then the
plot shows the smoother along with the points. I tried messing with
the alpha parameter but it didn't change the plot.

N = 10^4
a = seq(as.Date("1970-01-01"), by="day", length.out = N)
x = data.frame(date = a, x1 = 10/N*(1:N) + rnorm(N), x2 = 20/N*(1:N) +
rnorm(N))
x.m = melt(x, id.vars="date")
qplot(date, value, color=variable, data=x.m, size=I(1/2)) +
geom_smooth(aes(color = variable), alpha = 0, size = 1, se = FALSE)


Dennis Murphy

unread,
May 18, 2011, 9:43:00 PM5/18/11
to astudent, ggplot2
Hi:

Drat. I can see the smooths now - the eyes aren't what they used to
be, sorry. There is an alpha = argument in geom_smooth(), but as you
noted, it doesn't have an effect on the transparency of the smooths -
alpha = 0 looked the same as alpha = 1. In retrospect, if it had
worked, the transparency would have transferred to the legend as well;
that issue has come up before in this group.

Unfortunately, without such a trick it will be hard to resize the
points in the legend, because you set the point size in the qplot()
call and that gets transferred from the plot to the legend. None of
the opts() involving legend, AFAIK, allows modification of the
aesthetic itself (in this case, the points) and scale_colour_manual()
doesn't have an option for modifying the size of the point. I hope I'm
wrong about that and corrected, because such a feature would be useful
to know.

I tried to create a factor sz with three levels for a size aesthetic
but with a constant value 0.5 for each observation, but couldn't get
rid of the second legend even though I tried about five different ways
I know that have worked in other contexts. One in particular that I
thought should have worked but didn't was

qplot(date, value, color=variable, data=x.m2, size = sz) +
scale_size(legend = FALSE)

I'm out of ideas for trying to trick ggplot2 in that sense.

If no correction is forthcoming and you can live with the smooths in
the plot, then you can get the thicker lines in the legend. Without
them, you may be out of luck - but as I said, I hope I'm wrong in that
opinion. I'm aware that there is some development work going on to
'decouple' the legends from the plot, but it is not implemented yet in
the released version of ggplot2.

Dennis

Kohske Takahashi

unread,
May 18, 2011, 10:12:19 PM5/18/11
to Dennis Murphy, astudent, ggplot2
Hi

At this time, it is impossible to directly specify the size (and the
other drawing parameters) of the geoms in legends.
As a dirty workaround, you can directly edit the grob objects using
grid editing function.

N = 10^3


a = seq(as.Date("1970-01-01"), by="day", length.out = N)
x = data.frame(date = a, x1 = 10/N*(1:N) + rnorm(N), x2 = 20/N*(1:N) +
rnorm(N))
x.m = melt(x, id.vars="date")
qplot(date, value,color=variable,data=x.m, size=I(1/2)) +
opts(legend.key.size = unit(c(1.2, 2.4), "lines"))

# edit the key.points
grid.gedit(size=unit(5, "mm"), "key.points", grep=T)


Probably the next version of ggplot2 will get this ability directly
specifying the appearance of legends.

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Dennis Murphy

unread,
May 18, 2011, 10:30:40 PM5/18/11
to Kohske Takahashi, ggplot2
Thanks, Kohske!

Dennis

Nate Coehlo

unread,
May 19, 2011, 12:41:19 AM5/19/11
to Kohske Takahashi, Dennis Murphy, ggplot2
This grid.gedit solution works perfect.

Thanks!

On Wed, May 18, 2011 at 7:12 PM, Kohske Takahashi <takahash...@gmail.com> wrote:

astudent

unread,
Sep 3, 2012, 4:37:51 PM9/3/12
to ggp...@googlegroups.com, Kohske Takahashi, Dennis Murphy
I've run into this issue again, and the solution from this thread is giving me an error now [1].

Does anyone know of a way to make the points in the legend bigger while leaving the points in the plot small?

Thanks!

[1] # current error message

> N = 10^3
> a = seq(as.Date("1970-01-01"), by="day", length.out = N)
> x = data.frame(date = a, x1 = 10/N*(1:N) + rnorm(N), x2 = 20/N*(1:N) +
+ rnorm(N))
> x.m = melt(x, id.vars="date")
> qplot(date, value,color=variable,data=x.m, size=I(1/2)) +
+ opts(legend.key.size = unit(c(1.2, 2.4), "lines"))
>
> # edit the key.points
> grid.gedit(size=unit(5, "mm"), "key.points", grep=T)
Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) :
  'gPath' (key.points) not found
>>> > To unsubscribe: email ggplot2+unsubscribe@googlegroups.com

>>> > More options:http://groups.google.com/group/ggplot2
>>
>> --
>> You received this message because you are subscribed to the ggplot2 mailing list.
>> Please provide a reproducible example: http://gist.github.com/270442
>>
>> To post: email ggp...@googlegroups.com
>> To unsubscribe: email ggplot2+unsubscribe@googlegroups.com

>> More options: http://groups.google.com/group/ggplot2
>>
>
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com

Andreas Christoffersen

unread,
Sep 3, 2012, 5:27:24 PM9/3/12
to astudent, ggp...@googlegroups.com
How about keywidth and keyheight from guide_legend http://had.co.nz/ggplot2/docs/guide_legend.html


HTH
Andreas

Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com

Roey Angel

unread,
Sep 21, 2012, 5:17:26 AM9/21/12
to ggp...@googlegroups.com, astudent
keywidth and keyheight will only changes the size of the grey box surrounding each legend key.
To change the size (or colour or alpha) of the actual legend keys you should use the 'override.aes' option.
e.g.
guides(colour=guide_legend(override.aes = list(size = 4)))

Roey
Andreas

>>> > 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 ggplot2 mailing list.
>> Please provide a reproducible example: http://gist.github.com/270442
>>
>> 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 ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Dan Oele

unread,
Aug 29, 2013, 4:05:48 PM8/29/13
to ggp...@googlegroups.com, Kohske Takahashi, Dennis Murphy
I am having a similiar issue w/ the legend symbol sizes. How do you manipulate the line sizes from geom_line() figure inside the legend? I cannot seem to get the grid edit and guide overrides to work

thanks in advance

>>> > 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 ggplot2 mailing list.
>> Please provide a reproducible example: http://gist.github.com/270442
>>
>> 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 ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com

Ista Zahn

unread,
Aug 29, 2013, 7:31:32 PM8/29/13
to Dan Oele, ggplot2, Kohske Takahashi, Dennis Murphy
1. Use the override.aes approach.
2. We cannot know what the problem is without more information. What
version of ggplot2? What is an example of the approach you've tried
that doesn't work?

Best,
Ista
> --
> --
> 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/groups/opt_out.

Dan Oele

unread,
Aug 30, 2013, 10:22:18 AM8/30/13
to Ista Zahn, ggplot2, Kohske Takahashi, Dennis Murphy
thanks for the response, 

here is an example from the mtcars dataset. I can't seem to figure out how to manipulate the size of the lines within the legend. I have changed the position and size of the legend, the size and color of the boxes, key, etc. How do I change the size of symbols, in this case lines, within legend 

p<-ggplot(mtcars,aes(x=hp,y=cyl))+geom_line(aes(colour=as.factor(cyl)),size=2)
p
 
adding + guides(colour = guide_legend(override.aes = list(alpha = 1))) #only changes the visibility, adding " size=12" seems to do nothing.

thanks in advance and hopefully this is more clear. 

I am using ggplot2 v 9.3.1 and R v3.01. " good sport"


--
Dan Oele
Graduate Student
Center For Limnology 
University of Wisconsin-Madison
680 North Park Street
Madison, WI 53706

Ista Zahn

unread,
Aug 30, 2013, 11:02:35 AM8/30/13
to Dan Oele, ggplot2, Kohske Takahashi, Dennis Murphy
On Fri, Aug 30, 2013 at 10:22 AM, Dan Oele <oe...@wisc.edu> wrote:
>
> thanks for the response,
>
> here is an example from the mtcars dataset. I can't seem to figure out how to manipulate the size of the lines within the legend. I have changed the position and size of the legend, the size and color of the boxes, key, etc. How do I change the size of symbols, in this case lines, within legend
>
> p<-ggplot(mtcars,aes(x=hp,y=cyl))+geom_line(aes(colour=as.factor(cyl)),size=2)
> p
>
> adding + guides(colour = guide_legend(override.aes = list(alpha = 1))) #only changes the visibility, adding " size=12" seems to do nothing.

Are you sure?

p + guides(colour = guide_legend(override.aes = list(size=12)))

works as expected for me, using the same versions of R and ggplot2.

Best,
Ista
Reply all
Reply to author
Forward
0 new messages