簡體   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