[R] ggplot2 qplot and add

hadley wickham h.wickham at gmail.com
Sat Aug 4 16:14:32 CEST 2007


On 8/4/07, Emilio Gagliardi <emilio at ualberta.ca> wrote:
> Dear Hadley,
> You are a machine! Its way past late you should be relaxing man ;)

Thanks :)

>
> Here is what I hacked together. My question was how to pass in the colour
> used to fill the dot/legend and the label for the legend entries.
>  test <- qplot(x,y, data=some.data, size=chop(variance, n=4),
> colour="cornflowerblue", xlim=c(-20,20), main="title", xlab="label",
> ylab="label") + geom_jitter(aes(colour="gray15", x=col1,
> xjitter=1, yjitter= 0.01)) +
> geom_jitter(aes(colour="gray20", x=col2, xjitter=1,
> yjitter=0.01)) + geom_jitter(aes(colour="gray25", x=col3,
> xjitter=1, yjitter=0.01)) +
> geom_jitter(aes(colour="gray30", x=col4, xjitter=1,
> yjitter= 0.01)) +
> scale_colour_identity(labels=c("number1","number2","number3","number4","number5"),
> grob="tile", name="Legend title")
>
> Because I want to be able to set the colors based on properties of my data,
> while still meaningfully name the legend entry.
>
> I did find another example, but I can't seem to find it atm, but it did
> assign labels from the underlying dataframe...which is better than hard
> coding them, but at this point its good enuf!

Yes, those are the two approaches I'd take - either build up piece by
piece and use scale_identity, or reshape the data frame so you can do
it one call.

Hadley



More information about the R-help mailing list