0

I have been trying to plot a spectra but I was not successful.

What I have is a file with over 300 columns and 10000 rows (containing intensity information for 300 different signals) and another data with 1 column and 10000 rows (which contains the wavelength measurements corresponding to the 300 signals' intensity levels). I want to plot all 300 columns of intensity data against the wavelength data,

I checked this one Plotting multiple spectra with different colors in a hyperSpec object

but it did not help much, I try to simulate a data which can be used for interpretation

Its <- matrix(rexp(200, rate=0.1),nrow=10000,ncol=300)
Wls <- 1:10000

I tried to put them together first

df <- cbind(Wls,Its) 

Then i melt it in order to use the ggplot according to what is commented before,

library(reshape2)
library(ggplot2)
melted data <- melt(df,id.vars = 'V1')
ggplot(melted) + 
  aes(x=df, y=value, group=variable, color=variable) + 
  geom_line()

Is there really a good way to plot spectra ?? I really appreciate any help

Community
  • 1
  • 1
Best
  • 31
  • 7
  • Can you clarify a bit more on what the two data-sets are actually supposed to contain. – B.Shankar May 26 '15 at 15:30
  • 300 lines in a plot don't seem very useful. – Roland May 26 '15 at 15:42
  • @B.Shankar i cannot post a photo above, but I try to show a link here.http://www.nature.com/srep/2015/150130/srep08050/fig_tab/srep08050_F5.html – Best May 26 '15 at 17:21
  • @Roland my reputation is low but if you google specta plot, you will see that , it shows variation over wavelength , so for me it is very useful – Best May 26 '15 at 17:24
  • What I want to know is the physical quantities for which your data-sets store measurements. For example, do the columns (or maybe the rows) store measurements of **intensities** or **frequencies** or **wavelengths** ... What is the exact relationship between your two data-sets ... – B.Shankar May 26 '15 at 18:38
  • @B.Shankar Yes they are intensities – Best May 27 '15 at 06:19
  • But to plot a spectrum you need frequency information. And again, if all are intensities then why one column is in a separate data-set. – B.Shankar May 27 '15 at 08:59
  • @B.Shankar This is just an example simulated data. the T is intensities and TT is wavelength . normally it is easy to plot it like plot(TT, T[,1]) etc, but I want to plot all columns versus TT , now it is clear? – Best May 27 '15 at 11:49

1 Answers1

1

This is one possible solution. I am posting here a smaller example but you can make it work for your data-sets with only minor modifications.

library(reshape2)
library(ggplot2)

# Generate artificial data
Its <- matrix(sapply(1:30, function(N) {
    100 * dcauchy(x = c(scale(1:1000, scale = 1000)), 
                  location = dnorm(rnorm(1))/2, 
                  scale = 0.4+0.5*(N-1)/29, 
                  log = F)
  }), ncol = 30) # Intensities data-set
Wls <- 100:1100  # Wavelengths data-set

# Combine the relevant data and process it for plotting; replace "Wls", "Its" with your data-sets.
Full.Set <- data.frame(cbind(Wls, Its))
colnames(Full.Set) <- c("Wavelength", paste0("Sig.", 1:30)) # change 30 to the number of columns 
                                                            # in your intensity data-set.
Melted.Set <- melt(Full.Set, id.vars = "Wavelength")

# Plot 
ggplot(Melted.Set, 
       aes(x = Wavelength, y = value, color = variable)) + 
  geom_line()

I got a graph like this :

Spectra for artificially generated data, doesn't necessarily correspond to a real phenomenon

A point worth mentioning is that plotting 300 different signals in a single plot might complicate the plot too much to be of high utility.

B.Shankar
  • 1,271
  • 7
  • 11
  • If we are in visible spectra range I would change the colors to this [RGB values of visible spectrum](http://stackoverflow.com/a/22681410/2521214) That should make easier to comprehend the frequency dependency. Also additive transparency for the curves would be beneficial I think. – Spektre Mar 01 '17 at 12:37