SpaCE-Lab-MSU / warmXtrophic

This repository contains R scripts that organize, analyze, and plot data from the long term Warming X Trophic Interactions experiment at Kellogg Biological Station and University of Michigan Biological Station (UMBS).
Other
0 stars 0 forks source link

Having trouble creating a function that returns a plot #3

Closed moriahy closed 4 years ago

moriahy commented 4 years ago

@billspat I'm working in my practice.R script and I'm trying to create a function that returns a plot that graphs percent cover overtime for a single species in a plot. We talked about this in one of our meetings a couple of weeks back now. I feel like I'm missing something that is really simple to execute. Here is the code of the function I've written.

perc_cover_plot <- function(Species, Plot) { PlantComp$Species == "Species" PlantComp$Plot == "Plot" PlantComp$Date == "Date" PlantComp$Cover == "Cover" return(plot("Date", "Cover", type = "p")) }

When I run: perc_cover_plot(Cest, B2)

This is what is returned in the console: Error in charToDate(x) : character string is not in a standard unambiguous format Called from: charToDate(x)

I did this earlier on in my script: PlantComp$Date <- as.Date(PlantComp$Date, format = "%m/%d/%y")

Thank you in advance! And please let me know if this was a good use of the "issue" feature on here and if there's anything I can do better the next time around!

moriahy commented 4 years ago

I had a lot of trouble creating this function to work, but someone on StackOverFlow told me, "This is yet another issue of scoping variables with the same names in different environments. The easiest way to bypass that is to rename the variables internally before applying the filter. Example:" perc_cover_plot <- function(Species, Plot) { sp <- Species pl <- Plot PlantCompSub <- subset(PlantComp, Species == sp & Plot == pl) return(plot(Cover ~ Date, data = PlantCompSub, main = "2019 Percent Cover Over Time", col = "blue", pch = 19)) } This worked for me.