简体   繁体   English

y 轴值的 geom_bar 问题位置不正确

[英]geom_bar issues with y axis values incorrect placement

I have two datasets each with 10000 chromosomal regions.我有两个数据集,每个数据集都有 10000 个染色体区域。 Then I count the number of times my chromosomal regions overlap with a specific chromosomal element (LINE).然后我计算我的染色体区域与特定染色体元素 (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.我这样做了 4 次,如果我的染色体区域与 30%、50%、80% 和 100% 的 LINE 元素重叠,我将计算重叠。

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.然后我希望制作一个酒吧锅,显示计算与 LINE 的实际重叠所需的重叠百分比越少,您得到的重叠就越多。

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.我已经用我需要做 facet_wrapt 和填充等的值定义了我的向量。

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?我的问题是我希望 LINE 1 的紫色条形 100% 重叠成为最低条形,因为它的 y 轴值最小为 955,所以我不确定为什么它显示为高于该 LINE1 组的其他条形? 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".它似乎适用于 LINE2 和 LINE3 组,其中最小值位于左侧,并且对于每个 LINE 将它们很好地分为“chr_reg”和“random”。 which Is why I'm having trouble understanding why there are issues for "LINE1" "chr_reg".这就是为什么我无法理解为什么“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: Nointeger列:

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:根据您的需要,您可能还想将overlap转换为numeric变量:

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 No ,字符只是添加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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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