R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00
##### STEP 1
A3 <- read.csv('A3.csv')
B4 <- read.csv('B4.csv')
### STEP 2 - column slice to keep only m/z and intensity
A3new <- A3[,1:2]
B4new <- B4[,1:2]
      #can also use c(#,#) if the column numbers are not next to each other
names(A3new) <- c('m_z', 'int')
names(B4new) <- c('m_z', 'int')


##STEP 3 - Filtering by m_z and round
A3filtered <- A3new[A3new$int>1000,]
B4filtered <- B4new[B4new$int>100,]
A3filtered$m_z <-round(A3filtered$m_z)
B4filtered$m_z <- round(B4filtered$m_z)


#STEP 4 - use merge to keep perfect matches
matchDF <- merge(A3filtered, 
                 B4filtered, by='m_z')



#STEP 5 - Tidy data
A3tidy <- cbind(sample = 'Standard',A3filtered)
    #ggplot needs: sample, variable, value
B4tidy <- cbind(sample = 'Digest',B4filtered)
matchtidy <- cbind(sample = 'Match', 
                   matchDF[,c(1,3)])
names(matchtidy) <- c('sample', 'm_z', 'int')
tidyDF <- rbind(A3tidy,
                B4tidy,
                matchtidy)

#STEP 6 - Plot using ggplot & facet wrap
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
p<-ggplot(tidyDF, aes(x = m_z,y = int))+
  geom_col(aes(colour = sample ,fill=sample))+
  xlab("m/z ratio") + ylab("Millivolts")+
  facet_wrap(~sample,nrow=3)
p

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.