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() 

current plot.

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() 

thick plot lines.

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")) 

big legend keys

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:

points

i wanted use lines plot , dots/points legend.

so i'm asking 2 things:

  1. how change width of line in legend without changing plot?
  2. 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))) 

enter image description here

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))) 

enter image description here


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -