Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x-axis displaced curve after smooth with procspec #234

Closed
juandemedel opened this issue Jun 14, 2022 · 5 comments
Closed

x-axis displaced curve after smooth with procspec #234

juandemedel opened this issue Jun 14, 2022 · 5 comments
Labels

Comments

@juandemedel
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

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

@Bisaloo Bisaloo added the bug label Jun 17, 2022
@Bisaloo
Copy link
Collaborator

Bisaloo commented Jun 17, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants