1dotPlot = function(x, group, xlim, ylim, col, xlab, ylab, pch, cex, breaks, stacked = TRUE, ...) { 2 DB = FALSE 3 pch.size = "O" 4 grouped = TRUE 5 parList = list(...) 6 if (missing(xlab)) 7 xlab = deparse(substitute(x)) 8 x = x[!is.na(x)] 9 if (missing(xlim)) 10 xlim = range(x) 11 if (missing(ylim)) 12 ylim = c(0, 1) 13 if (missing(ylab)) 14 ylab = "" 15 if (missing(cex)) 16 cex = 1 17 if (missing(group)) 18 group = rep(1, length(x)) 19 if (length(unique(group)) == 1) 20 grouped = FALSE 21 if (missing(pch) || length(unique(group)) > length(pch)) 22 pch = 1:length(unique(group)) 23 if (missing(col) || length(unique(group)) > length(col)) 24 col = 1:length(unique(group)) 25 if (missing(breaks)) { 26 plot(1, 1, xlim = xlim, ylim = ylim, type = "n", axes = FALSE, cex = cex, xlab = xlab, ylab = ylab) 27 slotSizeX = strwidth(pch.size, units = "user", cex = cex) 28 if (DB) 29 print(paste("slotSizeX:", slotSizeX)) 30 span = diff(range(x)) 31 temp1 = ppoints(2 * ceiling(span/slotSizeX)) 32 temp2 = numeric(length(temp1) + 2) 33 temp2[2:(length(temp1) + 1)] = temp1 34 temp2[1] = temp2[1] - 1.01 * diff(c(temp1[1], temp1[2])) 35 temp2[length(temp2)] = rev(temp1)[1] + 1.01 * diff(c(temp1[1], temp1[2])) 36 temp2 = temp2 * span + min(x) 37 temp = min(x) + ppoints(span/slotSizeX) * span 38 breaks = numeric(length(temp) + 2) 39 breaks[2:(length(temp) + 1)] = temp 40 breaks[1] = temp[1] - diff(c(temp[1], temp[2])) * 1.001 41 breaks[length(breaks)] = rev(temp)[1] + diff(c(temp[1], temp[2])) * 1.001 42 breaks = temp2 43 } 44 slotSizeY = strheight(pch.size, units = "user", cex = cex) 45 if (DB) 46 print(paste("slotSizeY:", slotSizeY)) 47 span = diff(ylim) 48 temp1 = ppoints(2 * ceiling(span/slotSizeY)) 49 temp2 = numeric(length(temp1) + 2) 50 temp2[2:(length(temp1) + 1)] = temp1 51 temp2[1] = temp2[1] - 1.01 * diff(c(temp1[1], temp1[2])) 52 temp2[length(temp2)] = rev(temp1)[1] + 1.01 * diff(c(temp1[1], temp1[2])) 53 yVec = temp2 * span + min(ylim) 54 if (yVec[1] < 0) 55 yVec = yVec + abs(yVec[1]) 56 else yVec = yVec - yVec[1] 57 if (DB) 58 print(paste("temp2:", temp2)) 59 if (DB) 60 print(paste("breaks:", breaks)) 61 histObj = hist(x, breaks = breaks, right = FALSE, plot = FALSE) 62 hMids = histObj$mids 63 hCounts = histObj$counts 64 hMids = histObj$mids 65 mat = matrix(NA, nrow = length(x), ncol = length(hMids)) 66 colMat = mat 67 groupmat = mat 68 numVec = 1:nrow(mat) 69 cutOff = 1 70 groupList = vector(mode = "list", length = length(unique(group))) 71 for (k in unique(group)) { 72 histObj = hist(x[group == k], breaks = breaks, plot = FALSE) 73 hMids = histObj$mids 74 hCounts = histObj$counts 75 hMids = histObj$mids 76 for (i in seq(along = hMids)) { 77 value = pch[k] 78 colValue = col[k] 79 from = 0 80 from = numVec[is.na(mat[, i])][1] 81 to = from 82 if (hCounts[i] == 0) 83 value = NA 84 if (hCounts[i] >= 1) 85 to = to + hCounts[i] - 1 86 if (to > cutOff) 87 cutOff = to 88 if (DB) { 89 print(paste("from:", from)) 90 print(paste("to:", to)) 91 print(paste("i:", i)) 92 print(paste("value:", value)) 93 } 94 mat[from:to, i] = value 95 colMat[from:to, i] = colValue 96 } 97 groupList[[k]] = groupmat 98 } 99 if (grouped && !stacked) { 100 groupIndex = unique(group) 101 par(mfrow = c(length(groupIndex), 1)) 102 for (i in groupIndex) dotPlot(x[group == i], xlim = xlim, breaks = breaks, cex = cex, xlab = xlab, ylab = ylab, col = col, pch = pch, ...) 103 } 104 else { 105 mat = mat[1:cutOff, ] 106 if (!is.matrix(mat)) 107 mat = matrix(mat, nrow = 1) 108 if (DB) 109 print(mat) 110 plot(1, 1, xlim = xlim, ylim = ylim, type = "n", cex = cex, xlab = xlab, ylab = ylab, ...) 111 for (i in 1:nrow(mat)) { 112 x = hMids[!is.na(mat[i, ])] 113 y = rep(i * 0.3, times = length(x)) 114 y = rep(yVec[i], times = length(x)) 115 col = colMat[i, !is.na(mat[i, ])] 116 pch = mat[i, !is.na(mat[i, ])] 117 points(x, y, col = col, pch = pch, cex = cex) 118 } 119 } 120 if (DB) 121 print(hMids) 122 invisible(mat) 123} 124