ATAC seq : Plotting insert sizes My tutorial link
library(GenomicAlignments)

atacReads &lt;- readGAlignmentPairs(&quot;MACS2_peak_call/ALL_ATAC_data/HSC1.bam&quot;, param = ScanBamParam(mapqFilter = 1, 
            flag = scanBamFlag(isPaired = TRUE, isProperPair = TRUE), what = c(&quot;qname&quot;, 
        &quot;mapq&quot;, &quot;isize&quot;), which = GRanges(&quot;chr20&quot;, IRanges(1, 63025520))))
# length(atacReads)
atacReads


atacReads_read1 &lt;- GenomicAlignments::first(atacReads)
insertSizes &lt;- abs(elementMetadata(atacReads_read1)$isize)
head(insertSizes)


library(magrittr)
library(dplyr)
library(ggplot2)
fragLenPlot &lt;- table(insertSizes) %&gt;% data.frame %&gt;% dplyr::rename(InsertSize = insertSizes, 
    Count = Freq) %&gt;% mutate(InsertSize = as.numeric(as.vector(InsertSize)), 
 Count = as.numeric(as.vector(Count))) %&gt;% ggplot(aes(x = InsertSize, y = Count)) + 
  geom_line()

fragLenPlot + theme_bw()

fragLenPlot + scale_y_continuous(trans = &quot;log2&quot;) + theme_bw()

How to i specify the Grange to span the total chromosome. Now I use only &quot;chr20&quot; as shown in the example.
You can exclude the which statement and get the entirety of all of the chromosomes. Note however that R is a very bad platform to use for this, since you end up reading everything into memory (I hope the BAM file isn't huge).
Note that you're seriously reinventing the wheel. There are a number of packages that will directly produce the plot you want, such as bamPEFragmentSize from deepTools.