1

I have two datasets each with 10000 chromosomal regions. Then I count the number of times my chromosomal regions overlap with a specific chromosomal element (LINE). I do this 4 times, where I count an overlap if my chromosomal region overlap with 30%, 50%, 80% and 100% of the LINE elements.

Then I wish to make a bar pot showing the less percentage overlap required to count an actual overlap with the LINEs the more overlaps do you get.

So a simple example of what I've done. I have defined my vectors with the values i need to do the facet_wrapt and filling and so on.

overlap <- c(0.3,0.5,0.8,1.0,
             0.3,0.5,0.8,1.0,
             0.3,0.5,0.8,1.0,
             0.3,0.5,0.8,1.0,
             0.3,0.5,0.8,1.0,
             0.3,0.5,0.8,1.0)

region <- c("chr_reg","chr_reg","chr_reg","chr_reg",
          "chr_reg","chr_reg","chr_reg","chr_reg",
          "chr_reg","chr_reg","chr_reg","chr_reg",
          "random","random","random","random",
          "random","random","random","random",
          "random","random","random","random")

Element <- c("LINE1","LINE1","LINE1","LINE1",
         "LINE2","LINE2","LINE2","LINE2",
         "LINE3","LINE3","LINE3","LINE3",
         "LINE1","LINE1","LINE1","LINE1",
         "LINE2","LINE2","LINE2","LINE2",
         "LINE3","LINE3","LINE3","LINE3")

No <- c(1100,1000,1000,900,
        3000,3000,2900,2900,
        1900,1500,1700,1500,
        2500,2500,2500,2600,
        5200,5000,5200,5000,
        3500,3000,3500,3600)


df_full2 <- as.data.frame(cbind(overlap,Element,region,No))

ggplot(df_full2,aes(x = region, y = No,fill = overlap)) + 
  geom_bar(stat = "identity", position = "dodge",colour="black")+
  theme_bw() + facet_wrap(~Element)

and i get the following plot

enter image description here

My issue is I would like for LINE 1 the purple bar so 100 percent overlap to be the lowest bar since it has the smallest y-axis value of 955 so I'm not sure why its shown as higher than the others for that LINE1 group? I would also like the purple bar to be on the left like for the two other groups, so sorted based on the values. It seem to work for the groups LINE2 and LINE3, where the smallest values are on the left and they are separated nicely for each LINE into "chr_reg" and "random". which Is why I'm having trouble understanding why there are issues for "LINE1" "chr_reg".

So ideally something like this: enter image description here

4
  • can you somehow show your desired output?
    – bird
    Commented Jul 18, 2021 at 8:31
  • My desired output would just be in the first group "LINE1" to have the purple bar for the group "chr_reg" to be on the left and be the smallest bar, since if you look on the y-axis it has a value of 955, which is lower than all the other bars, in the same group "LINE1 -> chr_reg" then the blue bare with 0.8 has a value of 1008 which is why the purple should be smaller. So the desired output is just I want the LINE1 group to have the same output as seen in LINE2 and LINE3 group, where the ordering and values are correct. Does it make sense? thanks. Commented Jul 18, 2021 at 8:34
  • Consider using df_full2 <- data.frame(overlap,Element,region,No) to construct the dataframe.
    – Ronak Shah
    Commented Jul 18, 2021 at 8:49
  • Ronak is correct. Your use of cbind forces R to coerce all the data to the same type, which is why "No" gets converted to character, and is plotted incorrectly. data.frame should be used without cbind.
    – jdobres
    Commented Jul 25, 2021 at 17:29

2 Answers 2

1

No is character just add as.integer

ggplot(df_full3,aes(x = region, y = as.integer(No),fill = overlap)) + 
  geom_bar(stat = "identity", position = "dodge",colour="black")+
  theme_bw() + facet_wrap(~Element)

enter image description here

2
  • 1
    I agree that integer makes more sense in this case. I updated my answer to integer credits to you! :)
    – bird
    Commented Jul 18, 2021 at 9:10
  • I think we both provide a good solution! Upvote!
    – TarJae
    Commented Jul 18, 2021 at 9:35
1

Your data is not in the right format, hence your plot looks "odd". No needs to be an integer column:

library(tidyverse)
df_full2 %>%
        mutate(No = as.integer(No)) %>% 
        ggplot(aes(x = region, y = No,fill = overlap)) + 
        geom_bar(stat = "identity", position = "dodge",colour="black")+
        theme_bw() + facet_wrap(~Element)

enter image description here

Depending on your needs, you may also want to convert overlap into a numeric variable:

df_full2 %>%
        mutate(No = as.integer(No),
               overlap = as.numeric(overlap)) %>% 
        ggplot(aes(x = region, y = No, fill = overlap)) + 
        geom_bar(stat = "identity", position = "dodge",colour="black")+
        scale_fill_viridis_c() +
        theme_bw() + facet_wrap(~Element)

enter image description here

Or, if you REALLY want to keep your original columns as is and match your desired output plot:

df_full2 %>%
        mutate(No = fct_reorder(No, as.integer(No))) %>% 
        ggplot(aes(x = region, y = No, fill = overlap)) + 
        geom_bar(stat = "identity", position = "dodge",colour="black") +
        theme_bw() + facet_wrap(~Element)

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.