1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
### singleclusterplot ###
#' @export
#' @name singleclusterplot
#' @aliases LSD.singleclusterplot
#' @title Visualize two-dimensional data clusters (add to an existing plot)
#' @description Depict a numeric matrix or list utilizing the underlying distribution quantiles of one dimension in a color encoded fashion (add to an existing plot).
#' @param input matrix or list with numerical entries.
#' @param at a integer vector containing the x-positions corresponding to the columns of 'input'.
#' @param fromto a numeric vector containing the range of quantiles (between 0 and 1) to be plotted.
#' @param colpal a character vector containing R built-in color names or a name of a \code{LSD} colorpalette as a character string (see disco() or \code{\link{disco}}).
#' @param simulate logical: if \code{TRUE} (\code{FALSE} by default), a converted colorpalette is used to simulate dichromat vision according to \url{http://www.daltonize.org} (see \code{\link{daltonize}}).
#' @param daltonize logical: if \code{TRUE} (\code{FALSE} by default), a converted colorpalette is used to enhance dichromat vision according to \url{http://www.daltonize.org} (see \code{\link{daltonize}}).
#' @param cvd character string implying the type of color vision deficiency ("p" for protanope, "d" for deuteranope or "t" for tritanope).
#' @param nrcol a non-negative integer specifying the number of colors to be used (defaults to 25, if not specified).
#' @param outer.col R built-in color to be used for outlier lines (lines outside of 'fromto').
#' @param rev logical: if \code{TRUE} (\code{FALSE} by default), a reversed colorpalette is used.
#' @param alpha alpha value: a two-digit integer between 01 and 99 for color opacity, i.e. appearance of partial or full transparency (usage omitted by default).
#' @param quartiles.col a character vector containing three R built-in colors for quartile lines (c('0.25','0.5','0.75')).
#' @param add.quartiles logical: if \code{TRUE} (by default), lines are plotted corresponding to the quartiles.
#' @author Achim Tresch, Bjoern Schwalb
#' @seealso \code{\link{clusterplot}}, \code{\link{align}}, \code{\link{demotour}}, \code{\link{disco}}, \code{\link{colorpalette}}
#' @examples samples = 100
#' probes = 200
#' clus = matrix(rnorm(probes*samples,sd=1),ncol=probes)
#'
#' clus = rbind(
#' t(t(clus)+sin(1:probes/10))+1:nrow(clus)/samples,
#' t(t(clus)+sin(pi/2+1:probes/10))+1:nrow(clus)/samples)
#'
#' emptyplot(xlim = c(1,ncol(clus)),ylim = range(clus))
#' singleclusterplot(clus)
#' axis(1)
#' axis(2)
#' box()
#' @keywords cluster
singleclusterplot = function(input,at = NULL,fromto = c(0.05,0.95),colpal = "standardheat",simulate = FALSE,daltonize = FALSE,cvd = "p",nrcol = 25,outer.col = "lightgrey",rev = FALSE,alpha = NULL,quartiles.col = c("grey","black","grey"),add.quartiles = TRUE)
{
# stops execution, if 'input' is neither a list nor a matrix and executes an error action #
if (!is.matrix(input) & !is.list(input)) stop("'input' must be a matrix or a list !")
# define x-positions corresponding to the columns/elements of 'input' #
if (is.null(at)) if (is.matrix(input)){at=1:ncol(input)} else if (is.list(input)){at=1:length(input)}
# preliminaries #
probes = length(at)
# wrapper for the lines function #
drawline = function(y,col="black",lwd=1,lty=1){lines(at[1:length(y)],y,type="l",col=col,lwd=lwd,lty=lty)}
# optional drawing of outlier lines #
if (is.matrix(input)){if (outer.col!="none") apply(input,1,drawline,col=outer.col)}
# provide 'colpal' via colorpalette #
colpal = colorpalette(colpal,nrcol,simulate = simulate,daltonize = daltonize,cvd = cvd,alpha = alpha,rev = rev)
colpal = c(rev(colpal),colpal)
# determine quantiles among columns/elements of 'input' #
if (is.matrix(input)){
qline = apply(input,2,quantile,probs=seq(fromto[1],fromto[2],length=(length(colpal)+1)),na.rm=TRUE)
} else if (is.list(input)){
qline = lapply(input,quantile,probs=seq(fromto[1],fromto[2],length=(length(colpal)+1)),na.rm=TRUE)
qline = sapply(qline,c)
}
# plot polygons according to qline #
for (j in 1:length(colpal)){polygon(at[c(1:probes,probes:1)],c(qline[j,],qline[j+1,probes:1]),col = colpal[j],lty=0)}
# add lines corresponding to the quartiles #
if (add.quartiles){
if (is.matrix(input)){
drawline(apply(input,2,quantile,probs=0.5,na.rm=TRUE),col=quartiles.col[2],lwd=2)
drawline(apply(input,2,quantile,probs=0.25,na.rm=TRUE),col=quartiles.col[1],lwd=2)
drawline(apply(input,2,quantile,probs=0.75,na.rm=TRUE),col=quartiles.col[3],lwd=2)
} else if (is.list(input)){
drawline(sapply(input,quantile,probs=0.5,na.rm=TRUE),col=quartiles.col[2],lwd=2)
drawline(sapply(input,quantile,probs=0.25,na.rm=TRUE),col=quartiles.col[1],lwd=2)
drawline(sapply(input,quantile,probs=0.75,na.rm=TRUE),col=quartiles.col[3],lwd=2)
}
}
}
# alias #
LSD.singleclusterplot = singleclusterplot
### clusterplot ###
#' @export
#' @name clusterplot
#' @aliases LSD.clusterplot
#' @title Visualize two-dimensional data clusters
#' @description Depict a numeric matrix or list utilizing the underlying distribution quantiles of one dimension in a color encoded fashion.
#' @param input matrix or list with numerical entries.
#' @param label a character vector assigning rows/elements of 'input' to clusters (if specified, multiple clusters can be depicted in different colors and/or subsequent plots).
#' @param at a integer vector containing the x-positions corresponding to the columns of 'input'.
#' @param main title(s) of the plot, standard graphics parameter.
#' @param xlim x limits, standard graphics parameter.
#' @param ylim y limits, standard graphics parameter.
#' @param xlabels a character vector containing labels for the x-axis.
#' @param fromto a numeric vector containing the range of quantiles (between 0 and 1) to be plotted.
#' @param colpal a character vector containing R built-in color names or a name of a \code{LSD} colorpalette as a character string (see disco() or \code{\link{disco}}).
#' @param simulate logical: if \code{TRUE} (\code{FALSE} by default), a converted colorpalette is used to simulate dichromat vision according to \url{http://www.daltonize.org} (see \code{\link{daltonize}}).
#' @param daltonize logical: if \code{TRUE} (\code{FALSE} by default), a converted colorpalette is used to enhance dichromat vision according to \url{http://www.daltonize.org} (see \code{\link{daltonize}}).
#' @param cvd character string implying the type of color vision deficiency ("p" for protanope, "d" for deuteranope or "t" for tritanope).
#' @param nrcol a non-negative integer specifying the number of colors to be used (defaults to 25, if not specified).
#' @param outer.col R built-in color to be used for outlier lines (lines outside of 'fromto').
#' @param quartiles.col a character vector containing three R built-in colors for quartile lines (c('0.25','0.5','0.75')).
#' @param add.quartiles logical: if \code{TRUE} (by default), lines are plotted corresponding to the quartiles.
#' @param separate if \code{TRUE} (by default), different clusters are depicted in subsequent plots.
#' @param rev logical: if \code{TRUE} (\code{FALSE} by default), a reversed colorpalette is used.
#' @param size logical: if \code{TRUE} (by default), the size of each cluster is added to the title of the respective plot.
#' @param alpha alpha value: a two-digit integer between 01 and 99 for color opacity, i.e. appearance of partial or full transparency (usage omitted by default).
#' @param axes logical: if \code{TRUE} (by default), a box and axes are added to the plot (if \code{FALSE}, custom specification of axes can be achieved via basic R graphics functions).
#' @param ... additional parameters to be passed to points and plot.
#' @author Achim Tresch, Bjoern Schwalb
#' @seealso \code{\link{singleclusterplot}}, \code{\link{align}}, \code{\link{demotour}}, \code{\link{disco}}, \code{\link{colorpalette}}
#' @examples samples = 100
#' probes = 75
#' at = 1:probes
#' clus = matrix(rnorm(probes*samples,sd=1),ncol=probes)
#'
#' clus = rbind(
#' t(t(clus)+sin(1:probes/10))+1:nrow(clus)/samples,
#' t(t(clus)+sin(pi/2+1:probes/10))+1:nrow(clus)/samples)
#'
#' quartiles.col = c("transparent","black","transparent")
#' colpal = c("standardheat","crazyblue","crazyred","crazygreen")
#'
#' labs = paste("cluster",kmeans(clus,4)$cluster)
#' clusterplot(clus,fromto=c(0,1))
#'
#' clusterplot(clus,labs,separate=FALSE,xaxt="n",fromto=c(0.4,0.6),colpal=colpal,
#' outer.col="none",ylim=c(-2,3),quartiles.col = quartiles.col)
#'
#' clusterplot(clus,labs,colpal=colpal)
#'
#' labs = paste("cluster",kmeans(clus,2)$cluster)
#' colpal = c("greens","purples")
#' clusterplot(clus,labs,separate=FALSE,xaxt="n",fromto=c(0.3,0.7),colpal=colpal,
#' outer.col="none",ylim=c(-1,2),alpha=50,quartiles.col = quartiles.col)
#' @keywords cluster
clusterplot = function(input,label = NULL,at = NULL,main = NULL,xlim = NULL,ylim = NULL,xlabels = NULL,fromto = c(0.05,0.95),colpal = "standardheat",simulate = FALSE,daltonize = FALSE,cvd = "p",nrcol = 25,outer.col = "lightgrey",quartiles.col = c("grey","black","grey"),add.quartiles = TRUE,separate = TRUE,rev = FALSE,size = TRUE,alpha = NULL,axes = TRUE,...)
{
# stops execution, if 'input' is neither a list nor a matrix and executes an error action #
if (!is.matrix(input) & !is.list(input)) stop("'input' must be a matrix or a list !")
# define x-positions corresponding to the columns of 'input' #
if (is.null(at)) if (is.matrix(input)){at=1:ncol(input)} else if (is.list(input)){at=1:length(input)}
# preliminaries #
probes = length(at)
if (is.null(xlim)){xlim=c(min(at),max(at))}
maxp = xlim[2]
minp = xlim[1]
if (is.null(ylim)) if (is.matrix(input)){ylim=c(min(input,na.rm=TRUE),max(input,na.rm=TRUE))} else if (is.list(input)){ylim=c(min(unlist(input),na.rm=TRUE),max(unlist(input),na.rm=TRUE))}
if (is.null(xlabels)) xlabels = 1:length(at)
# one cluster (i.e. one plot), if label = NULL #
if (is.null(label)){
plot.new()
plot.window(xlim = xlim,ylim = ylim,...)
if (size){
if (is.matrix(input)){
main = paste(main," ( #",nrow(input)," )")
} else if (is.list(input)){
input.length.range = range(as.numeric(summary(input)[,"Length"]))
main = paste(main," ( #",input.length.range[1],"-",input.length.range[2]," )")
}
}
title(main)
if (axes){
axis(1,at=at,labels=xlabels,...)
axis(2)
box()
}
singleclusterplot(input=input,at=at,fromto=fromto,colpal=colpal,simulate=simulate,daltonize=daltonize,cvd=cvd,nrcol=nrcol,outer.col=outer.col,add.quartiles=add.quartiles,quartiles.col=quartiles.col,rev=rev,alpha=alpha)
}
# multiple clusters, if label is specified #
if (!is.null(label)) {
clusternames = sort(unique(label))
nrclusters = length(clusternames)
if (!is.matrix(input)) stop("'input' must be a matrix, if 'label' is specified !")
clustersets = split(1:nrow(input), factor(label))
if (!is.list(colpal)) colpal = as.list(colpal)
if (length(colpal) < nrclusters) colpal = rep(colpal, nrclusters)
# multiple clusters in one plots #
if (separate == FALSE){
plot.new()
plot.window(xlim = xlim,ylim = ylim,...)
if (size){main = paste(main," ( #",nrow(input)," )")}
title(main)
if (axes){
axis(1,at=at,labels=xlabels,...)
axis(2)
box()
}
}
# multiple clusters in subsequent plots #
if (separate == TRUE) par(mfrow = windowxy(nrclusters))
for (j in seq(clusternames)){
if (separate == TRUE){
if (length(main) == length(clustersets[[j]])) clustermain = main[j] else clustermain = paste(main,clusternames[j])
plot.new()
plot.window(xlim = xlim,ylim = ylim,...)
if (size){clustermain = paste(clustermain," ( #",length(clustersets[[j]])," )")}
title(clustermain)
if (axes){
axis(1,at=at,labels=xlabels,...)
axis(2)
box()
}
}
singleclusterplot(input=input[clustersets[[j]],,drop = FALSE],at=at,fromto=fromto,colpal=colpal[[j]],simulate=simulate,daltonize=daltonize,cvd=cvd,nrcol=nrcol,outer.col=outer.col,add.quartiles=add.quartiles,quartiles.col=quartiles.col,rev=rev,alpha=alpha)
}
}
}
# alias #
LSD.clusterplot = clusterplot
|