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