#' Plot tSNE plot. Version 2.
#'
#' Plot 2D tSNE graph given a data frame. Tailor graphing parameters and save figure in working directory id needed. Can also spit out the tSNE dimensions if requested. Allows for setting perplexity.
#'
#' @param df A data frame on which PCA plot is based. It is transposed before inputting to tSNE algorithm.
#' @param dims A numeric of the number of tSNE dimensions to compute. Defults to 2.
#' @param perplexity A numeric whose 3 times multiplication must be greater than number of columns minus 1. Defults to 30.
#' @param spitOut_tSNE_dims A boolean indicating whether to output the tSNE dimensions as a data frame and assign them to a variable.
#' @param colorVec A vector of colors corresponding to column names of df. Coerced to characted. Defaults to "black" (see http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf).
#' @param label A boolean indicating whether to add labels to PCA dots. Defaults to FALSE.
#' @param label_dist A numeric of the vertical distance between a dot and dot label. Defaults to 0.05.
#' @param main A character of plot title. Defaults to "PCA".
#' @param cex Either a single numeric of label size or a vector of point size. Defaults to a single numeric 1.
#' @param pch A numeric specifying the type of points in the scatter. Defults to 16 which yields filled circles. If many types of points needed please supply a redundant vector of plotting symbol codes (see http://www.statmethods.net/advgraphs/parameters.html) of lenght equal to column names. E.g. pch=c(16,13)[c(rep(1,15),rep(2,125))]
#' @param addLegend A boolean indicating whether to add the legend of colors used in the plot.
#' @param legendPosition A character indicating position of legend. One of "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center. Defults to "topright".
#' @param legendNames A character vector of legend names. Defults to NA.
#' @param legendColors A character vector of colors corresponding to legendNames. Must be in the same left-to-right order (see http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf).
#' @param plotOut A boolean indicating whether to save a plot figure in the working directory.
#' @param width A numeric of plot's figure width (applicable only when plotOut==TRUE).
#' @param height A numeric of plot's figure height (applicable only when plotOut==TRUE).
#' @param plotOutName A character of plot figure file name (applicable only when plotOut==TRUE).
#' @param horizontal A numeric indicating where the draw a horizontal line. Defults to NA.
#' @param vertical A numeric indicating where to draw a vertical line. Defults to NA.
#' @return A tSNE plot and tSNE coordinates if requested.
#' @export
make_tSNEplot.2 <- function(df,dims=2, perplexity = 30, spitOut_tSNE_dims = TRUE,colorVec,label=FALSE,label_dist=0.05, main="tSNE", cex=1, pch = 16, addLegend=FALSE, plotOut=FALSE, width=13, height=8, plotOutName="PCA.pdf", legendPosition="topright", legendNames=NA, legendColors=NA, horizontal=NA, vertical=NA){
library(Rtsne)
tsne <- Rtsne(X = t(df),dims=dims, perplexity = perplexity)
tsne_dims <- tsne$Y
colnames(tsne_dims) <- c("tSNE_dim_1", "tSNE_dim_2")
row.names(tsne_dims) <- colnames(df)
if(plotOut == TRUE){
pdf(plotOutName, width = width, height = height)
plot(x = tsne_dims[,1], y = tsne_dims[,2], xlab = "tSNE_dim_1", ylab = "tSNE_dim_2", col=as.character(colorVec), main = main, type = "p", pch=pch,cex=cex)
if(label==TRUE){
text(tsne_dims[,1], tsne_dims[,2]+label_dist, colnames(df), col = as.character(colorVec), cex = cex)
}
if(addLegend==TRUE){
if(anyNA(legendNames)|anyNA(legendColors)){
stop("'legendNames' or 'legendColors' are/have NA. Exiting!")
}
legend(legendPosition, legendNames, fill=legendColors, bty = "n")
}
if(class(horizontal)=="numeric"){
abline(h = horizontal)
}
if(class(vertical)=="numeric"){
abline(v= vertical)
}
dev.off()
}
else{
plot(x=tsne_dims[,1], y=tsne_dims[,2], xlab = "tSNE_dim_1", ylab = "tSNE_dim_2", col=as.character(colorVec), main = main, type = "p", pch=pch,cex=cex)
if(label==TRUE){
text(tsne_dims[,1], tsne_dims[,2]+label_dist, colnames(df), col = as.character(colorVec), cex = cex)
}
if(addLegend==TRUE){
if(anyNA(legendNames)|anyNA(legendColors)){
stop("'legendNames' or 'legendColors' are/have NA. Exiting!")
}
legend(legendPosition, legendNames, fill=legendColors, bty = "n")
}
if(class(horizontal)=="numeric"){
abline(h = horizontal)
}
if(class(vertical)=="numeric"){
abline(v= vertical)
}
}
# deciding whether to spit out tSNE data
if(spitOut_tSNE_dims==TRUE){
print("Outputting tSNE dims")
return(tsne_dims)
}
else{
print("DONE")
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.