简体   繁体   English

R ggplot2,如何在值栏中绘制一条均值线?

[英]R ggplot2, how to plot a line of the mean among the value bars?

I have the following values that I want to plot into a bar chart with R ggplot2 : 我想使用R ggplot2将以下值绘制到条形图中:

chr1.0  41.744548
chr1.1  52.77
chr1.2  38.941655
chr1.3  34.448161
chr1.4  49.671
chr1.5  36.515152
chr1.6  87.289
chr1.7  52.401747
chr10.0 35.671642
chr10.1 45.259939
chr10.2 81.22807
chr11.0 27.639752
chr11.1 40.451128
chr11.2 32.051282
chr11.3 70.05
chr11.4 35.691824
chr11.5 78.409091
chr12.0 79.77707
chr12.1 73.921
chr12.2 81.6609
chr13.0 31.055901
chr13.1 66.818182
chr14.0 10.785824
chr15.0 26.8657
chr16.0 11.7161
chr16.1 87.5
chr17.0 18.56678
chr17.1     66.055
chr18.0 82.773109
chr19.0 17.453505
chr2.0  24.7734
chr2.1  47.2613
chr2.2  52.922591
chr2.3  56.661046
chr2.4  35
chr2.5  38.848921
chr20.0 41.157
chr20.1     39.712919
chr21.0 40.791738
chr22.0     25.406204
chr22.1 37.614679
chr3.0  46.141975
chr3.1  51.441578
chr3.2  13.875598
chr3.3  41.248097
chr3.4  41.810345
chr4.0  23.982558
chr4.1  85.598706
chr4.2  40.449438
chr4.3  38.028169
chr5.0  16.224189
chr5.1  51.482059
chr5.2  40.980736
chr5.3  46.693387
chr6.0  97.854785
chr6.1  62.947
chr6.2  24.276527
chr6.3  18.449198
chr7.0  17.567568
chr7.1  72.006221
chr7.2  76.049767
chr7.3  40.96
chr8.0  41.269841
chr8.1  61.185185
chr8.2  16.99
chr8.3  46.031746
chr9.0  64.723926
chr9.1  38.125
chr9.2  39.148073

I found the commands to do this: 我找到了执行此操作的命令:

mydata <- read.table("myfile.dat")
colnames(mydata) <- c("chromName", "precisionEUC");
ggplot(mydata, aes(x=chromName, y=precisionEUC)) + 
  geom_bar(aes(fill=precisionEUC>=50), colour="black", 
           position=position_dodge(), stat="identity") + 
  xlab("chromName [a-z]") + 
  ylab("precision LDA-Euclidean")

And that's what I got: 这就是我得到的:

在此处输入图片说明

Among the bars, I also want to print an horizontal line that indicates the mean of the analyzed values, but I don't know how to do this. 在酒吧,我也想打印一个水平线表示分析值的平均值 ,但我不知道如何做到这一点。 In my case the mean is 46.23%, and so I would like to know how to modify the previous R commands to print an horizontal line, to produce something like: 在我的情况下,平均值为46.23%,因此我想知道如何修改前面的R命令以打印水平线,以产生类似以下内容的结果:

在此处输入图片说明

Any idea on how to do this? 关于如何执行此操作的任何想法? Thanks! 谢谢!

Modifying your earlier code: 修改您以前的代码:

ggplot(mydata, aes(x=chromName, y=precisionEUC)) + 
  geom_bar(aes(fill=precisionEUC>=50), colour="black", 
           position=position_dodge(), stat="identity") + 
  xlab("chromName [a-z]") + 
  ylab("precision LDA-Euclidean") +
  geom_hline(yintercept = mean(precisionEUC))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM