Hi:
scale_manual() is a little tricky when you build the legend from within the
plot. I used shorter labels than you, but this worked for me:
ggplot(mydata, aes(y=score2, x=score1)) +
geom_point() +
stat_quantile(quantiles=c(0.50), aes(colour='red'), size = 1) +
stat_quantile(quantiles=c(0.90), aes(colour='blue'), size = 2) +
scale_colour_manual(name = 'Quantiles', breaks = c('red',
'blue'),
values = c('red', 'blue'), labels =
c('50%', '90%'))
name = gives a title to the legend, the breaks are the 'factor levels'
of
the legend, the values are the colors to assign and the labels are the
assigned legend labels. It may just be me, but the plot seems better if both
lines have the same width. But if this is what you want and you want
different line sizes in the legend, then you have to create two legends and
merge them. It took me about 10 tries to get the second one right, and it
may be overkill, but at least it works. Size is considered continuous, so
you need to limit its extent, use a manual scale and set breaks and values
correctly (i.e., in coordination with the color scale). The values and
breaks correspond to the desired size values, to be matched to the color
breaks and scales. The labels and title are the same in both scale_* calls.
ggplot(mydata, aes(y=score2, x=score1)) +
geom_point() +
stat_quantile(quantiles=c(0.50), aes(colour='red', size = 1)) +
stat_quantile(quantiles=c(0.90), aes(colour='blue', size = 2)) +
scale_colour_manual(name = 'Quantiles', breaks = c('red',
'blue'),
values = c('red', 'blue'), labels =
c('50%', '90%')) +
scale_size_manual(name = 'Quantiles', limits = c(1, 2), breaks = c(1,
2),
values = c(1, 2), labels = c('50%', '90%'))
HTH,
Dennis
On Tue, Oct 19, 2010 at 10:52 PM, YN Kim <y2silence@gmail.com> wrote:
> Hi, all.
>
> I'd like to add legend on my graph but I can't. My code is follows.
>
> library(ggplot2)
> score1<-rnorm(100,0,5)
> score2<-rnorm(400,10,15)
> mydata<-data.frame(score1,score2)
>
>
ggplot(mydata,aes(y=score2,x=score1))+geom_point()+stat_quantile(quantiles=c(0.50),col="red")+stat_quantile(quantiles=c(0.90),col="blue",size=2)
>
> I like to add legend indicating the information of "thick-blue line =
90%
> line" & "thin-red line = 50% line".
> Of course, I hope this legend should have same visual line with the plot.
> I tried to use 'scale_colour_manual' function, but it did not work.
>
> Could anyone give a advice for me?
>
> - YNK
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]