Help with removing extra legend elements in ggplot

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with removing extra legend elements in ggplot

scoyoc
I can't get the fine tuning right with my legend. I get an extra legend
element "10" which is the point size in my plot. Can someone help me get rid
of this extra element? Additionally I would also like to reduce the size of
the legend.

If you want to reproduce my figure you can  download my data in csv format
here
<https://github.com/scoyoc/EcoSiteDelineation/blob/master/VegNMDS_scores.csv
>
.

Here is my code...

>veg.nmds.sc = read.csv("VegNMDS_scores.csv", header = T)

>nmds.fig = ggplot(data = veg.nmds.sc, aes(x = NMDS1, y = NMDS2))
>nmds.fig + geom_point(aes(color = VegType, shape = VegType, size = 10)) +
>  scale_colour_manual(name = "Vegetation Type",
>                      values = c("blue", "magenta", "gray50", "red",
> "cyan3",
>                                 "green4", "gold")) +
>  scale_shape_manual(name = "Vegetation Type", values = c(15, 16, 17, 18,
> 15, 16, 17)) +
>  theme_bw() +
>  theme(panel.background = element_blank(), panel.grid.major =
> element_blank(),
>        panel.grid.minor = element_blank(),
>        legend.key = element_rect(color = "white")
>        )

I have been messing around with
>  theme(..., legend.key.size = unit(1, "cm"))
but I keep getting the error "could not find function unit". I'm not sure
why, isn't unit supposed to be part of the legend.key argument?

... and the resulting figure...
<http://r.789695.n4.nabble.com/file/n4680764/VegNMDS.jpeg
>

Thanks for the help.
MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department <http://www.cnr.usu.edu/wild/> & Ecology
Center <http://www.usu.edu/ecology/>
Quinney College of Natural Resources <http://cnr.usu.edu/>
Utah State University
Logan, UT
=====
Think SNOW!

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department & Ecology Center
Quinney College of Natural Resources
Utah State University
Logan, UT
=====
Think SNOW!

Reply | Threaded
Open this post in threaded view
|

Re: Help with removing extra legend elements in ggplot

scoyoc
This post has NOT been accepted by the mailing list yet.
Oh yeah...
OS: Windows 7
R version: R 3.0.2 "Frisbee Sailing"
IDE: RStudio 0.97.551
MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department & Ecology Center
Quinney College of Natural Resources
Utah State University
Logan, UT
=====
Think SNOW!

Reply | Threaded
Open this post in threaded view
|

Re: Help with removing extra legend elements in ggplot

David Winsemius
In reply to this post by scoyoc

On Nov 19, 2013, at 3:44 PM, Matthew Van Scoyoc wrote:

> I can't get the fine tuning right with my legend. I get an extra legend
> element "10" which is the point size in my plot. Can someone help me get rid
> of this extra element? Additionally I would also like to reduce the size of
> the legend.
>
> If you want to reproduce my figure you can  download my data in csv format
> here
> <https://github.com/scoyoc/EcoSiteDelineation/blob/master/VegNMDS_scores.csv
>>
> .
>
> Here is my code...
>
>> veg.nmds.sc = read.csv("VegNMDS_scores.csv", header = T)
>
>> nmds.fig = ggplot(data = veg.nmds.sc, aes(x = NMDS1, y = NMDS2))
>> nmds.fig + geom_point(aes(color = VegType, shape = VegType, size = 10)) +
>> scale_colour_manual(name = "Vegetation Type",
>>                     values = c("blue", "magenta", "gray50", "red",
>> "cyan3",
>>                                "green4", "gold")) +
>> scale_shape_manual(name = "Vegetation Type", values = c(15, 16, 17, 18,
>> 15, 16, 17)) +
>> theme_bw() +
>> theme(panel.background = element_blank(), panel.grid.major =
>> element_blank(),
>>       panel.grid.minor = element_blank(),
>>       legend.key = element_rect(color = "white")
>>       )
>
> I have been messing around with
>> theme(..., legend.key.size = unit(1, "cm"))
> but I keep getting the error "could not find function unit". I'm not sure
> why, isn't unit supposed to be part of the legend.key argument?

Try this workaround to what sounds like a bug:

library(grid)

# then repeat the call.

--

David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with removing extra legend elements in ggplot

scoyoc
No dice. I still get the "10" legend element.
Thanks for the quick reply.
Cheers,

MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department <http://www.cnr.usu.edu/wild/> & Ecology
Center <http://www.usu.edu/ecology/>
Quinney College of Natural Resources <http://cnr.usu.edu/>
Utah State University
Logan, UT

<[hidden email]>https://sites.google.com/site/scoyoc/
=====
Think SNOW!


On Tue, Nov 19, 2013 at 5:12 PM, David Winsemius <[hidden email]>wrote:

