0

enter image description hereTrying to plot multiple curves on one graph using ggplot2. The x-axis will be wavelength, the y-axis will show intensity.

Data Table (called ‘Set’) is shown below (col2 refers to column 2 etc.), wavelength goes from 3600 to 8000 wavenumbers. Each of col1, 2, 3 all have different values, leading to different curves. The columns provide values in intensity.

Wavelength col1 col2 col3

The melt syntax can be found below, at melted.

    df<-data.table(Set)
    melted <- melt(df, id.vars = NULL, measure.vars=colnames(df), 
variable.name = "wavelength", value.name = "value")
wavelength  value

1: Wavelength 3600.809

2: Wavelength 3601.637

etc

To plot,

    ggplot(
      plot_input[wavelength %in% c("col1", "col2", "col3”)],
      aes(y=value, x=wavelength)) + 
      geom_line() + 
      theme_classic()

Issues –

2 ggplot output [per image] results in three vertical lines, am trying to plot three distinct curves for each of col1, col2, col3

2 ggplot – looking for wavenumbers on the x-axis, not col_num

badly constructed plot

I am probably missing something very simple. Could someone explain what I am doing incorrectly?

Thank you.

[additional image added, apols for poor quality]. Shown are 5 spectra, in colours. The data is contained in the columns - y values]

Screamh
  • 39
  • 5

1 Answers1

0

The main question is that what x axis in the plot represents ?

Now I think it's what you desire to see

library(data.table)
library(tidyverse)

set.seed(1453)

Set <- data.frame(col1=sample(6000:7000,size = 20,replace = T),
                 col2=sample(6000:7000,size = 20,replace = T),
                 col3=sample(6000:7000,size = 20,replace = T))

df <- data.table(Set)

melted <- melt(df, id.vars = NULL, measure.vars=colnames(df), 
variable.name = "wavelength", value.name = "value")

melted %>%
ggplot(aes(x=value,color=wavelength))+
geom_density()

enter image description here

Samet Sökel
  • 2,515
  • 6
  • 21