When I tried to add lines to my graph, I became confused of how to get correct index to draw the line so that the points can be located right in the middle of the bar,below are the graph and the code I used to plot it. I wonder how to adjust the codes to make the positioning of the points better.
cupbp<-read.xlsx("Go.xlsx",sheet=2)
cupbp2<-subset(cupbp,PValue<0.05)
cupbp2$P.value<-log(cupbp2$PValue,base=2)
cupbp2$P.value<-abs(cupbp2$P.value)
cupbp2$bp<-substr(cupbp2$Term,12,nchar(cupbp2$Term))
opar<-par(no.readonly=T)
par(mar=c(5,25,7,10)+0.1)
par(cex.axis=0.6)
mybar<-barplot(height=cupbp2$P.value,names=cupbp2$bp,horiz=TRUE,las=1,xaxt="n",col="red",xlab="-log2 P value",xlim=c(0,ceiling(max(cupbp2$P.value))))
title("control vs AD up Bioprocess",line=4,adj=0.5)
axis(side=1,at=axTicks(1))
par(new=T)
plot(cupbp2$Count, mybar,
type="o", pch=19, lwd=2, col="yellow", cex=1.2,
ann=F,xaxt="n",yaxt="n",xlab="Gene number")
axis(side=3,at=axTicks(3))
mtext("Gene number",3,line=2)
par(opar)
Barplot function provided the midpoint value, stored in "mybar" in my example, however, plot function was not suitable to plot the lines, for a reason I don't quite understand right now, changing plot(count, mybar) to lines(count, mybar) solves the problem of the points location, but this time the axis of the graph goes wrong. Maybe using lines() will cause R to consider the second graph not a new graph, so the axis was contained as the original one. See this modified example, in which the gene number axis should be 0 to 7, but it was kept as 0 to 14 as the original one.
Please give me precious suggestions about how to adjust the figure as expected.