I am trying to transform the code of a plot from base R to taking advantage of the plotly
library. Here is the code for the plot, as well as an image. contrastTable
is a table that is being read in (above the code here) that contains contrasts between groups for different GSE #'s. (see here for more info)
#Get gene to plot
gene = toupper(readline(prompt="Enter gene name (e.g. GZMA): "))
#Find all instances of query gene in master contrast table
geneLines = grep(paste("^",gene,"$",sep = ""),contrastTable[,"gene"])
#If the gene is found at least once, continue
if (length(geneLines)>0)
{
########Plot contrasts
par(oma = c(1,10,1,1))
geneRows = contrastTable[geneLines,]
fcs = as.list(geneRows$logFC)
names(fcs) = paste(geneRows$GSE,"\n",geneRows$Group2," \U2192 ",geneRows$Group1,sep = "")
maxFC = max(as.numeric(as.character(geneRows$logFC)))
minFC = min(as.numeric(as.character(geneRows$logFC)))
#Set up marker color and type
markers = rep(4,length(fcs))
markers[which(as.numeric(as.character(geneRows[,"adj.P.Val"]))<0.05)] = 15
markers[which(as.numeric(as.character(geneRows[,"adj.P.Val"]))<0.01)] = 19
outerCol = rep("grey20",length(fcs))
outerCol[which(as.numeric(as.character(geneRows[,"adj.P.Val"]))<0.05)] = "darkorange"
outerCol[which(as.numeric(as.character(geneRows[,"adj.P.Val"]))<0.01)] = "red"
layout(t(c(1,2)), c(3.5,1), c(1,1))
#Plot all contrasts
stripchart(fcs,xlim = c(min(-2,minFC),max(2,maxFC)),las = 1,col = outerCol,pch = markers,main = gene,xlab = expression(paste("log"[2],"(Fold Change)")),cex.axis=0.75)
abline(v = 0,col = "grey50",lty=2)
abline(v = log(c(0.5,2/3,1.5,2),2),col = "grey80",lty=2)
usr = par("usr")
legend(usr[1]-(usr[2]-usr[1])/3.5,usr[3]-(usr[4]-usr[3])/10,c("p \u2265 0.05","p < 0.05","p < 0.01"),col = c("black","darkorange","red"),pch = c(4,15,19),cex = 0.7,xpd = TRUE)
#add confidence interval
for (i in 1:nrow(geneRows))
{
segments(geneRows[i,"CI.L"],i,geneRows[i,"CI.R"],i,col = "grey50")
}
#Re-plot to put markers on top
par(new=TRUE)
stripchart(fcs,xlim = c(min(-2,minFC),max(2,maxFC)),las = 1,col = outerCol,pch = markers,xlab = "",ylab = "",xaxt = "n",yaxt="n")
#add lines dividing different GSE#s
datasetList = geneRows$GSE
hLines =(which(duplicated(datasetList) == FALSE) - 0.5)
abline(h = hLines, col = "grey80")
Aside from the obvious spacing/scaling issues with the plot, I have two main questions.
1) How can I recreate the horizontal lines between each GSE # in plotly
? (it is now currently being done by the last two lines of code)
2) How can I recreate the y-axis in plotly
, where it shows the GSE #, and underneath, the group 1 -> group 2
kind of format?
Any help with this would be greatly appreciated!