Error with specifying axis limits in ggplot

1,019 views
Skip to first unread message

Kristina Wolf

unread,
Apr 22, 2015, 1:52:56 PM4/22/15
to davi...@googlegroups.com
Hi again! 

I have two questions/issues with gplot

1) New error on previously fine code, and 2) changing y-axis limits makes CI bands wonky. 

Error 1
I am struggling now with a new error that popped up between now and yesterday. Code worked fine, nothing changed, now I get an error.

Error in structure(list(call = match.call(), aesthetics = aesthetics,  : 
  object 'rescale_none' not found

I have been running this type of code with this "object" for two years with no issues. Now this happens. Here is my code to generate a plot that shows changes in annual and perennial grass cover over time, with SE bars at each event monitoring point (this might look familiar). 

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")) + #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))

Error 2
There are thre questions embedded in this error: 
1) I have a similar graph, only now with Confidence Intervals instead of SE bars. Worked fine, except that my CIs extend below 0, and grass cover cannot be < 0. I try to apply limits, and all of a sudden my CIs no longer match up to the points/lines. 
2) Also, I am now getting the same error (Error 1, above) with this graph too. 
3) Finally, there are two legends, and when I try to apply a change in text style, I get an error. I assume it is because I have two legend components, but am not actually sure. 
I can get pieces of the code to work, but cannot put it all together. 

Here is my code: 

ggplot(data=KIN.Aug.Per.Seed.sum) + 
  geom_ribbon(aes(x=Monitoring.Event, ymin=lower, ymax = upper, fill =Per.Ann), alpha=0.5) + 
  geom_line(aes(x=Monitoring.Event,y=mean, col=Per.Ann), size = 1.25) + geom_point(aes(x=Monitoring.Event,y=mean, colour = Per.Ann), 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 (+- 2sd)") + 
  scale_x_continuous(breaks=c(seq(1,25,by=3))) + scale_y_continuous (oob = rescale_none) +
  ggtitle("Mean Cover each monitoring week, August watering") + 
  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","Perennial 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))

Attached an idea of what it currently looks like (because apparently today I cannot copy and paste an image into an email), when I walk through functional pieces.

One last note: Only thing that is different between last night and this morning is my computer performed "updates", without my permission (Plant Sciences IT overrode my preferences, and now I cannot control my computer - thanks guys!). 

Thank you everyone for your thoughts and suggestions. I realize this is probably a super-simple fix but I as supposed to have a meeting this morning, armed with these graphs, and now I can't get them in the form they should be! 
Kinsella, Mean across all plots by treatment, August watering.png

Kristina Wolf

unread,
Apr 22, 2015, 1:56:56 PM4/22/15
to davi...@googlegroups.com
Also, here's the graph with CIs after I attempt to limit the y-axis range: 
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
 

--
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 22, 2015, 2:15:10 PM4/22/15
to davi...@googlegroups.com
Best I can find is that you need to load the scales package to get oob to work. 


library(scales)

Does that get you further?

Noam Ross

unread,
Apr 22, 2015, 2:19:35 PM4/22/15
to davi...@googlegroups.com

Errors 1 and 2 are related. rescale_none() is a function in the scales package. I think at some point ggplot2 may have changed whether it automatically loaded the scales package, so if your computer updated the package it may have broken the behavior. You should be able to fix this by calling library(scales) before plotting.

The call scale_y_continuous (oob = rescale_none) determines how out of bounds values are treated. rescale_none should show your confidence intervals even if they fall out of bounds of the plot (the default is censor). If not you can try scale_y_continuous (oob = rescale_pal(range = c(0, 80 )), or whatever your y limits are.

What’s the error you get from changing the legend text style?

image.png

Kristina Wolf

unread,
Apr 23, 2015, 1:29:32 AM4/23/15
to davi...@googlegroups.com
Thank you Brandon and Noam! 

Loading scales worked; and then, my other issues with style disappeared. 

My kinda lame workaround earlier was to just create a new column where any SD < 0 was set to 0, so that even with the default ylim, the CIs were limited to begin at zero. 

Your approach is much more elegant. 

Here's the current code and graph: 

ggplot(data=KIN.Aug.Per.Seed.sum) + 
  geom_ribbon(aes(x=Monitoring.Event, ymin=lower, ymax = upper, fill =Per.Ann), alpha=0.5) + 
  geom_line(aes(x=Monitoring.Event,y=mean, col=Per.Ann), size = 1.25) + 
  geom_point(aes(x=Monitoring.Event,y=mean, colour = Per.Ann), size = 4) + 
  facet_wrap (~Treatment, nrow=1) +
  xlab("Monitoring Week (8/14/2013 - 5/5/2014)") + ylab("Mean Percent Cover (+- 2sd)") + 
  scale_x_continuous(breaks=c(seq(1,25,by=3))) + scale_y_continuous (limits = c(0,80), oob = rescale_none) +
  ggtitle("Mean Cover each monitoring week, August watering") + 
  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","Perennial 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))
Inline image 1
Next up, adding in a layer from a separate dataframe to only one panel of that graph (adding error bars to those two points in the leftmost panel.... work in progress, with very little progress!) 
You will probably see me back here soon. 

Thanks again! 

~ 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