繁体   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