##Load Data and Install Packages

#install.packages(c("readxl", "dplyr", "ggplot2", "ggrepel"))

library(readxl)
A3 <- read_excel("~/Downloads/A3_Pierce_Standard_0001_A3_26-07-17_13-30_0002.xlsx")
View(A3)

library(readxl)
D4 <- read_excel("~/Downloads/D4.xlsx")
View(D4)

library(ggplot2)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggrepel)

##Format and Filter Datasets

A3 <- A3[,1:2]
D4 <- D4[,c(1,2)]
names(A3) <- c('m_z','int')
names(D4) <- c('m_z','int')

A3filtered <- A3[A3$int>1000,]
D4filtered <- D4[D4$int>10,]
A3filtered$m_z <- round(A3filtered$m_z)
D4filtered$m_z <- round(D4filtered$m_z)

matchDF <- merge(A3filtered,
                 D4filtered,by="m_z")

A3tidy <- cbind(sample='Standard',A3filtered)
D4tidy <- cbind(sample='Digest',D4filtered)
Matchtidy <- cbind(sample='Match',
                   matchDF[,c(1,3)])
names(Matchtidy) <- c('sample','m_z','int')
tidyDF <- rbind(A3tidy,
                D4tidy,
                Matchtidy)

##Plot Data

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