0

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

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

Erick
  • 11
  • 4
  • 1
    It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your `chrom` dataset. Please do not post an image of data [for these reasons](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Just include the data directly using e.g. `dput()`, i.e. do `dput(head(chrom))` for the first five rows of your data and copy the output into your post. – stefan Apr 11 '23 at 14:20
  • 1
    @stefan I have edited it. Apologies. I was unaware. – Erick Apr 11 '23 at 14:26

1 Answers1

0

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()

stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    Awesome. I need to adjust the plot to make it look prettier but that worked for the essentials. Thanks! – Erick Apr 11 '23 at 14:51
  • Could I ask how you adjust the legend in this code? I'm familiar with doing that in the ggplot function but not with these functions. – Erick Apr 11 '23 at 18:27
  • Hi Erick. Sure. What do you want to change? – stefan Apr 11 '23 at 18:29
  • Thanks! Just need to change the numbers in the legend(id section in your image) with text. i.e. change 1 to "Control" and 2 to "10% liquid" and so on. – Erick Apr 11 '23 at 18:32
  • Try `+ scale_color_discrete(labels = c("Control", "10% liquid", ...))` – stefan Apr 11 '23 at 18:35
  • 1
    Swear I tried that earlier and it wasn't working.... but oh well now it works. Thanks again stefan! – Erick Apr 11 '23 at 18:38