Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Plotting chromatogram from CSV with multiple, independent x and y columns

I am creating an overlay of multiple plots. I have 2 columns for each graph (4 graphs total), with all the x values in the same units and all the y values in the same units. I’m not sure if my csv is formatted properly so that R can overlay each set of mL and mAU values independently, or if it needs to be read in a certain way.

example of my table, but it’s 8 columns instead of 2

ml mAu
val val
val val

image of my csv

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here’s the csv data outputted:

dput(head(chrom))
structure(list(Chrom.1 = c("UV 1_295", "ml", "0", "0.002", "0.005", 
"0.007"), X = c("", "mAU", "0.06199001", "0.0617022", "0.06154499", 
"0.06143872"), Chrom.1.1 = c("UV 1_295 (1)", "ml", "0", "0.003", 
"0.005", "0.007"), X.1 = c("", "mAU", "0.04090555", "0.04144096", 
"0.0419024", "0.04236258"), Chrom.1.2 = c("UV 1_295 (2)", "ml", 
"0.002", "0.004", "0.006", "0.008"), X.2 = c("", "mAU", "0.02393208", 
"0.02436912", "0.02512711", "0.02582231"), Chrom.1.3 = c("UV 1_295 (3)", 
"ml", "0.001", "0.003", "0.006", "0.008"), X.3 = c("", "mAU", 
"0.0300992", "0.02977365", "0.02929245", "0.02880815")), row.names = c(NA, 
6L), class = "data.frame")

I have a juryrigged version set up from what I have read from other posts but it is graphing the x values(mL) as their own lines, which I don’t want.

Here’s the plot I have so far:
incorrect plot

library(readxl)
library(ggplot2)
library(reshape2)

chrom <- read.csv(file = 'Linear Gradient Composite.csv', skip = 2)
colnames(chrom) <- c("mL", "mAU", "mL1","mAU1","mL2", "mAU2", "mL3","mAU3")
chromMelt <- melt(chrom, "mL")
ggplot(chromMelt, aes(mL, value, color = variable)) + geom_line()

Correct plot using answer code:
Overlay chromatogram

>Solution :

As you already guessed your data isn’t in the right shape to achieve your desired result. But that could be fixed by first renaming the columns consistently, i.e. starting with a 1 for the first pair of columns and to make reshaping easier by adding e.g. an _ as separator. Afterwards you could reshape your data using tidyr::pivot_longer and create your plot:

library(ggplot2)
library(tidyr)
library(dplyr, warn=FALSE)

# Skip first two rows
chrom <- chrom[-c(1:2),]

colnames(chrom) <- paste(rep(c("mL", "mAU"), 4), rep(1:4, each = 2), sep = "_")

chrom
#>    mL_1      mAU_1  mL_2      mAU_2  mL_3      mAU_3  mL_4      mAU_4
#> 3     0 0.06199001     0 0.04090555 0.002 0.02393208 0.001  0.0300992
#> 4 0.002  0.0617022 0.003 0.04144096 0.004 0.02436912 0.003 0.02977365
#> 5 0.005 0.06154499 0.005  0.0419024 0.006 0.02512711 0.006 0.02929245
#> 6 0.007 0.06143872 0.007 0.04236258 0.008 0.02582231 0.008 0.02880815

chromMelt <- chrom |> 
  pivot_longer(everything(), names_to = c(".value", "id"), names_sep = "_") |> 
  mutate(across(-id, as.numeric))

ggplot(chromMelt, aes(mL, mAU, color = id)) + 
  geom_line()

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading