Description Usage Arguments Examples
View source: R/category.plot.R
Plots a seto of categorical data, allowing for the individual data to be seeing as well as mean and error bars.
TODO: better doucmentation.
1 | category.plot(formula, data = NULL, category.names = NULL, label.width = 20, control = NULL, control.column = 1, label.height = 0, show.individual.data = TRUE, label.categories = NULL, type = c("points", "hist"), height.scale = 1, arrow.length = 0.05, hist.breaks = "Sturges", new.plot = TRUE, p.values = NULL, ...)
|
formula |
|
data |
|
category.names |
|
label.width |
|
control |
|
control.column |
|
label.height |
|
show.individual.data |
|
label.categories |
|
type |
|
height.scale |
|
arrow.length |
|
hist.breaks |
|
new.plot |
|
p.values |
|
... |
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 | ##---- Should be DIRECTLY executable !! ----
##-- ==> Define data, use random,
##-- or do help(data=index) for the standard data sets.
## The function is currently defined as
function (formula, data = NULL, category.names = NULL, label.width = 20,
control = NULL, control.column = 1, label.height = 0, show.individual.data = TRUE,
label.categories = NULL, type = c("points", "hist"), height.scale = 1,
arrow.length = 0.05, hist.breaks = "Sturges", new.plot = TRUE,
p.values = NULL, ...)
{
dat = model.frame(formula, data)
stopifnot(ncol(dat) >= 2)
categories <- sprintf("category%d", 1:(ncol(dat) - 1))
colnames(dat) <- c("y", categories)
for (col in categories) {
stopifnot(is.factor(dat[, col]))
dat[, col] = dat[, col][, drop = TRUE]
}
num.categories <- prod(unlist(lapply(lapply(dat[, 2:ncol(dat)],
levels), length)))
dat$x = 0
cat.factor = 1
for (col in categories) {
cat.factor = cat.factor * (length(levels(dat[, col])) +
if (cat.factor == 1)
-0.75
else 1)
dat$x = dat$x + (as.numeric(dat[, col]) - if (show.individual.data)
1
else 0.75)/cat.factor
}
if (length(categories) > 1) {
means <- aggregate(dat[, c("x", "y")], by = as.list(dat[,
categories]), mean)
ses <- aggregate(dat$y, by = as.list(dat[, categories]),
std.error)
}
else {
means <- aggregate(dat[, c("x", "y")], by = list(dat[,
categories]), mean)
ses <- aggregate(dat$y, by = list(dat[, categories]),
std.error)
names(means)[1] = categories[1]
names(ses)[1] = categories[1]
}
names(ses)[names(ses) == "x"] = "se"
means = merge(ses, means)
means = means[order(means$x), ]
if (length(label.categories) == 0)
label.categories = rep(TRUE, length(categories))
n.offsets = sum(label.categories)
args = list(...)
saved.xlab = all.vars(formula)[2]
if (!hasArg(ylim))
args$ylim <- range(dat$y)
if (!hasArg(xlim))
args$xlim <- c(0, 1.15)
if (hasArg(xlab))
saved.xlab = args$xlab
if (!hasArg(ylab))
args$ylab = all.vars(formula)[1]
if (!hasArg(mar))
args$mar = c(5 + 3 * (n.offsets - 1), 4, 4, 2)
if (hasArg(x.mgp)) {
saved.x.mgp = args$x.mgp
args$x.mgp <- NULL
}
else saved.x.mgp <- par()$mgp
args$x <- means$x
args$y <- means$y
args$xlab = ""
args$xaxt <- "n"
args$type <- "n"
if (new.plot)
do.call("plot", args)
if (length(control) > 0) {
plot.confidence.box(dat[dat[, 1 + control.column] ==
control, ]$y, 0.01, 1.08)
}
if (type[1] == "points") {
arrows(means$x, means$y + means$se, means$x, means$y -
means$se, angle = 90, code = 3, length = arrow.length,
col = if (hasArg(col))
args$col
else "black")
points(means$x, means$y, cex = 2 * (if (hasArg(cex))
args$cex
else 1), pch = if (hasArg(pch))
args$pch
else 22, col = if (hasArg(col))
args$col
else "black", bg = if (hasArg(bg))
args$bg
else "black")
dat$deviate = 0
for (i in 1:nrow(means)) {
same.categories = Reduce(function(x, y) {
x & y
}, lapply(categories, function(cat) {
means[i, cat] == dat[, cat]
}))
indices = which(same.categories)
indices = indices[order(dat[indices, ]$y)]
indices = c(indices[seq(1, length(indices), 2)],
rev(indices[seq(2, length(indices), 2)]))
dat[indices, ]$deviate = seq(-0.5, 0.5, length.out = nrow(dat[indices,
])) + rep(c(-1, -1/3, 1/3, 1), length.out = nrow(dat[indices,
]))
}
width = 1/cat.factor
if (show.individual.data) {
points(dat$x + width/2 + 0.1 * width * dat$deviate,
dat$y, pch = 23, cex = 0.5, col = "black", bg = "black")
}
if (length(p.values) > 0) {
sig.points.x <- c()
sig.points.y <- c()
near.sig.points.x <- c()
near.sig.points.y <- c()
condition.index = 1
for (p.value in p.values) {
if (p.value < 0.05) {
sig.points.x = c(sig.points.x, means$x[condition.index])
if (!any(means[, control.column] == control) ||
means$y[condition.index] < means[means[,
control.column] == control, ]$y)
sig.points.y = c(sig.points.y, means$y[condition.index] -
means$se[condition.index] * 2)
else sig.points.y = c(sig.points.y, means$y[condition.index] +
means$se[condition.index] * 2)
}
else if (p.value < 0.06) {
near.sig.points.x = c(near.sig.points.x, means$x[condition.index])
if (!any(means[, control.column] == control) ||
means$y[condition.index] < means[means[,
control.column] == control, ]$y)
near.sig.points.y = c(near.sig.points.y,
means$y[condition.index] - means$se[condition.index] *
2)
else near.sig.points.y = c(near.sig.points.y,
means$y[condition.index] + means$se[condition.index] *
2)
}
condition.index = condition.index + 1
}
points(sig.points.x, sig.points.y, pch = "*", cex = 2,
col = "blue", bg = "blue")
points(near.sig.points.x, near.sig.points.y, pch = 16,
cex = 0.8, col = "blue", bg = "blue")
}
}
if (type[1] == "hist") {
if (length(hist.breaks) == 1 && typeof(hist.breaks) ==
"double")
hist.breaks = pretty(dat$y, hist.breaks)
for (row in 1:nrow(means)) {
same.categories = Reduce(function(x, y) {
x & y
}, lapply(categories, function(cat) {
means[row, cat] == dat[, cat]
}))
heights <- hist(dat[same.categories, ]$y, breaks = hist.breaks,
plot = FALSE)
heights$scaled = heights$density/max(heights$density)
for (i in 1:length(heights$counts)) {
x = means$x[row]
width = 1/cat.factor
polygon(x = c(x, x + heights$scaled[i] * width *
height.scale, x + heights$scaled[i] * width *
height.scale, x), y = c(heights$breaks[i],
heights$breaks[i], heights$breaks[i + 1], heights$breaks[i +
1]), col = "gray", border = "black")
}
}
}
col.labels = list()
index = 0
for (col in categories) {
index = index + 1
cats = levels(dat[, col])
col.labels[[col]] = if (label.categories[index] && length(category.names[[index]]) >
0) {
break.labels(category.names[[index]][cats], label.width)
}
else {
temp <- cats
names(temp) <- cats
temp
}
}
cat.factor = 1
index = -1
all.ats = rep(0, nrow(means))
n.repeat = 1
n.cats = length(categories)
offset = -1
for (col in categories) {
index = index + 1
n.levels = length(levels(means[, col]))
cat.factor = cat.factor * (n.levels + if (cat.factor ==
1)
-0.75
else 1)
all.ats = all.ats + (as.numeric(means[, col]) - 1)/cat.factor
if (label.categories[index + 1]) {
ats = sort(unique(all.ats) + if (index + 1 < n.cats)
0.25/cat.factor
else 0.25/cat.factor)
axis(1, at = ats, labels = rep(col.labels[[col]][levels(dat[,
col])], n.repeat), mgp = c(0, -0.5 + label.height *
(n.offsets - index), -1 + label.height * (n.offsets -
index)), tick = FALSE, lty = 0, padj = 1)
}
n.repeat = n.repeat * n.levels
}
means
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.