Increasing line size in color legend when using both color and size aesthetics?

6,429 views
Skip to first unread message

Jeff Marcus

unread,
Dec 4, 2013, 10:35:44 PM12/4/13
to ggp...@googlegroups.com
I can't figure out how to increase line thickness in a legend where I am using both color and size aesthetics. Here is sample code:

 df <-data.frame(x.left=1:8, x.right=1:8, y.bot=2:9, y.top=3:10, color.var=c(rep("a", 4), rep("b", 4)), width.var=rep(c("c", "d"),,4))
#> df
#  x.left x.right y.bot y.top color.var width.var
#1      1       1     2     3         a         c
#2      2       2     3     4         a         d
#3      3       3     4     5         a         c
#4      4       4     5     6         a         d
#5      5       5     6     7         b         c
#6      6       6     7     8         b         d
#7      7       7     8     9         b         c
# 8      8       8     9    10         b         d

 gp <- ggplot(df) + geom_segment(aes(x=x.left,xend=x.right, y=y.bot, yend=y.top, color=color.var, size=width.var))
print (gp)
This produces the attached plot thin_legend_lines.png where the width of the lines in the color.var legend is very thin and hard to distinguish in my real use case where I am using 7 colors. 

I tried two things:Increasing legend key size:
(A) Increasing legend key size

require("grid")
gp <- gp + theme(legend.key.size=unit(1,"cm"))
print (gp)

That increased the size of the background in the legend but didn't change the line width. 

(B) Making line size constant.
gp <- ggplot(df) + geom_segment(aes(x=x.left,xend=x.right, y=y.bot, yend=y.top, color=color.var, size=width.var), size=5)

That actually did make the legend lines have size 5 but overrode my size aesthetics setting (i.e., all lines were of size 5).(I expected this but gave it a shot anyways). 

Any ideas on how to preserve the size aesthetic while making the lines in the color legend thicker? Thanks. 

  Jeff



thin_legend_lines.png

Dennis Murphy

unread,
Dec 5, 2013, 1:57:22 PM12/5/13
to Jeff Marcus, ggplot2
Hi:

Is
gp + guides(color = guide_legend(override.aes = list(size = 2)))

along the lines of what you were after?

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

Jeff Marcus

unread,
Dec 5, 2013, 2:13:05 PM12/5/13
to Dennis Murphy, ggplot2
Exactly! Thanks very much. 

 Jeff

Saurajyoti Kar

unread,
Jul 16, 2018, 3:46:45 PM7/16/18
to ggplot2
Thanks so much!!

Saurajyoti

Jason Brannock

unread,
Jun 20, 2022, 3:52:50 PM6/20/22
to ggplot2
Hello! I know this is a pretty old thread, but I'm running into almost this exact issue and I haven't been able to find any solutions. In my ggplot2 plot, I've got three facets, each with its own color scheme (the default color scheme) rather than a continuation of colors, so each facet has red, blue, and green and each legend is labeled with the proper category (see below). This was accomplished with new_scale_color() after each "data = ~ subset(., filter)" geom_line layer. However, I'm publishing this content in a bookdown and when it's published, those tiny legend lines are almost non-existent. 

I've tried using legend.key and using guides(color = guide_legend(override.aes = list(size = 50)) and neither work. Legend.key made the lines a little bigger, but not enough. If I remove the last new_scale_color(), override.aes does change the lines how I want, but only for the last legend. I need the lines bigger for all three legends. 

image.png

Thank you,
Jason

This email message, including attachments, may contain private, proprietary, or privileged information and is the confidential information and/or property of GRAIL, LLC, and is for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is strictly prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.

Reply all
Reply to author
Forward
0 new messages