簡體   English   中英

R ggplot2 使用堆疊列而不是分組

[英]R ggplot2 with stacked column instead of grouped

我想要 plot 下面顯示的數據作為分組的 bar_plot。

我嘗試position = "dodge"position = "dodge2"但它沒有用。 Ι 也試過position = position_dodge()

如果我使用geom_bar而不是geom_col並刪除y=overlap_percent ,它會起作用:

p3 <- ggplot(data = comp_coors, aes(x = species_code, fill = mirna_form)) + 
  geom_bar(position = "dodge2") + theme_classic()

p3

https://i.stack.imgur.com/bhdU2.png

但我希望 y_axis 有overlap_percent

以堆疊條形圖結束的另一種嘗試是:

p2 <- ggplot(data = comp_coors, aes(x = species_code, y = overlap_percent, fill = mirna_form)) + 
  geom_bar(stat = "identity") + theme_classic()

p2

https://i.stack.imgur.com/erhNo.png

最后通過使用geom_col ,它返回這個它沒有意義,至少對我來說:

p4 <- ggplot(data = comp_coors, aes(x = species_code, y = overlap_percent, fill = mirna_form)) + 
  geom_col(position = "dodge") + theme_classic()

p4

我想要 plot 的數據:

comp_coors <- data.table( species = c("aae","cel", "dme","hsa", "mdo"),
mirna_form = c("mature", "precursor"),
overlap_percent = c(100.0, 100.0, 88.0, 95.5, 91.7, 100.0, 96.6, 98.4),
overlapping_attribute = c("ID=MIMAT0014285;Alias=MIMAT0014285", "ID=MI0000043;Alias=MI0000043;Name=cel-mir-72", "ID=MIMAT0000401;Alias=MIMAT0000401;Name=dme-miR-", "ID=MI0000791;Alias=MI0000791;Name=hsa-mir-383", "ID=MI0005331;Alias=MI0005331;Name=mdo-let-7g")
)

嘗試使用species作為一個因素並添加stat = "identity" ,如下所示:

ggplot(data = comp_coors, aes(x = factor(species), y = overlap_percent, fill = mirna_form)) + 
  geom_bar(position = "dodge", stat = "identity") + theme_classic() + labs(x = "Species", y = "Overlap percent")

Output:

在此處輸入圖像描述

在 y 軸右側具有overlap_percent的分組條形圖。

暫無
暫無

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

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