Generic shell function that calls a plotting function in order to produce a marginal distribution plot for a variable (or for each variable in a dataset). What type of plot is made might depend on the data class of the variable.
visualize(v, vnam = NULL, visuals = setVisuals(), doEval = TRUE, ...)
| v | The variable (vector) or dataset (data.frame) which is to be plotted. |
|---|---|
| vnam | The name of the variable. This name might be printed on the plots, depending on the
choice of plotting function. If not supplied, it will default to the name of |
| visuals | A list of visual functions to use on each supported variable type. We recommend
using |
| doEval | A logical. If |
| ... | Additional arguments used for class-specific choices of visual functions (see details). |
Visual functions can be supplied using their names (in character strings) using
setVisuals. Note that only a single visual function is allowed for each variable class.
The default visual settings can be inspected by calling setVisuals().
An overview of all available visualFunctions can be obtained by calling
allVisualFunctions.
A user defined visual function can be supplied using its function name. Details on how
to construct valid visual functions are found in visualFunction.
#Standard use: Return standalone code for plotting a function: visualize(c(1:10), "Variable 1", doEval = FALSE)#> [1] "ggAggHist(data = structure(list(factorV = structure(1:20, .Label = c(\"[1,1.45]\", " #> [2] "\"(1.45,1.9]\", \"(1.9,2.35]\", \"(2.35,2.8]\", \"(2.8,3.25]\", \"(3.25,3.7]\", " #> [3] "\"(3.7,4.15]\", \"(4.15,4.6]\", \"(4.6,5.05]\", \"(5.05,5.5]\", \"(5.5,5.95]\", " #> [4] "\"(5.95,6.4]\", \"(6.4,6.85]\", \"(6.85,7.3]\", \"(7.3,7.75]\", \"(7.75,8.2]\", " #> [5] "\"(8.2,8.65]\", \"(8.65,9.1]\", \"(9.1,9.55]\", \"(9.55,10]\"), class = \"factor\"), " #> [6] " Freq = c(1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, " #> [7] " 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), xmin = c(1, 1.45, 1.9, 2.35, " #> [8] " 2.8, 3.25, 3.7, 4.15, 4.6, 5.05, 5.5, 5.95, 6.4, 6.85, 7.3, " #> [9] " 7.75, 8.2, 8.65, 9.1, 9.55), xmax = c(1.45, 1.9, 2.35, 2.8, " #> [10] " 3.25, 3.7, 4.15, 4.6, 5.05, 5.5, 5.95, 6.4, 6.85, 7.3, 7.75, " #> [11] " 8.2, 8.65, 9.1, 9.55, 10), ymin = c(0, 0, 0, 0, 0, 0, 0, " #> [12] " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), ymax = c(1L, 0L, " #> [13] " 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, " #> [14] " 1L, 0L, 1L)), row.names = c(NA, -20L), class = \"data.frame\"), " #> [15] " vnam = \"Variable 1\")"#Define a new visualization function and call it using visualize either #using allVisual or a class specific argument: mosaicVisual <- function(v, vnam, doEval) { thisCall <- call("mosaicplot", table(v), main = vnam, xlab = "") if (doEval) { return(eval(thisCall)) } else return(deparse(thisCall)) } mosaicVisual <- visualFunction(mosaicVisual, description = "Mosaicplots from graphics", classes = allClasses())#> Error in get(fName): object 'mosaicVisual' not found#Inspect all options for visualFunctions: allVisualFunctions()#> #> ------------------------------------------------------------------------------ #> name description classes #> ---------------- ------------------------------- ----------------------------- #> basicVisual Histograms and barplots using character, Date, factor, #> graphics integer, labelled, logical, #> numeric #> #> standardVisual Histograms and barplots using character, Date, factor, #> ggplot2 integer, labelled, logical, #> numeric #> ------------------------------------------------------------------------------ #># NOT RUN { #set mosaicVisual for all variable types: visualize(c("1", "1", "1", "2", "2", "a"), "My variable", visuals = setVisuals(all = "mosaicVisual")) #set mosaicVisual only for character variables: visualize(c("1", "1", "1", "2", "2", "a"), "My variable", visuals = setVisuals(character = "mosaicVisual")) #this will use standardVisual, as our variable is not numeric: visualize(c("1", "1", "1", "2", "2", "a"), "My variable", visuals = setVisuals(numeric = "mosaicVisual")) # }#return code for a mosaic plot visualize(c("1", "1", "1", "2", "2", "a"), "My variable", allVisuals = "mosaicVisual", doEval=FALSE)#> [1] "ggAggBarplot(data = structure(list(x = structure(1:3, .Label = c(\"1\", " #> [2] "\"2\", \"a\"), class = \"factor\"), y = 3:1), class = \"data.frame\", row.names = c(NA, " #> [3] "-3L)), vnam = \"My variable\")"# NOT RUN { #Produce multiple plots easily by calling visualize on a full dataset: data(testData) testData2 <- testData[, c("charVar", "factorVar", "numVar", "intVar")] visualize(testData2) #When using visualize on a dataset, datatype specific arguments have no #influence: visualize(testData2, setVisuals(character = "basicVisual", factor = "basicVisual")) #But we can still use the "all" argument in setVisuals: visualize(testData2, visuals = setVisuals(all = "basicVisual")) # }