Changing Legend Title on Ggplot 0.9.3 duplicates Legend and Color Scale

589 views
Skip to first unread message

Carlos Andrade

unread,
Feb 12, 2013, 1:57:23 PM2/12/13
to ggp...@googlegroups.com
Hi,

I have been struggling to change the title but I have been unsuccessful so far. The most recommended solution I have seen usually suggests +labs(colour = "New legend title") such as:


However instead of changing current existing title what I get is a duplicated legend as you may see on the attached picture.

Here is the code I am using:

ggplot(data=lucene.data, aes(x=actions, y=discussion, fill=lucene.data.churn.groups))+
geom_point(size=5, aes(color=lucene.data.churn.groups, shape=lucene.data.churn.groups))+
geom_line(size=1, aes(group=lucene.data.churn.groups, color=lucene.data.churn.groups))+
ylab("")+
xlab("")+
labs(fill = "Loren")+
ggtitle("Lucene (N = 152)")+
theme_bw()+
theme(axis.title.x=element_text(size=16)) +
theme(axis.title.y=element_text(size=16)) +
theme(plot.title=element_text(size=20))+
geom_point() + geom_rangeframe() + theme_tufte() 

Why is that? 

I try changing fill to colour but it didn't seem to work either. 
---
As a second question, is there any way to change the color from my lines so that they become, say, from 'cold colors' to 'hot colors'? It seems to me such color scale would be easier to visualize trends in patterns like the one I have now.

Thank you,

Carlos


Screen Shot 2013-02-12 at 3.55.32 PM.png

Dennis Murphy

unread,
Feb 12, 2013, 2:35:21 PM2/12/13
to Carlos Andrade, ggp...@googlegroups.com
Hi:

You're assigning the same variable to multiple aesthetics, each of
which creates its own legend internally. To merge them, they all have
to have the same title, breaks and labels...so change your current
labs() statement to

labs(fill = "Loren", color = "Loren", shape = "Loren")

and they should all be merged. A reproducible example would have made
it easy to establish this claim...

I don't see why you need the fill aesthetic since (a) you don't appear
to use it and (b) it plays no particular role in geoms point and line.
I suspect the amended graph would not change if you took out the fill
= ... assignment in ggplot().

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Carlos Andrade

unread,
Feb 12, 2013, 2:46:19 PM2/12/13
to Dennis Murphy, ggp...@googlegroups.com
Dennis,

Thank you for the quick reply. I had not seeing this solution before but that now makes sense and it, thankfully works.

I was able to figure out the scale as well in case someone comes around looking for this using:

scale_colour_manual(values = c("#FFFF33", "#FFCC33", "#FF9933","#FF6633","#FF3333","#330000"))+

And deciding on the colors on my own from a hex plot.

Sorry for the reproducibility part, but I couldn't post the data around and being a novice in R it is hard for me to replicate the problem.

In respect to what you said last, I might be using the code wrong since I got started on this plot from an example and maybe understood it wrong but:

derby.plot = ggplot(data=derby.data, aes(x=actions, y=discussion, fill=derby.data.churn.groups))+


Derby.data is a dataframe, of which have over 33 columns and many rows. I understood that by passing actions to X it wold be akin to say 'plot the information from derby.data$actions, same idea for y, and for fill it would be the same to say to color using a third dimension.

In other words, 

I thought I would be reproducing the behaviour of this following plot:

plot(derby.data$actions,derby.data$discussion,pch=19,cex=0.5,col=derby.data.churn.groups)
legend("center",legend=c("[  0,   19)", "[ 19,   55)", "[ 55,  143)","[143,  490)","[490,88836]"),col=derby.data.churn.groups,pch=c(19,19),cex=c(0.5,0.5))

 Which is attached. 

Is my assumption wrong?



2013/2/12 Dennis Murphy <djm...@gmail.com>
Screen Shot 2013-02-12 at 4.47.46 PM.png
Reply all
Reply to author
Forward
0 new messages