简体   繁体   中英

geom_bar issues with y axis values incorrect placement

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

在此处输入图片说明

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: 在此处输入图片说明

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)

在此处输入图片说明

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)

在此处输入图片说明

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)

在此处输入图片说明

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM