4

I'm finding the combined legend for shape and linetype to be difficult to decipher. specifically, the shape is hard to see because it is behind the line and it is too small.

library(ggplot2)
ggplot(mtcars)+
geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)))+
geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
theme_bw(base_size=18)

How do I increase the size of the shapes in the legend without increasing the size of the line?

this attempt below increases the size for both (not what I want). the order of the guide_legend also does not seem to affect the order of the symbols in the legend keys. Changing the order of the geom_point and geom_smooth would give the desired result in the legend but not in the plot.

+guides(linetype=guide_legend('Cylinders'),shape=guide_legend('Cylinders',override.aes=list(size=3)))

I was also hoping theme(legend.key.size=grid::unit(2,'cm')) would scale up the size of the objects in the legend key but it doesn't appear to do so.

suggestions? also open to other ideas how to make the graph more legible.

Dominik
  • 782
  • 7
  • 27
  • The question has been asked before ([can-ggplot2-control-point-size-and-line-size-lineweight-separately-in-one-legend](http://stackoverflow.com/questions/25007324/can-ggplot2-control-point-size-and-line-size-lineweight-separately-in-one-lege/34304453#34304453)). In addition to @Sam Dickson's answer below, others might be interested in answers provided to the earlier question. – Sandy Muspratt Dec 16 '15 at 05:31

1 Answers1

2

The legend produces the lines and points in the order that they are plotted, so in order to get the points in front of the line you could do this:

ggplot(mtcars)+
  geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
  geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)))+
  theme_bw(base_size=18)

enter image description here

Changing the size of the point in the legend is a little more frustrating. Maybe you want to try a hack that allows you to take a legend off of one plot and put it on another:

library(gtable)
library(gridExtra)

# Has the legend you want
p1 <- ggplot(mtcars)+
  geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
  geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)),size=3)+
  theme_bw(base_size=18)+labs(shape="Cylinders",linetype="Cylinders")

# Has the plot you want
p2 <- ggplot(mtcars)+
  geom_smooth(aes(x=hp,y=mpg,linetype=as.factor(cyl)),method='lm')+
  geom_point(aes(x=hp,y=mpg,shape=as.factor(cyl)))+
  theme_bw(base_size=18)+theme(legend.position="none")

# Take the legend from p1
fill.legend <- gtable_filter(ggplot_gtable(ggplot_build(p1)), "guide-box") 
legGrob <- grobTree(fill.legend)

# Put the legend from p1 onto p2
grid.arrange(p2, legGrob, ncol=2, widths = c(6,1))

enter image description here

Sam Dickson
  • 5,082
  • 1
  • 27
  • 45
  • I don't really want the points in front of the lines on the plot (as I indicated in my question) but I'll accept your answer for the hack at the end. – Dominik Dec 10 '15 at 18:05
  • You can make the plot you want with the points behind the lines and the legend you want with the points in front. That's the beauty of the hack. – Sam Dickson Dec 10 '15 at 20:01