aphalo / ggspectra

R package 'ggspectra' (from r4photobiology suite)
https://docs.r4photobiology.info/ggspectra
5 stars 0 forks source link

stat_label_peaks() etc. with transformed wavelength scales #14

Open nist-zack opened 1 month ago

nist-zack commented 1 month ago

I am trying to plot a reserved x-axis for spectrum plot, but stat_label_peaks() seems not support for the reverse plot. Is there any way to do this?

The warning message shows like following: Warning message: Computation failed in stat_label_peaks(). Caused by error in color_of.numeric(): ! all(ifelse(is.na(x), Inf, x) > 0) is not TRUE

ggplot(sun.spct) +
  geom_line() +
  stat_label_peaks(hjust = "left", span = 31, angle = 90, color = "red") +
  scale_x_reverse()
aphalo commented 1 month ago

Thanks for raising the issue! I never tried this! :-( I need to check this but I have some guesses. However it is over midnight here. I will try to fix this a.s.a.p., hopefuly tomorrow.

aphalo commented 1 month ago

Had a very quick look. The function that computes colours from wavelengths I think is internally receiving as input negative wavelength values because of how the reverse scale works. Should be rather easy to fix. I will check in the morning.

aphalo commented 1 month ago

@nist-zack The problem is now fixed in GitHub. You can update package 'photobiology' from https://aphalo.r-universe.dev by running:

install.packages("photobiology", repos = c("https://aphalo.r-universe.dev", "https://cloud.r-project.org"))

The error was triggered by a function in package 'photobiology', not in 'ggspectra'.

aphalo commented 1 month ago

This fix does not solve problems with other transformation. All calls to color_of() need to have all scale transformations reversed, otherwise, for example a log10 or inverse scale transformation result in wrong colours.

nist-zack commented 1 month ago

Thank you to fix this issue. The plot can be labeled without warning now, but I still have a small request. It is now labeled negative value on the spectrum which is not the same as the x-axis , so could you also try to fix this? Maybe just use absolute value when label reverse axis? see the plot following: image I just simply use the code following:

ggplot(sun.spct) +
  geom_line() +
  stat_label_peaks(vjust=-1, span = 31, color = "red") +
  scale_x_reverse()

Thanks again for the fixing.

aphalo commented 1 month ago

@nist-zack I should properly fix this problem with transformed scales. This could involve relatively large edits to several of the stats. It may take a week or two before I get this fixed. I will tell here when the fix is done.

Meanwhile, you can use stat_peaks():

library(ggspectra)

ggplot(sun.spct) +
  geom_line() +
  stat_peaks(vjust=-1, span = 31, color = "red", geom = "text", 
             aes(label = stage(after_stat = after_stat(x)))) +
  scale_x_reverse()
aphalo commented 1 month ago

@nist-zack I implemented a simple manual approach, that I hope to be able to automate in the future. It works by applying a function that the user can supply. I edited stat_peaks(), stat_label_peaks(), stat_valleys() and stat_label_valleys().

After installing the development version of 'ggspectra' with:

install.packages("ggspectra", repos = c("https://aphalo.r-universe.dev", "https://cloud.r-project.org"))

One can use code like this:

ggplot(sun.spct) +
  geom_line() +
  stat_label_peaks(vjust=-1, span = 31, color = "red", 
                   x.label.transform = abs) +
  scale_x_reverse()

or

ggplot(sun.spct) +
  geom_line() +
  stat_label_peaks(vjust=-1, span = 31, color = "red", 
                   x.label.transform = function(x) {10^x}) +
  scale_x_log10()

and get corect labels.

This feature may change before the next release.

nist-zack commented 1 month ago

Thank you for temporary modification for this package. It works normally now.