RetoSchmucki / rbms

A home for the latest rbms R package
https://retoschmucki.github.io/rbms/
Other
4 stars 4 forks source link

How to plot(flight curve) ignoring the years with no flight curve available? #18

Closed SarahVray closed 12 months ago

SarahVray commented 1 year ago

Dear Reto, @RetoSchmucki

In https://retoschmucki.github.io/rbms/articles/Get_Started_1.html, I am trying to run the very last part of the script to plot the annual flight curves in one graph. It works well for species with available flight curve in each year. However, for species with at least one year without an available flight curve, it returns the error message: "Error in plot.window(...) : need finite 'ylim' values" I suppose it is because NM = NA for the years with no flight curve. How could I adapt the script so that it can still plot the curves, just "ignoring" the years with no flight curve available ? The result would be a plot showing only the available flight curves.

I did a few tries with the "if"/"next" or "else" commands but I'm a beginner for that.

Thank you very much for your help!

Best wishes,

Sarah

SarahVray commented 1 year ago

Hello again, I found an actually simple solution, but surely there is a better way to do this. I just created a new "pheno" where I deleted the rows where NM = NA: pheno <- ts_flight_curve$pheno pheno2 <- pheno[pheno$NM != "NA", ] And I ran the code on this new "pheno2" instead of "pheno". The only minor issue is that the color of a year will vary from one species to another depending on the years with available flight curve.

However, when including this in a "for loop" to automate the process over all species, it causes a problem for species where no flight curve is available at all as the error message ends the loop. I will create a new issue for this. Thanks!

RetoSchmucki commented 1 year ago

Hi @SarahVray We can adapt the plot function to avoid the error you highlighted. Here is one solution using !is.na(NM) in plot.

## create data with NA for year 2002
pheno <- pheno[M_YEAR == 2002, NM := NA]

## add the line of the first year 
yr <- unique(pheno[order(M_YEAR),][!is.na(NM), as.numeric(as.character(M_YEAR))])

if("trimWEEKNO" %in% names(pheno)){
  plot(unique(pheno[M_YEAR == yr[1], .(trimWEEKNO, NM)]), type = 'l', ylim = c(0, max(pheno[!is.na(NM), ][, NM])), xlab = 'Monitoring Week', ylab = 'Relative Abundance')
} else {
  plot(unique(pheno[M_YEAR == yr[1], .(trimDAYNO, NM)]), type = 'l', ylim = c(0, max(pheno[!is.na(NM), ][, NM])), xlab = 'Monitoring Day', ylab = 'Relative Abundance')

}
## add individual curves for additional years
if(length(yr) > 1) {
i <- 2
  for(y in yr[-1]){
    if("trimWEEKNO" %in% names(pheno)){
      points(unique(pheno[M_YEAR == y , .(trimWEEKNO, NM)]), type = 'l', col = i)
    } else {
      points(unique(pheno[M_YEAR == y, .(trimDAYNO, NM)]), type = 'l', col = i)
    }
    i <- i + 1
  }
}

## add legend
legend('topright', legend = c(yr), col = c(seq_along(c(yr))), lty = 1, bty = 'n')