rmaia / pavo

tools for the analysis of color data in R
http://pavo.colrverse.com
GNU General Public License v2.0
68 stars 17 forks source link

x-axis displaced curve after smooth with procspec #234

Closed juandemedel closed 2 years ago

juandemedel commented 2 years ago

Hello dear community. I'm new here. I've plotting spectral transmittance data using pavo. However, when smooth my curves with procspec I get my curves displaced in x-axis to the right. Would you please give me some advice to fix them?

thomased commented 2 years ago

Hi @juandemedel - that’s odd, but shouldn’t be hard to fix. Would you mind providing a little data + code to reproduce the issue? I can’t seem to make it happen.

juandemedel commented 2 years ago

Hello Mr. Thomas White. Sadly I can not provide data but I can show my code:

library(pavo)

library(readxl)

DataSample <- read_excel("DataSample.xlsx")

DataSample

is.rspec(DataSample)

as.rspec(DataSample, interp = FALSE)

specs <- procspec(DataSample, opt = c("max", "min"))

levels <- gsub(".[A-Z]", "", names(specs))[-1]

table(levels)

mspecs <- aggspec(specs, by = levels, FUN = mean)

is.rspec(mspecs)

plot(mspecs)

aggplot(specs, by=levels, FUN.center = mean, FUN.error = sd, ylim = c(0, 1), alpha = 0.2, legend = TRUE, ylab="Transmittance", yaxt = "n")

axis(2, at=c(0,1))

spec.sm <- procspec(mspecs, opt = c("smooth"), span = 0.05)

plot(spec.sm, ylab="Transmittance", yaxt = "n") axis(2, at=c(0,1))

plot(mspecs[, 2] ~ specs[, 1], type = 'l', lwd = 10, col = 'grey', xlab = "Wavelength (nm)", ylab = "Transmittance")

lines(spec.sm[, 2] ~ specs[, 1], col = 'red', lwd = 1)

Screenshot 2022-06-15 at 11 21 21
thomased commented 2 years ago

Hi @juandemedel, @Bisaloo has mostly cracked it, but for now if you're in need of a quick solution you'll just need to interpolate your spectra (with as.rspec(DataSample, interp = TRUE), which is also the default, instead of FALSE) when converting them to rspec objects. Without going into detail — that'll fix the x-shift issue, and you lose very little detail when interpolating to 1 nm bins so it shouldn't much matter for downstream analyses.

If for some analytical reason you really can't interpolate the data though, then we're still looking into the specifics of the bug and will update when we've figured it out one way or the other.

juandemedel commented 2 years ago

Thank you so much Doctor Thomas White. I ran again the code and it worked as I wanted.

Bisaloo commented 2 years ago

Hi again @juandemedel, thanks for your bug report.

The issue should now be solved. You can now use procspec() on uninterpolated spectra, as demonstrated in the example below:

library(pavo)
#> Welcome to pavo 2! Take a look at the latest features (and update your bibliography) in our recent publication: Maia R., Gruson H., Endler J. A., White T. E. (2019) pavo 2: new tools for the spectral and spatial analysis of colour in R. Methods in Ecology and Evolution, 10, 1097-1107.

# Uninterpolated spectra
helio <- lightr::lr_get_spec(
  system.file("testdata", "heliomaster", package = "lightr"),
  ext = "jdx", interpolate = FALSE
)
#> 4 files found; importing spectra:

helio_smoothed <- procspec(helio, "smooth", span = 0.1)
#> processing options applied:
#> smoothing spectra with a span of 0.1

plot(merge(helio, helio_smoothed), col = rep(c("black", "red"), each = 4))

Created on 2022-06-17 by the reprex package (v2.0.1.9000)

For this to work, you will need to install the development version of pavo by running the following code:

install.packages("remotes")
remotes::install_github("rmaia/pavo")