簡體   English   中英

如何在ggplot條形圖中沿x軸分組

[英]How to groups along x axis in ggplot barchart

我有如下的基因組數據

chr   leftPos    V
1      1232      122
1      3232      443
1      43534     13  
3      12        12
3      234234    432
4      1222      155
5      4324      124
5      345345    75
5      456457    83

我想將此數據繪制為由chr組織的條形圖,以便完全按照數據幀顯示chr1的所有條形,然后顯示chr2等

到目前為止,我所管理的是:

ggplot(TBB_2)+
  geom_bar(aes(TBB_2$leftPos, TBB_2$V),stat="identity", fill="red",group="chr")

但是我得到了錯誤

Warning messages:
1: Stacking not well defined when ymin != 0 
2: position_stack requires constant width: output may be incorrect 

編輯

因此,我嘗試了一種替代方法,該方法可以組織為構面圖,以便每個構面都代表一個chr,但是什么都沒有繪制,我不知道為什么不這樣做:

ggplot(TBB_2)+
  geom_bar(aes(x = TBB_2$leftPos,y = as.numeric(TBB_2$V)),stat="identity")+
  facet_wrap(~ chr)

在此處輸入圖片說明

那對你有用嗎?

ggplot(TBB_2, aes(x=factor(leftPos), y=V)) +
  facet_grid(. ~ chr, scales="free_x", space="free_x") +
  geom_bar(stat="identity")

(請注意,您需要將leftPos視為因子,並在x軸上設置自由縮放和間距)

R圖

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM