繁体   English   中英

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

[英]geom_bar issues with y axis values incorrect placement

我有两个数据集,每个数据集都有 10000 个染色体区域。 然后我计算我的染色体区域与特定染色体元素 (LINE) 重叠的次数。 我这样做了 4 次,如果我的染色体区域与 30%、50%、80% 和 100% 的 LINE 元素重叠,我将计算重叠。

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

这是我所做的一个简单的例子。 我已经用我需要做 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)

我得到以下情节

在此处输入图片说明

我的问题是我希望 LINE 1 的紫色条形 100% 重叠成为最低条形,因为它的 y 轴值最小为 955,所以我不确定为什么它显示为高于该 LINE1 组的其他条形? 我还希望紫色条像其他两个组一样位于左侧,因此根据值进行排序。 它似乎适用于 LINE2 和 LINE3 组,其中最小值位于左侧,并且对于每个 LINE 将它们很好地分为“chr_reg”和“random”。 这就是为什么我无法理解为什么“LINE1”“chr_reg”存在问题。

所以理想情况下是这样的: 在此处输入图片说明

您的数据格式不正确,因此您的情节看起来很“奇怪”。 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)

在此处输入图片说明

根据您的需要,您可能还想将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)

在此处输入图片说明

或者,如果您真的想保持原始列不变并匹配所需的输出图:

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