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)))
How to achieve a big shape and thin line in the legend?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With