-1

I have dataset in bed file and I want to calculate and plot the length and coverage distribution of file. How can I calculate length distribution in R.

df1:

chr21 2800 3270
chr21 3600 4152
chr2 3719 5092
chr22 3893 4547 
chr2 339 5092
chr22 3563 3597 
structure(list(df1c = c("chr21", "chr21", "chr2", "chr22","chr2"), df1c2 = c(2800, 
3600, 3719, 3893,339,3563), df1c3 = c(3270, 4152, 5092, 4547,5092,3597)), class = "data.frame", row.names = c(NA, 
-4L))
user438383
  • 5,716
  • 8
  • 28
  • 43
  • You do not give any output example. Probably `bedtools genomecov` is a good choice to get the coverage first, which you can then import into R. Something like `rtracklayer::import` comes to mind. For plotting one needs examples. – ATpoint Apr 26 '22 at 14:14

1 Answers1

0

You could do:

library(tidyverse)

df %>% 
  mutate(id = factor(seq(nrow(.)))) %>%
  ggplot(aes(y = df1c2, x = df1c, xend = df1c, yend = df1c3)) +
  geom_segment(aes(y = 1, yend = max(df1c3)), size = 8, lineend = 'round',
               color = 'gray20') +
  geom_segment(size = 7, aes(color = 'coverage')) +
  coord_flip() + 
  labs(color = '', y = 'Location', x = 'Chromosome') +
  theme_light(base_size = 16)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • i am getting this error Error in `ggplot_add()`: ! Can't add `o` to a ggplot object. Run `rlang::last_error()` to see where the error occurred. – user15708301 Apr 26 '22 at 14:07
  • would it be feasible for large datasets? like 600k reads? – user15708301 Apr 26 '22 at 14:08
  • @user15708301 where is `o` coming from? It does not appear in my code. – Allan Cameron Apr 26 '22 at 14:15
  • @user15708301 if you want to show the start and end point of 600K reads, then there is no easy way to show all the different files the reads belong to. If you are only interested in coverage, then you can set the segments to a single fixed color, which should still be feasible for 600K reads. I'll update to demonstrate. – Allan Cameron Apr 26 '22 at 14:18
  • @user15708301 see my update. – Allan Cameron Apr 26 '22 at 14:24
  • Thanks but i dont why I am getting the same error saying `ggplot_add()`: ! Can't add `o` to a ggplot object. Run `rlang::last_error()` to see where the error occurred. – user15708301 Apr 26 '22 at 14:29
  • @user15708301 neither do I. Can you update your question to show _exactly_ the code that is causing that error? With the sample data you provided and the code I provided, this error doesn't occur, so it is impossible for me to debug it. – Allan Cameron Apr 26 '22 at 14:35
  • Hi The error was causing because i have loaded one package ggbio and it was conflicting with ggplot2 package so it was not allowing to plot the graph. Now solved and worked..Thanks for your time and help – user15708301 Apr 26 '22 at 15:03