Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot reduce line weight in legend

Tags:

r

legend

ggplot2

I am trying to control a plot using ggplot2

Sample Script:

dat1 <- data.frame(
     sex = factor(c("Female","Female","Male","Male")),
     time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
     total_bill = c(13.53, 16.81, 16.24, 17.42)
)
p = ggplot(data=dat1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) + geom_line() + geom_point()

Now I want to control how the shape and line looks like in the legend. I want a bigger shape, and thin line in the legend. But I can not perform the both.

If I do,

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

Both line and shape are thick, similar to Fig B.

If I do,

p = p + guides(colour = guide_legend(override.aes = list(size=5,linetype=0)))

Then shapes appear at correct size, but line disappears (Fig A). I tried something like this unsuccessfully.

p = p + guides(colour = guide_legend(override.aes = list(size=5,linetype=0.5)))

Example

How to achieve a big shape and thin line in the legend?

like image 882
max Avatar asked Mar 22 '16 18:03

max


1 Answers

Thanks Sandy Muspratt and user20650. Both the link was very useful.

I went with user20650's code just for simplicity.

Complete code here:

dat1 <- data.frame(
    sex = factor(c("Female","Female","Male","Male")),
    time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
    total_bill = c(13.53, 16.81, 16.24, 17.42)
)
p = ggplot(data=dat1, aes(x=sex, y=total_bill, group=time, shape=time, color=time)) 
p = p + geom_line() + geom_point(size=5, alpha=0) + geom_point(show.legend=FALSE) 
p = p + guides(colour = guide_legend(override.aes = list(alpha=1)))

Thanks.

like image 147
max Avatar answered Oct 04 '22 21:10

max