Variables are the rows of data and the variable names are drawn from the row names. It is helpful to keep in mind that there are a number of manipulations and functions in R that remove or reset rownames on various data structures.
Samples are the columns of data and the sample names are drawn from the column names.
Annotations are considered to be extra information or characteristics. These data add to the information about samples or variables but are not a part of the main dataset.
The JSON format for the data is essentially a list of lists. From a data perspective the y list (compartment) is where the numerical data resides with three sub-lists - the names of the variables, the names of the samples, and the actual data. The x list contains the sample annotation data, and the z list contains the variable annotation data.
When utilizing the canvasXpress functions from R the following mappings are made, which covers the most common charts. There are additional named lists and properties that are mapped for specific chart types and covered with those chart examples below.
data -> y
y.vars = row names
y.smps = column names
y.data = values
smpAnnot -> x
varAnnot -> z
Examples here use data manipulated with the tidyverse related packages (dplyr, tibble, etc). This is just one way to manipulate data into the correct format to plot in CanvasXpress.
A variety of commonly-used canvasXpress options are used below to provide examples of how to position, resize and configure various aspects of the charts from the call to the CanvasXpress function in R. This includes items such as the Axis Titles, Legends, Colors, etc. All of these optional parameters are documented on the main CanvasXpress site at https://www.canvasxpress.org.
cxdata <- data %>% select(Murder, Assault) cxdata.varAnnot <- data %>% select(UrbanPop, Category) rownames(cxdata) <- data[, "State"] rownames(cxdata.varAnnot) <- data[, "State"] canvasXpress(data = cxdata, varAnnot = cxdata.varAnnot, graphType = "Scatter2D", colorBy = "UrbanPop", shapeBy = "Category", legendPosition = "right", legendOrder = list("Category" = list("low", "med", "high")), title = "Murder vs Assault Rates", xAxisMinorTicks = FALSE, yAxisMinorTicks = FALSE)
cxdata <- t(data %>% select(Assault, Rape, Murder)) colnames(cxdata) <- data$State canvasXpress(data = cxdata, graphType = "Stacked", colorScheme = "Blues", graphOrientation = "vertical", legendInside = TRUE, legendPosition = "topRight", legendBackgroundColor = 'white', smpLabelRotate = 20, title = "US Arrests by State and Type", xAxisTitle = "Total Arrests", xAxis2Title = "Total Arrests")
cxdata <- t(data %>% select(Assault, Rape, Murder)) colnames(cxdata) <- data$State canvasXpress(data = cxdata, graphType = "Stacked", graphOrientation = "horizontal", colorScheme = "Reds", showSampleNames = FALSE, title = "Clustered Arrests", subtitle = "(by State and Type)", titleScaleFontFactor = 0.6, subtitleScaleFontFactor = 0.4, xAxisShow = FALSE, xAxis2Title = "Total Arrests", legendPosition = "bottom", legendColumns = 3, sampleSpaceFactor = 0.5, #canvasXpress clustering options samplesClustered = TRUE, linkage = "single", distance = "manhattan", smpDendrogramPosition = "left")
reshape <- data %>% gather(key = "Type", value = "Rate", Assault, Rape, Murder) cxdata <- t(reshape %>% select(Rate)) cxdata.smpAnnot <- t(reshape %>% select(Type)) colnames(cxdata.smpAnnot) <- colnames(cxdata) canvasXpress(data = cxdata, smpAnnot = cxdata.smpAnnot, graphType = "Boxplot", colorScheme = "Pastel", colorBy = "Type", graphOrientation = "vertical", groupingFactors = list("Type"), smpLabelFontStyle = "italic", smpLabelRotate = 90, showLegend = FALSE, title = "US Arrests by Type")
Additional information and many examples with R code for the canvasXpress library can be found at https://www.canvasxpress.org.