Vinay vinay
Vinay vinay

Reputation: 17

Splitting the legend in multiple rows in ggplot. Guides not accepting group aesthetic

I want to split the legend of my plot into two rows. I used the below code and it does not split the legend into two rows. I searched in the forums and most of them suggested using the grouping in aesthetics for eg if the group by variable is fill then the same has to be passed in guides argument. I did that with the group and it doesnt work. Any help would be appreciated.

points<-c("80","60","40","20","all")` 
var1<-c(59,48,44,39,36)
var2<-c(91,88,81,78,69)
var3<-c(98,97,96,93,90)  
var4<-c(8,7.5,6,5,4)  
data<-data.frame(points,var1,var2,var3,var4)  
data2<-melt(data,key=points) 
x<-ggplot(data2,aes(x=points,y=value,group=variable))+
    geom_line(aes(linetype=variable))+geom_point()+
    scale_linetype_manual(values=c(5,6,3,1))  
x+guides(group=guide_legend(nrow=2))  

Upvotes: 1

Views: 8945

Answers (1)

Jack Brookes
Jack Brookes

Reputation: 3830

You changed the legend for the group aesthetic, not the linetype. I believe there is no legend associated with group. In fact the group mapping in your aes(...) is redundant since you used linetype.

x + guides(linetype = guide_legend(nrow = 2)) 

enter image description here

Upvotes: 6

Related Questions