I have a little script that allows me to create overlapped Manhattan plots with ggplot2 between 2 different dataframes (SNPs and CpGs). I have interesting columns in 22 chromosomes and intend to get 22 plots. However, I was thinking that repeating the same code over and over 22 times was a bit of an overkill, but I do not know if it is possible to do it a bit more compact with R.
This is the code fragment I will have to repeat over and over if I want this to work:
#PLOT OF CHROMOSOME 1 chr1_SCPG_computed <- ch1_SCPG %>% # Compute chromosome size group_by(CHR) %>% summarise(chr_len=max(POSITION))%>% # Calculate cumulative position of each chromosome mutate(tot=cumsum(as.numeric(chr_len))-chr_len) %>% dplyr::select(-chr_len) %>% # Add this info to the initial dataset left_join(ch1_SCPG, ., by=c("CHR"="CHR")) %>% # Add a cumulative position of each SNP arrange(CHR, POSITION) %>% mutate( BPcum=POSITION+tot) axisC1_2 = chr1_SCPG_computed %>% group_by(CHR) %>% summarize(center=(max(BPcum) + min(BPcum) ) / 2 ) axisC1_2$CHR <- as.numeric(axisC1_2$CHR) ggplot()+ geom_point(data=chr1_SSNP_computed, aes(x=BPcum, y=-log10(P)), colour = "red", size=1.3, alpha=0.5, shape=1)+ geom_point(data=chr1_SCPG_computed, aes(x=BPcum, y=-log10(P)), colour = "dark green", size=1.3, alpha=0.5, shape=1)+ geom_hline(yintercept = -log10(sig), color ="red", linetype = "dashed")+ scale_x_continuous(label= axisC1$CHR, breaks = axisC1$center)+ scale_y_continuous(expand = c(0, 0))+ theme_bw() + theme( legend.position="none", panel.border = element_blank(), panel.grid.major.x = element_blank(), panel.grid.minor.x = element_blank() )
Any ideas would be appreciated, thanks!