Skip to content

Instantly share code, notes, and snippets.

@jnpaulson
Last active August 29, 2015 14:00
Show Gist options
  • Save jnpaulson/caa77199dd3b402e87fc to your computer and use it in GitHub Desktop.
Save jnpaulson/caa77199dd3b402e87fc to your computer and use it in GitHub Desktop.
Bubble plot function!
bubblePlot<-function(yvector,xvector,sigvector=NULL,nbreaks=10,ret=FALSE,scale=1,...){
#if(names(yvector)%in%names(xvector)){
# stop("Name the y and x vectors -- ideally the same name ;-)")
#}
ybreaks = cut(yvector,breaks=quantile(yvector,p=seq(0,1,length.out=nbreaks)),include.lowest=T)
xbreaks = cut(xvector,breaks=quantile(xvector,p=seq(0,1,length.out=nbreaks)),include.lowest=T)
numFeatures = lapply(levels(xbreaks),function(i){
k = which(xbreaks==i)
sapply(levels(ybreaks),function(j){
length(which(ybreaks[k]==j))
})
})
names(numFeatures) = levels(xbreaks)
yvec = 1:length(levels(ybreaks))
nc = length(yvec)
if(!is.null(sigvector)){
# I am calculating numFeatures twice if sigvector==TRUE
# This can be changed to if else statement to return two rows
numSig = lapply(levels(xbreaks),function(i){
k = which(xbreaks==i)
sapply(levels(ybreaks),function(j){
x = sum(names(yvector[k])[which(ybreaks[k]==j)]%in%names(sigvector))/length(which(ybreaks[k]==j))
if(is.na(x)) x = 0
x
})
})
}
medianSizes = median(unlist(numFeatures))
plot(y=yvec,x=rep(1,nc),cex=scale*numFeatures[[1]]/medianSizes,
xlim=c(-0.25,nc+.25),ylim=c(-0.25,nc+.25),bty="n",xaxt="n",yaxt="n",
xlab="",ylab="",pch=21,...,bg=rgb(blue=1,red=0,green=0,alpha=numSig[[1]]))
for(i in 2:length(numFeatures)){
points(y=yvec,x=rep(i,nc),cex =scale*numFeatures[[i]]/medianSizes,pch=21,bg=rgb(blue=1,red=0,green=0,alpha=numSig[[i]]))
}
axis(1,at = 1:nc,labels=levels(xbreaks),las=2,cex.axis=.5)
axis(2,at = 1:nc,labels=levels(ybreaks),las=2,cex.axis=.5)
if(ret == TRUE){
return(cbind(xbreaks,ybreaks))
}
}
@LeeMendelowitz
Copy link

xlabel and ylabel?

@jnpaulson
Copy link
Author

What do you mean xlabel and ylabel? Currently the function takes two ordered vectors and breaks each separately into nbreaks. The breaks are turned into tick marks and displayed on the respective x,y axis. The ... in the function allows for sending a ylab and xlab.

I'm not sure if I should change the function to allow n1 breaks for y and n2 breaks for x; automatically size circles, etc. any suggestions are greatly appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment