1

I'm trying to visualize my preliminary data by showcasing the specific compounds and their retention times. What I'm trying to aim for is a graph that is able to show retention times on the x-axis and Ion mass on the y-axis. Some of the ions either have an exact retention time or a time frame of the time that it elutes in the analytical instrument.

In the example dataframe below QIon is the Ion mass, RT is the time frame if any, and RT_Center is the exact retention time. Some compounds overlap by having the same mass, but different Retention times, so on the graph the time frames may be on the same y axis but differ on the x-axis. Please let me know if you need any additional information.

Compound       QIon   RT      RT_Center

Biphenyl       154    30-32
Fluorene       166            37.1
Dibenzofuran   168    31-34
Acenaphthene   154            34.14
Phil
  • 7,287
  • 3
  • 36
  • 66
David
  • 43
  • 5
  • Are you familiar with `ggplot2`? – Érico Patto Apr 06 '21 at 20:57
  • Yes I am familiar with ggplot2, but unsure how to create the code to accomplish what I want since RT has a dash indicating a time range. – David Apr 06 '21 at 21:01
  • By the way, it's worth looking into `tibble`s. They're kind of `tidyverse`'s improved `data.frames`, and they solve a lot of problems – such as weird column names. They become ok. – Érico Patto Apr 06 '21 at 22:12
  • Also, it often helps to have the your data displayed in the format outputted by `dput()`; this makes it easier to import. – Érico Patto Apr 06 '21 at 22:15

1 Answers1

1

I wrote this code the way I am more comfortable with, but I'll probably put some other ways in here too.

I loaded the entire library tidyverse, but what I used was ggplot2 and tidyr.

library(tidyverse) # Or ggplot2 and tidyr

chem %>%
  separate(col = RT, sep = "-", into = c("Beginning", "End"), convert = TRUE) %>%
  ggplot(aes(y = QIon))+
  geom_point(aes(x = RT_Center))+
  geom_errorbar(aes(xmin = Beginning, xmax = End))+
  theme_minimal()

More customization is up to you.

Here is what it means: first, I separated the column RT into a beginning and an end. This way, I could use those columns to create a range as well as the points (check ?geom_errorbar to see other geoms).

I believe this is what you meant. Since there are both ranges and single dots, I thought using both would be the most appropriate. If that's not what you were looking for, feel free to say so.

Here's a way without using the pipelines, if it makes you more comfortable:

library(ggplot2)

chem2 <- tidyr::separate(chem, col = RT, sep = "-", into = c("Beginning", "End"), convert = TRUE)
ggplot(chem2, aes(y = QIon))+
  geom_point(aes(x = RT_Center))+
  geom_errorbar(aes(xmin = Beginning, xmax = End))+
  theme_minimal()
Érico Patto
  • 1,015
  • 4
  • 18