1

I would like to plot the data below as geom_rect using ggplot2 in R. Is it possible to plot some that overlaps under each other? It is a bit tricky for me as to plot geom_rect it is needed to specify the ymin and ymax. Any help is so appreciated!

chr start stop 
a   3   7
a   6   8
a   5   10
a   12  14
b   6   8
b   3   8
b   2   5
b   2   5

Genes <- ggplot(mydata, aes(xmin=start,ymin=0.20,xmax=stop,ymax=0.50),inherit.aes=FALSE) +
  xlab("position") +
  geom_rect(color="black") +
  theme(legend.position="none") +
  theme_bw() +
  theme(text=element_text(size=10)) +
  facet_wrap(~chr, ncol = 2, strip.position = "right")
plot(Genes)
Terru_theTerror
  • 4,918
  • 2
  • 20
  • 39
user3224522
  • 1,119
  • 8
  • 19
  • 3
    This is not reproducible. Your data has no headers, and there is a `V4` in your code even though you only have three variables. You have asked 57 questions on this site. I would really hope that you know how to post a clear minimal and reproducible example by now. Also see: [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269) – Axeman Aug 24 '18 at 12:21
  • 1
    Sincerely,this comment is a bit useless for me. I am working without a header, and i indicated the columns in the code, V4 is not vital, I just deleted it. Anyway I appreciate your effort and I have edited the question. – user3224522 Aug 24 '18 at 12:27
  • 2
    It is not useless not me. Why do expect others to fix basic things about your code that you can fix yourself? It is a good idea to be accommodating towards people that you hope will help you for free. (Also, many people are likely to just ignore questions if they see that there is no reproducible code.) The question is much better now. – Axeman Aug 24 '18 at 12:31
  • 1
    What should your desired output look like? – Adela Aug 24 '18 at 12:44

1 Answers1

3

Probably some combination of fill and alpha can be utilized to show the underlying overlap. Here is on idea:

ggplot(df %>%
         mutate(n = 1:n()),
       aes(xmin = start,
           ymin = as.numeric(chr) - 0.1,
           xmax = stop,
           ymax = as.numeric(chr) + 0.1)) +
  xlab("position") +
  geom_rect(color = "black", alpha = 0.5) +
  scale_y_continuous(breaks = c(1, 2),
                     labels = levels(df$chr))+
  theme(legend.position = "none",
        text = element_text(size = 10)) +
  theme_bw() 

enter image description here

the darker the region the more overlaps it has. This might be handy if there are few overlaps but if there are a lot of them I would emphasize them like this:

ggplot(df %>%
         group_by(chr) %>%
         mutate(n = 1:n()),
       aes(xmin = start,
           ymin = n - 0.5,
           xmax = stop,
           ymax = n + 0.5)) +
  xlab("position") +
  geom_rect(color = "black", alpha = 0.5) +
  facet_wrap(~chr, ncol = 1, strip.position = "right")+
  theme_bw() +
  theme(legend.position = "none",
        axis.text.y = element_blank(),
        axis.ticks.y = element_blank())

enter image description here

data:

structure(list(chr = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L), .Label = c("a", "b"), class = "factor"), start = c(3L, 6L, 
5L, 12L, 6L, 3L, 2L, 2L), stop = c(7L, 8L, 10L, 14L, 8L, 8L, 
5L, 5L)), .Names = c("chr", "start", "stop"), class = "data.frame", row.names = c(NA, 
-8L))
missuse
  • 19,056
  • 3
  • 25
  • 47
  • Hi this awesome solution I could integrate it with my another graph...a question, If I increase ymin = n - 2 and ymax = n + 2, to make it more visible, they start overlapping, why is that? what should I modify to overcome it? – user3224522 Aug 24 '18 at 14:07
  • @user3224522 remove the part `axis.text.y = element_blank(), axis.ticks.y = element_blank()` from `theme` and you will understand the reasons. As for increasing visibility try to manipulate the output image dimensions. – missuse Aug 24 '18 at 15:24