>
> On Nov 19, 2013, at 3:44 PM, Matthew Van Scoyoc wrote:
>
> > I can't get the fine tuning right with my legend. I get an extra legend
> > element "10" which is the point size in my plot. Can someone help me get
> rid
> > of this extra element? Additionally I would also like to reduce the size
> of
> > the legend.
> >
> > If you want to reproduce my figure you can  download my data in csv
> format
> > here
> > <
> https://github.com/scoyoc/EcoSiteDelineation/blob/master/VegNMDS_scores.csv
> >>
> > .
> >
> > Here is my code...
> >
> >> veg.nmds.sc = read.csv("VegNMDS_scores.csv", header = T)
> >
> >> nmds.fig = ggplot(data = veg.nmds.sc, aes(x = NMDS1, y = NMDS2))
> >> nmds.fig + geom_point(aes(color = VegType, shape = VegType, size = 10))
> +
> >> scale_colour_manual(name = "Vegetation Type",
> >>                     values = c("blue", "magenta", "gray50", "red",
> >> "cyan3",
> >>                                "green4", "gold")) +
> >> scale_shape_manual(name = "Vegetation Type", values = c(15, 16, 17, 18,
> >> 15, 16, 17)) +
> >> theme_bw() +
> >> theme(panel.background = element_blank(), panel.grid.major =
> >> element_blank(),
> >>       panel.grid.minor = element_blank(),
> >>       legend.key = element_rect(color = "white")
> >>       )
> >
> > I have been messing around with
> >> theme(..., legend.key.size = unit(1, "cm"))
> > but I keep getting the error "could not find function unit". I'm not sure
> > why, isn't unit supposed to be part of the legend.key argument?
>
> Try this workaround to what sounds like a bug:
>
> library(grid)
>
> # then repeat the call.
>
> --
>
> David Winsemius
> Alameda, CA, USA
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department & Ecology Center
Quinney College of Natural Resources
Utah State University
Logan, UT
=====
Think SNOW!

Reply | Threaded
Open this post in threaded view
|

Re: Help with removing extra legend elements in ggplot

djmuseR
The additional element comes from this code:

geom_point(aes(color = VegType, shape = VegType, size = 10))

Take the size argument outside the aes() statement and the legend will
disappear:

geom_point(aes(color = VegType, shape = VegType), size = 10)

The aes() statement maps a variable to a plot aesthetic. In this case
you're mapping VegType to color and shape. You want to *set* the size
aesthetic to a constant value, and that is done by assigning the value
10 to the size aesthetic outside of aes().

Dennis

On Tue, Nov 19, 2013 at 4:35 PM, Matthew Van Scoyoc <[hidden email]> wrote:

> No dice. I still get the "10" legend element.
> Thanks for the quick reply.
> Cheers,
>
> MVS
> =====
> Matthew Van Scoyoc
> Graduate Research Assistant, Ecology
> Wildland Resources Department <http://www.cnr.usu.edu/wild/> & Ecology
> Center <http://www.usu.edu/ecology/>
> Quinney College of Natural Resources <http://cnr.usu.edu/>
> Utah State University
> Logan, UT
>
> <[hidden email]>https://sites.google.com/site/scoyoc/
> =====
> Think SNOW!
>
>
> On Tue, Nov 19, 2013 at 5:12 PM, David Winsemius <[hidden email]>wrote:
>
>>
>> On Nov 19, 2013, at 3:44 PM, Matthew Van Scoyoc wrote:
>>
>> > I can't get the fine tuning right with my legend. I get an extra legend
>> > element "10" which is the point size in my plot. Can someone help me get
>> rid
>> > of this extra element? Additionally I would also like to reduce the size
>> of
>> > the legend.
>> >
>> > If you want to reproduce my figure you can  download my data in csv
>> format
>> > here
>> > <
>> https://github.com/scoyoc/EcoSiteDelineation/blob/master/VegNMDS_scores.csv
>> >>
>> > .
>> >
>> > Here is my code...
>> >
>> >> veg.nmds.sc = read.csv("VegNMDS_scores.csv", header = T)
>> >
>> >> nmds.fig = ggplot(data = veg.nmds.sc, aes(x = NMDS1, y = NMDS2))
>> >> nmds.fig + geom_point(aes(color = VegType, shape = VegType, size = 10))
>> +
>> >> scale_colour_manual(name = "Vegetation Type",
>> >>                     values = c("blue", "magenta", "gray50", "red",
>> >> "cyan3",
>> >>                                "green4", "gold")) +
>> >> scale_shape_manual(name = "Vegetation Type", values = c(15, 16, 17, 18,
>> >> 15, 16, 17)) +
>> >> theme_bw() +
>> >> theme(panel.background = element_blank(), panel.grid.major =
>> >> element_blank(),
>> >>       panel.grid.minor = element_blank(),
>> >>       legend.key = element_rect(color = "white")
>> >>       )
>> >
>> > I have been messing around with
>> >> theme(..., legend.key.size = unit(1, "cm"))
>> > but I keep getting the error "could not find function unit". I'm not sure
>> > why, isn't unit supposed to be part of the legend.key argument?
>>
>> Try this workaround to what sounds like a bug:
>>
>> library(grid)
>>
>> # then repeat the call.
>>
>> --
>>
>> David Winsemius
>> Alameda, CA, USA
>>
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Help with removing extra legend elements in ggplot

