gpt4 book ai didi

r - 使用 ggplot2 在条形图中显示重叠数据

转载 作者:行者123 更新时间:2023-12-01 16:41:56 27 4
gpt4 key购买 nike

我生成了描述数据情况的条形图,如下所示并使用以下代码:

enter image description here

    mydf
Groups Gene_content Cases Intersection
25 903 Case1 512
25 817 Case2 512
20 722 Case1 400
20 644 Case2 400
15 543 Case1 332
15 469 Case2 332
10 357 Case1 172
10 287 Case2 172
5 184 Case1 65
5 125 Case2 65
        ggplot(mydf, aes(fill=Cases, y=Gene_content, x=Groups)) +        
geom_bar(position="dodge", stat="identity", color="black") +
geom_text(aes(label=Intersection), vjust=1.6, color="white",
position = position_dodge(0.9), size=3.5)+
geom_errorbar(aes(y = Intersection, ymin = Intersection,
ymax = Intersection), color="Orange",lty=2) +
scale_fill_brewer(palette="Set1")+
theme_minimal()

我想要的是:首先用相同的颜色对橙色线下的所有条形进行着色,然后区分直到橙色水平线的条形,因为它们已经是这样。共同颜色(黄色)表示“共同”元素。此外,我会将文本向下移动,表明有多少基因是共同的。所需的输出(根据功率点修改)如下: enter image description here

最佳答案

简单的做法是在顶部添加另一层geom_bar,高度为y=Intersection。您可以通过更改 geom 调用中的 y 美感轻松下移文本。

 ggplot(aes(fill=Cases, y=Gene_content, x=factor(Groups))) +        
geom_bar(position="dodge", stat="identity", color="black") +
geom_bar(position="dodge", stat="identity", aes(y = Intersection, group = Cases),
fill = "yellow", colour = "black") +
geom_text(aes(label=Intersection, y = Intersection), vjust=1.6, color="black",
position = position_dodge(0.9), size=3.5)+
geom_errorbar(aes(y = Intersection, ymin = Intersection,
ymax = Intersection), color="Orange",lty=2) +
scale_fill_brewer(palette="Set1")+
theme_minimal()

关于r - 使用 ggplot2 在条形图中显示重叠数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43850354/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com