Elizabeth
Elizabeth

Reputation: 6581

Remove and alter legend in ggplot2

I have the following plot but do not want the legend for point size to show. Also how can I change the title for the factor(grp)? Sorry I know this should be an easy one but I am stuck.

df1<-data.frame(x=c(3,4,5),y=c(15,20,25),grp=c(1,2,2))
p<-ggplot(df1,aes(x,y))
p<-p+ geom_point(aes(colour=factor(grp),size=4))
p

df2<-data.frame(x=c(3.5,4.5,5.5),y=c(15.5,20.5,25.5))
p<-p + geom_path(data=df2,aes(x=x,y=y))
p

enter image description here

Upvotes: 0

Views: 1450

Answers (2)

csgillespie
csgillespie

Reputation: 60522

To change the legend title, it's easier (I find) to just change the data frame title:

df1$grp = factor(df1$grp)
colnames(df1)[3] = "Group"

The reason why size appears in the legend, is because you have made it an aesthetic - it's not! An aesthetic is something that varies with data. Here size is fixed:

p = ggplot(df1,aes(x,y))
p = p+ geom_point(aes(colour=Group), size=4)

You can also change the name of the legend in ggplot itself:

p =  p + scale_colour_discrete(name="Group")

Upvotes: 2

Maiasaura
Maiasaura

Reputation: 32996

Leave the size out of the aesthetics.

ggplot(df1,aes(x,y)) + geom_point(aes(colour = factor(grp)), size=4) +    
scale_colour_discrete(name = "Grp")

Upvotes: 2

Related Questions