我正在尝试打印带有多个绘图的pdf。我正在使用循环和 ggplot,这也是一个函数内部。我在"注释"情节时遇到麻烦。我想注释该条件的中位数和整体中位数。我正在使用水平线(geom_hline)和注释("文本")来标记这些行。
问题是单词被正确放置(即 annotate("text", x = 1, y = Cmedian) 似乎工作正常,但水平线绘制在错误的位置(即 geom_hline(aes(yintercept=Cmedian) 不起作用,。它始终将 Cmedian 线放在 30,将全中线放在 22。
我有时会在循环或函数中使用 ggplot 的"奇怪"行为,我想知道如何避免这种情况。
任何帮助,不胜感激。谢谢
suspect_conds<-c(head(suspect_SumPheno[rev(order(suspect_SumPheno$zscore)),],n=20)$condition,tail(suspect_SumPheno[rev(order(suspect_SumPheno$zscore)),],n=20)$condition)
pdf(paste(out_dir,paste(set,org,"bar.SuspectConditionssumPhenoPerPlate.pdf",sep="."),sep=""))
for (i in 1:length(suspect_conds)){
cond<-suspect_conds[i]
cond_subset<-PhenoSumPerPlatPerCond[PhenoSumPerPlatPerCond$condition==cond,]
Cmedian<-median(cond_subset$sum_pheno)
allmedian<-median(PhenoSumPerPlatPerCond$sum_pheno)
plateMedians<-aggregate(sum_pheno~plate,data=PhenoSumPerPlatPerCond,median)
p<-ggplot(cond_subset, aes(x=plate, y=sum_pheno, fill=plate)) + geom_bar(stat="identity") +
labs(title=paste("total phenotypes per plate in",cond,sep=" ")) +
geom_hline(aes(yintercept=Cmedian), colour="black")+ geom_hline(aes(yintercept=allmedian), colour="black",linetype="dashed")+
annotate("text", x = 1, y = Cmedian, label = "median number of phenotypes per plate in this condition",hjust=0,vjust=0)+
annotate("text", x = 1, y = allmedian, label = "median number of phenotypes per plate",hjust=0,vjust=0)+
geom_point(data=plateMedians,aes(x=plate,y=sum_pheno),colour="black",size=4)+
theme_bw()
print(p,file=paste(out_dir,paste(set,org,cond,"bar.sumPhenoPerPlate.pdf",sep="."),sep=""))
}
dev.off()
这个问题与循环中的多个绘图列表(ggplot2)有关。因此,正如那里提到的,您唯一需要做的就是将 aes(...) 替换为 aes_string(...)。