Geom_errorbar() legend won't go away

1,146 views
Skip to first unread message

Kristina Wolf

unread,
Apr 20, 2015, 12:39:25 PM4/20/15
to davi...@googlegroups.com
Hi all, 

I am trying to use ggplot to plot changes in annual and native grass cover over time, including errorbars. I have been able to do just that, but I get an errorbar legend that I could certainly just white out in Paint or the like, but it would be much nicer and easier (theoretically, at some point) to do it in R. Here is my code so far: 

ggplot(means2, aes(x = Monitoring.Event, y = mean, color = Grass.Type)) + 
  geom_errorbar(aes(ymin=mean - se, ymax=mean + se), width=1, size = 1, guide = FALSE) + 
                #geom_errorbar$guide_geom =function(.) "point")
  geom_line(aes(colour=Grass.Type, size = 0.5)) + 
  geom_point(aes(colour= Grass.Type, size = 1)) + 
  facet_wrap (~Treatment, nrow=1) + scale_y_continuous(oob=rescale_none) + 
  xlab("Monitoring Week (8/14/2013 - 5/5/2014)") + ylab("Mean Percent Cover (w/ SE bars)") + 
  ggtitle("Mean cover each monitoring week, August watering") + 
  scale_x_continuous( breaks=c(seq(1,25,by=3))) +
  scale_fill_discrete(name="Grass Type", breaks=c("Annual grass","Native grass"), labels=c("Annual Grass Cover","Perennial Grass Cover")) + 
  scale_colour_discrete(name="Grass Type", breaks=c("Annual grass","Native grass"), labels=c("Annual grass cover","Native grass cover")) +
  theme(panel.grid.major.x = element_blank() ,
        strip.text.x = element_text(face = "bold", size=20),
        strip.background = element_rect(colour="blue", fill="#CCCCFF"), 
        plot.title = element_text(face="bold", size=30), 
        axis.title.x = element_text(face="bold", size=20),
        axis.text.x  = element_text(face="bold", size=10),
        axis.title.y = element_text(face="bold", size=20),
        axis.text =element_text(face="bold", size=20),
        legend.text=element_text(face="bold", size=25),
        legend.title=element_text(size=30))

I am thinking the error lies in where and how I specify the line weight for the error bars, because when I change that, the legend scale changes too. But I can't seem to get rid of the legend completely; at best I have been able to get it down to one "category". 

The line in bold was a workaround suggested by Hadley, except that he did not provide context as to where it goes, and another user on that site said that you had to use "tile" instead of "point" (but also did not say where that line of code goes). I have tried at least a dozen different workarounds gleaned from various forums, to no avail. Without that line of code it produces the graph I want, and with it I get an inconsistent, variable error, most often revolving around the "function" in that line of code. 
Any additional suggestions would be much appreciated. 


Noam Ross

unread,
Apr 20, 2015, 1:04:36 PM4/20/15
to davi...@googlegroups.com

I think what you mean is that you want to get rid of the weird part of the legend titled “0.5”, correct? I think this is not related to the error bars but the lines


geom_line(aes(colour=Grass.Type, size = 0.5)) + 
geom_point(aes(colour= Grass.Type, size = 1)) +

Here you’ve put the size arguments inside the aes() argument, which tells ggplot to treat these as data, and therefore create a legend for “size”, even though it is constant. For ggplot to treat these as constant values that only affect chart appearance, they should be outside the aes() argument, so as to be a property of the geom, rather than the aesthetic mapping, like so:

geom_line(aes(colour=Grass.Type), size = 0.5) + 
geom_point(aes(colour= Grass.Type), size = 1) +

Hadley’s solution refers to the labels which, in your plot, appear next to “Annual grass cover” and “Native grass cover”. Note that these include both line and point elements. That line of code would make them only points. It also refers to an older version of ggplot, I believe, and there’s a simpler way to do this now: Set guide=FALSE in your geom_line() argument and your legend will only show points.





--
Check out our R resources at http://www.noamross.net/davis-r-users-group.html
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
Visit this group at http://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.

Brandon Hurr

unread,
Apr 20, 2015, 1:09:17 PM4/20/15
to davi...@googlegroups.com

On Mon, Apr 20, 2015 at 9:39 AM, Kristina Wolf <kmw...@ucdavis.edu> wrote:
geom_line(aes(colour=Grass.Type, size = 0.5)) + 
  geom_point(aes(colour= Grass.Type, size = 1)) + 

I think the problem might be with these two lines. 
Take the size out of the aes() and into only geom_point and geom_line and plot again. 
Like so, 
geom_line(aes(colour=Grass.Type), size = 0.5) + 
geom_point(aes(colour= Grass.Type), size = 1) + 


HTH, 

Branodn

Kristina Wolf

unread,
Apr 20, 2015, 1:10:50 PM4/20/15
to davi...@googlegroups.com
Thank you Noam, this got me closer! And this must have been what Matt Savoca was getting at by saying to set the line size outside the aes for geomerrorbar() (except that it was the lines/points that were the problem, not geomerrorbar). 

However, when I put the size specifications outside of aes for line and point, now it seems to ignore that specification completely and the lines and points are super tiny. 

