0

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)

enter image description here

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.

enter image description here

Please give me precious suggestions about how to adjust the figure as expected.

neilfws
  • 32,751
  • 5
  • 50
  • 63

1 Answers1

0

Without your data, I could not try it on my side, but I think it is possible: first, multiply your gene number by 2, and then generate graph, but label 0 as "0", 4 as "2", 6 as "3", 8 as "4" ... and 14 as "7".

Zhiqiang Wang
  • 6,206
  • 2
  • 13
  • 27
  • You can try it with any table with the form like this 'gene-name'-'Pvalue'-'Count', sorry I don't know how to upload my table here – 超菊苣 Oct 10 '19 at 01:35
  • check this may help: https://stackoverflow.com/questions/58323116/different-variable-not-the-transformed-value-of-primary-axis-variable-on-secon/58323334#58323334 – Zhiqiang Wang Oct 10 '19 at 12:57
  • Thank you for your advice, I‘ll try it later’ – 超菊苣 Oct 12 '19 at 08:09