r - Control ggplot2 legend look without affecting the plot -
i'm plotting lines ggplot2 this:
ggplot(iris, aes(petal.width,petal.length,color=species)) + geom_line() + theme_bw()
.
i find legend marks small want them bigger. if change size, lines on plot change too:
ggplot(iris, aes(petal.width,petal.length,color=species)) + geom_line(size=4) + theme_bw()
.
but want see thick lines in legend, want lines on plot thin. tried use legend.key.size changes square of mark, not width of line:
library(grid) # unit ggplot(iris,aes(petal.width,petal.length,color=species))+geom_line()+theme_bw() + theme(legend.key.size=unit(1,"cm")) 
i tried use points:
ggplot(iris,aes(petal.width,petal.length,color=species)) + geom_line() + geom_point(size=4) + theme_bw() but of course still affects both plot , legend:

i wanted use lines plot , dots/points legend.
so i'm asking 2 things:
- how change width of line in legend without changing plot?
- how draw lines in plot, draw points/dots/squares in legend?
to change line width in legend should use function guides() , colour= use guide_legend() override.aes= , set size=. override size used in plot , use new size value legend.
ggplot(iris,aes(petal.width,petal.length,color=species))+geom_line()+theme_bw()+ guides(colour = guide_legend(override.aes = list(size=3))) 
to points in legend , lines in plot workaround add geom_point(size=0) ensure points invisible , in guides() set linetype=0 remove lines , size=3 larger points.
ggplot(iris,aes(petal.width,petal.length,color=species))+geom_line()+theme_bw()+ geom_point(size=0)+ guides(colour = guide_legend(override.aes = list(size=3,linetype=0))) 
Comments
Post a Comment