2

I am trying to introduce a custom equation to my ggplot.

data1989 %>% 
 ggplot(aes(x = daysAFTERFIRE, y = NDVI)) + 
 stat_function(fun= ~ (.meanNDVI)-(exp(-0.0000348*(.x)-2.17)))

As you see, my custom equation includes both y (column NDVI) and x (column daysAFTERFIRE), but also a third term, meanNDVI which is another column on my tibble.

Trying to run I get this:

Computation failed in `stat_function()`:
object '.meanNDVI' not found 

Anyone has any idea to solve this? Thanks!

1 Answer 1

1

You could build your function outside of the ggplot call, and then it might be easier to troubleshoot:

Assign your column containing a repeating mean value to a numeric variable:

meanNDVI_variable <- df$meanNDVI[1]

Then use it in your function:

f <- function(.x) (meanNDVI_variable)-(exp(-0.0000348*(.x)-2.17)))

Then:

data1989 %>% 
 ggplot(aes(x = daysAFTERFIRE, y = NDVI)) + 
 stat_function(fun= f)))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Matt! Unfortunately that doesn't solve my problem, I keep having the same error. Apparently it doesn't know where to look for meanNDVI
Is meanNDVI in a dataframe? Have you tried using df$meanNDVI?
Nope! As I mention in my post, it's another column of the tibble. So if I write: f <- function(.x) (data1989$meanNDVI)-(exp(-0.0000348*(.x)-2.17)) as you suggest I get Warning messages: Warning messages: 1: In (data1989$meanNDVI) - (exp(-3.48e-05 * (.x) - 2.17)) : longer object length is not a multiple of shorter object length 2: Computation failed in stat_function(): Elements must equal the number of rows or 1
IsmeanNDVI the same number, since it's a calculated mean? If so, you can assign the value to a variable, instead of a value in a dataframe: meanNVDI_variable <- df$meanNDVI[1] and then replace meanNVDI with meanNVDI_variable in your function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.