##' An S4 class to hold text and cartesian coordinates for plotting
##'
##' A class designed to hold the data required to create a textplot
##' where character strings are plotted based on x and y coordinates.
##'
##' @slot x A numeric value with the x axis coordinates.
##' @slot y A numeric value with the y axis coordinates.
##' @slot labels A character string with the text to be plotted
##' @import methods
textplot <- setClass(
Class = "textplot",
slots = c(
x = "numeric",
y = "numeric",
labels = "character"),
prototype = list(
x = numeric(0),
y = numeric(0),
labels = character(0)),
validity = function(object) {
errors <- character()
if (length(object@x) != length(object@y)) {
errors <- c(errors,
sprintf("x (length %d) and y (length %d) are not equal",
length(object@x), length(object@y)))
}
if (length(object@x) != length(object@labels)) {
errors <- c(errors,
sprintf("x (length %d) and labels (length %d) are not equal",
length(object@x), length(object@labels)))
}
if (!all(nchar(object@labels) > 0, na.rm = TRUE)) {
errors <- c(errors, sprintf(
"%d label(s) are zero length. All labels must be missing or non zero length",
sum(nchar(object@labels) == 0, na.rm = TRUE)))
}
if (length(errors)) {
stop(paste(c("\n", errors), collapse = "\n"))
} else {
return(TRUE)
}
}
)
##' @describeIn textplot show method
##'
##' @param object The object to be shown
##' @importFrom utils head
setMethod(
f = "show",
signature = "textplot",
definition = function(object) {
cat(" X: ")
cat(head(object@x, 5), fill = TRUE)
cat(" Y: ")
cat(head(object@y, 5), fill = TRUE)
cat("Labels: ")
cat(head(object@labels, 5), fill = TRUE)
})
##' @describeIn textplot extract method
##'
##' @param x the object to subset
##' @param i the rows to subset (optional)
##' @param j the columns to subset (optional)
##' @param drop should be missing
##' @export
##' @aliases [,textplot-method
setMethod(
f = "[",
signature = "textplot",
definition = function(x, i, j, drop) {
if (missing(i) & missing(j)) {
out <- x
validObject(out)
} else if (!missing(i) & missing(j)) {
out <- textplot(
x = x@x[i],
y = x@y[i],
labels = x@labels[i])
validObject(out)
} else if (!missing(j)) {
if (missing(i)) {
i <- seq_along(x@x)
}
if (is.character(j)) {
out <- lapply(j, function(n) {
slot(x, n)[i]
})
names(out) <- j
} else if (is.numeric(j)) {
n <- slotNames(x)
out <- lapply(j, function(k) {
slot(x, n[j])[i]
})
names(out) <- n[j]
} else {
stop("j is not a valid type")
}
}
return(out)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.