rggplot2plottrigonometryspectra

How to graph a wave with increasing wavelengths?


I was looking at a bunch of electromagnetic spectrum diagrams and realized that the wave they show never actually corresponds with the given wavelengths in the diagram. For example, the wavelength of the wave at infrared (lambda = 800 nm) should appear 800 times longer than the wavelength at X-ray (lambda = 1 nm).

How can I plot a wave in r such that the wavelength increases proportional to the specified wavelength? i.e. f(x) = lambda and f(n * x) = n * lambda

Simple example just over a small part of the electromagnetic spectrum:

library(ggplot2)

# Make a simple dataset
max.x <- 800 # Maximum wavelength in nanometers (nm) to show
steps <- 10000 # More steps make plotting smooter

waveforms <- as.data.frame(matrix(data=NA, nrow=steps+1, ncol=2, dimnames=list(NULL, c("x", "y"))))
waveforms$x <- seq(0,max.x, by=max.x/steps)


# We can make a wave with periods that increase easily
waveforms$y <- sin(sqrt(waveforms$x))

ggplot()+
  geom_line(data=waveforms, aes(x=x, y=y), color="red")+
  scale_x_continuous("Wavelength in nanometers (nm)")

Plot of increasing wavelength

...but just eyeballing the graph, a wavelength at the low end of the spectrum isn't 800 times shorter than a wavelength at the upper end. What is the actual formula that would make the wavelengths proportional?


Solution

  • I think you need the cumulative sum of the reciprocal of wavelength (obviously you have to skip 0 because otherwise your cumulative sum would be infinite):

    waveforms$y <- c(0, sin(cumsum(1 / waveforms$x[-1])))
    
    
    ggplot()+
      geom_line(data=waveforms, aes(x=x, y=y), color="red")+
      scale_x_continuous("Wavelength in nanometers (nm)")
    

    enter image description here