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

stat_label_peaks() etc. with transformed wavelength scales #14

Open
nist-zack opened this issue Sep 26, 2024 · 8 comments
Open

stat_label_peaks() etc. with transformed wavelength scales #14

nist-zack opened this issue Sep 26, 2024 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@nist-zack
Copy link


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()
@nist-zack nist-zack added the question Further information is requested label Sep 26, 2024
@aphalo
Copy link
Owner

aphalo commented Sep 26, 2024

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

aphalo commented Sep 26, 2024

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

aphalo commented Sep 27, 2024

@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
Copy link
Owner

aphalo commented Sep 27, 2024

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.

  • Add unit tests for scale transformations
  • Fix all calls to color_of()

@aphalo aphalo added bug Something isn't working and removed question Further information is requested labels Sep 27, 2024
@aphalo aphalo changed the title Question for stat_label_peaks() stat_label_peaks() etc. with transformed wavelength scales Sep 27, 2024
@aphalo aphalo added this to the v0.3.14 milestone Sep 27, 2024
@nist-zack
Copy link
Author

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

aphalo commented Sep 27, 2024

@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
Copy link
Owner

aphalo commented Sep 27, 2024

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

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

@aphalo aphalo modified the milestones: v0.3.14, Future version Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants