Legend labels how to?

116 views
Skip to first unread message

Kamy

unread,
Oct 5, 2009, 4:45:18 AM10/5/09
to ggplot2
The documentation indicates that scale_fill_gradient() and
scale_colour_gradient() accepts labels(). I have been trying to use
these labels to set the legend; however, in the example below, the
legend shows 0,150,300 instead of x,y,z. What's the correct way to
set the legend labels? Thank you

mylabels=c("a","b","c")
myscores=c(10,100,300)
myd=data.frame(mylabels,myscores)
ggplot(data=myd)+
theme_bw() +
geom_bar(aes(x=factor(mylabels,levels=unique(mylabels)),y=myscores,
fill=myscores)) +
scale_fill_gradient("Legend Title", low="#F0F8FF",
high="#007FFF",breaks=c(0,150,300),labels=c("x","y","z"))

hadley wickham

unread,
Oct 5, 2009, 9:52:15 AM10/5/09
to Kamy, ggplot2
Hi Kamy,

It's a bug and on my to do list.

Hadley
--
http://had.co.nz/

Kamy

unread,
Oct 8, 2009, 5:48:38 PM10/8/09
to ggplot2
Hi Hadley,

Will look forward to any updates as I'm a big fan of ggplo2 now. Is
there a workaround for the legend labelling issue in the meantime? I
have a plot I'm very happy with which unfortunately can't be
interpreted without the correct legend labels.

Kamy

On Oct 5, 2:52 pm, hadley wickham <h.wick...@gmail.com> wrote:
> Hi Kamy,
>
> It's a bug and on my to do list.
>
> Hadley
>
>
>
>
>
> On Mon, Oct 5, 2009 at 3:45 AM, Kamy <combizkhoz...@gmail.com> wrote:
>
> > The documentation indicates that scale_fill_gradient() and
> > scale_colour_gradient() accepts labels().  I have been trying to use
> > these labels to set thelegend; however, in the example below, the
> >legendshows 0,150,300 instead of x,y,z.  What's the correct way to
> > set thelegendlabels?  Thank you
>
> > mylabels=c("a","b","c")
> > myscores=c(10,100,300)
> > myd=data.frame(mylabels,myscores)
> > ggplot(data=myd)+
> >        theme_bw() +
> >        geom_bar(aes(x=factor(mylabels,levels=unique(mylabels)),y=myscores,
> > fill=myscores)) +
> >        scale_fill_gradient("LegendTitle", low="#F0F8FF",

hadley wickham

unread,
Oct 9, 2009, 2:35:33 PM10/9/09
to Kamy, ggplot2
Hi Kamy,

I've fixed it in the development version, and you can apply the fix
yourself by running

ScaleGradient$labels <- function(.) {
nulldefault(.$.labels, .$.tr$label(.$input_breaks()))
}

Hadley
--
http://had.co.nz/

Kamy

unread,
Oct 10, 2009, 2:51:58 AM10/10/09
to ggplot2
Hi I was able to apply the patch and it works fine thanks a lot.

I've tried installing the dev version in windows by visiting
http://github.com/hadley/ggplot2 , downloading as a .zip file, then in
RGui selecting Packages-->install packages from local (zip) file. R
then says "updating HTML package descriptions", but then ggplot
commands don't work and the command 'library(ggplot2)' responds with
'Error in library(ggplot2) : 'ggplot2' is not a valid installed
package'. It's then necessary to re-install the stable release
package from CRAN. So, does anybody know how to install the dev
version under windows? Thanks

On Oct 9, 7:35 pm, hadley wickham <h.wick...@gmail.com> wrote:
> Hi Kamy,
>

Learning R Blog

unread,
Oct 11, 2009, 4:01:53 PM10/11/09
to ggplot2

On Oct 10, 10:51 am, Kamy <combizkhoz...@gmail.com> wrote:
> Hi I was able to apply the patch and it works fine thanks a lot.
>
> I've tried installing the dev version in windows by visitinghttp://github.com/hadley/ggplot2, downloading as a .zip file, then in
> RGui selecting Packages-->install packages from local (zip) file.  R
> then says "updating HTML package descriptions", but then ggplot
> commands don't work and the command 'library(ggplot2)' responds with
> 'Error in library(ggplot2) : 'ggplot2' is not a valid installed
> package'.  It's then necessary to re-install the stable release
> package from CRAN.  So, does anybody know how to install the dev
> version under windows?  Thanks

Have a look at this thread for ideas/hints:
http://groups.google.com/group/ggplot2/browse_thread/thread/8eda022728f49e03/ea1c6e0d569fb9f9?lnk=gst&q=github#ea1c6e0d569fb9f9

--
http://learnr.wordpress.com
Reply all
Reply to author
Forward
0 new messages