ggplot(means2, aes(x = Monitoring.Event, y = mean, color = Grass.Type)) + 
    geom_errorbar(aes(ymin=mean - se, ymax=mean + se), width=0.5, size = 1, guide = FALSE) + 
    geom_line(aes(colour=Grass.Type), size = 0.5) + 
    geom_point(aes(colour= Grass.Type), size = 1) + 
    facet_wrap (~Treatment, nrow=1) + scale_y_continuous(oob=rescale_none) + 
    xlab("Monitoring Week (8/14/2013 - 5/5/2014)") + ylab("Mean Percent Cover (w/ SE bars)") + 
    ggtitle("Mean cover each monitoring week, August watering") + 
    scale_x_continuous( breaks=c(seq(1,25,by=3))) +
    scale_fill_discrete(name="Grass Type", breaks=c("Annual grass","Native grass"), labels=c("Annual Grass Cover","Perennial Grass Cover")) + 
    scale_colour_discrete(name="Grass Type", breaks=c("Annual grass","Native grass"), labels=c("Annual grass cover","Native grass cover")) + #what the names of columns are, and then what I want them to be on the legend
    theme(panel.grid.major.x = element_blank() ,
          strip.text.x = element_text(face = "bold", size=20),
          strip.background = element_rect(colour="blue", fill="#CCCCFF"), 
          plot.title = element_text(face="bold", size=30), 
          axis.title.x = element_text(face="bold", size=20),
          axis.text.x  = element_text(face="bold", size=10),
          axis.title.y = element_text(face="bold", size=20),
          axis.text =element_text(face="bold", size=20),
          legend.text=element_text(face="bold", size=25),
          legend.title=element_text(size=30))

Inline image 1

~ Kristina

​​
Kristina Wolf

Ph.D. Candidate, Graduate Group in Ecology
M.S. Soil Science
​, 
B.S. Animal Science
KristinaMWolf.com
Restoration Ecology Lab
Department of Plant Sciences
University of California, Davis​

"We have to remember that what we observe is not nature herself, but nature exposed to our method of questioning." ~ Werner Heisenberg
 

Noam Ross

unread,
Apr 20, 2015, 1:21:31 PM4/20/15
to davi...@googlegroups.com

Previously, when you put size inside the aesthetic mapping, ggplot saw, “Map the value of 1 to a size scale of default values”, which I think makes the center of the size scale a value of 2 or 3. Now, you’ve told ggplot “make all points size 1”, and “make all lines size 0.5”, which are smaller values than the defaults you saw before. You should be able to fix this by increasing the size values you assign in geom_point and geom_line.

Kristina Wolf

unread,
Apr 20, 2015, 1:30:03 PM4/20/15
to davi...@googlegroups.com
Indeed! Thank you Noam, this does it. Here is the final code and pic in case anyone is interested: 

ggplot(means2, aes(x = Monitoring.Event, y = mean, color = Grass.Type)) + 
    geom_errorbar(aes(ymin=mean - se, ymax=mean + se), width=2, size = 1) + 
    geom_line(aes(colour=Grass.Type), size = 1.25) + 
    geom_point(aes(colour= Grass.Type), size = 4) + 
    facet_wrap (~Treatment, nrow=1) + scale_y_continuous(oob=rescale_none) + 
    xlab("Monitoring Week (8/14/2013 - 5/5/2014)") + ylab("Mean Percent Cover (w/ SE bars)") + 
    ggtitle("Mean cover each monitoring week, August watering") + 
    scale_x_continuous( breaks=c(seq(1,25,by=3))) +
    scale_fill_discrete(name="Grass Type", breaks=c("Annual grass","Native grass"), labels=c("Annual Grass Cover","Perennial Grass Cover")) + 
    scale_colour_discrete(name="Grass Type", breaks=c("Annual grass","Native grass"), labels=c("Annual grass cover","Native grass cover")) + 
    theme(panel.grid.major.x = element_blank() ,
          strip.text.x = element_text(face = "bold", size=20),
          strip.background = element_rect(colour="blue", fill="#CCCCFF"), 
          plot.title = element_text(face="bold", size=30), 
          axis.title.x = element_text(face="bold", size=20),
          axis.text.x  = element_text(face="bold", size=10),
          axis.title.y = element_text(face="bold", size=20),
          axis.text =element_text(face="bold", size=20),
          legend.text=element_text(face="bold", size=25),
          legend.title=element_text(size=30))

NOTE: because the burn was accidental (caused by an undergrad with a bee smoker working on a bee research project), there are no lines in the first facet panel because I didn't actually monitor any control unburned plots until the very end, just to have something to compare by control burned plots to. Thus, it's purposefully mostly blank, and not a full factorial design.

Inline image 1

~ Kristina

​​
Kristina Wolf

Ph.D. Candidate, Graduate Group in Ecology
M.S. Soil Science
​, 
B.S. Animal Science
KristinaMWolf.com
Restoration Ecology Lab
Department of Plant Sciences
University of California, Davis​

"We have to remember that what we observe is not nature herself, but nature exposed to our method of questioning." ~ Werner Heisenberg
 

Reply all
Reply to author
Forward
0 new messages