scoyoc
Awesome! Thanks for the fix Dennis, and thanks for clearing up aes() too.
It makes sense now.
Cheers,

MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department <http://www.cnr.usu.edu/wild/> & Ecology
Center <http://www.usu.edu/ecology/>
Quinney College of Natural Resources <http://cnr.usu.edu/>
Utah State University
Logan, UT

<[hidden email]>https://sites.google.com/site/scoyoc/
=====
Think SNOW!


On Tue, Nov 19, 2013 at 5:52 PM, Dennis Murphy <[hidden email]> wrote:

> The additional element comes from this code:
>
> geom_point(aes(color = VegType, shape = VegType, size = 10))
>
> Take the size argument outside the aes() statement and the legend will
> disappear:
>
> geom_point(aes(color = VegType, shape = VegType), size = 10)
>
> The aes() statement maps a variable to a plot aesthetic. In this case
> you're mapping VegType to color and shape. You want to *set* the size
> aesthetic to a constant value, and that is done by assigning the value
> 10 to the size aesthetic outside of aes().
>
> Dennis
>
> On Tue, Nov 19, 2013 at 4:35 PM, Matthew Van Scoyoc <[hidden email]>
> wrote:
> > No dice. I still get the "10" legend element.
> > Thanks for the quick reply.
> > Cheers,
> >
> > MVS
> > =====
> > Matthew Van Scoyoc
> > Graduate Research Assistant, Ecology
> > Wildland Resources Department <http://www.cnr.usu.edu/wild/> & Ecology
> > Center <http://www.usu.edu/ecology/>
> > Quinney College of Natural Resources <http://cnr.usu.edu/>
> > Utah State University
> > Logan, UT
> >
> > <[hidden email]>https://sites.google.com/site/scoyoc/
> > =====
> > Think SNOW!
> >
> >
> > On Tue, Nov 19, 2013 at 5:12 PM, David Winsemius <[hidden email]
> >wrote:
> >
> >>
> >> On Nov 19, 2013, at 3:44 PM, Matthew Van Scoyoc wrote:
> >>
> >> > I can't get the fine tuning right with my legend. I get an extra
> legend
> >> > element "10" which is the point size in my plot. Can someone help me
> get
> >> rid
> >> > of this extra element? Additionally I would also like to reduce the
> size
> >> of
> >> > the legend.
> >> >
> >> > If you want to reproduce my figure you can  download my data in csv
> >> format
> >> > here
> >> > <
> >>
> https://github.com/scoyoc/EcoSiteDelineation/blob/master/VegNMDS_scores.csv
> >> >>
> >> > .
> >> >
> >> > Here is my code...
> >> >
> >> >> veg.nmds.sc = read.csv("VegNMDS_scores.csv", header = T)
> >> >
> >> >> nmds.fig = ggplot(data = veg.nmds.sc, aes(x = NMDS1, y = NMDS2))
> >> >> nmds.fig + geom_point(aes(color = VegType, shape = VegType, size =
> 10))
> >> +
> >> >> scale_colour_manual(name = "Vegetation Type",
> >> >>                     values = c("blue", "magenta", "gray50", "red",
> >> >> "cyan3",
> >> >>                                "green4", "gold")) +
> >> >> scale_shape_manual(name = "Vegetation Type", values = c(15, 16, 17,
> 18,
> >> >> 15, 16, 17)) +
> >> >> theme_bw() +
> >> >> theme(panel.background = element_blank(), panel.grid.major =
> >> >> element_blank(),
> >> >>       panel.grid.minor = element_blank(),
> >> >>       legend.key = element_rect(color = "white")
> >> >>       )
> >> >
> >> > I have been messing around with
> >> >> theme(..., legend.key.size = unit(1, "cm"))
> >> > but I keep getting the error "could not find function unit". I'm not
> sure
> >> > why, isn't unit supposed to be part of the legend.key argument?
> >>
> >> Try this workaround to what sounds like a bug:
> >>
> >> library(grid)
> >>
> >> # then repeat the call.
> >>
> >> --
> >>
> >> David Winsemius
> >> Alameda, CA, USA
> >>
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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.
MVS
=====
Matthew Van Scoyoc
Graduate Research Assistant, Ecology
Wildland Resources Department & Ecology Center
Quinney College of Natural Resources
Utah State University
Logan, UT
=====
Think SNOW!

Reply | Threaded
Open this post in threaded view
|

Re: Help with removing extra legend elements in ggplot

JohnDee
In reply to this post by scoyoc
On Tue, 19 Nov 2013 16:44:11 -0700
Matthew Van Scoyoc <[hidden email]> wrote:

You want to consider this as a programming bug in your code.  Executing
each line sequentially shows that the problem appears in the second
line:

nmds.fig + geom_point(aes(color = VegType, shape = VegType, size = 10))

?aes() and ?geom_point() reveals a misplaced right parenthesis. "Size"
belongs to geom_point(), not aes() as you have it grouped.

jwdougherty

______________________________________________
[hidden email] 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.