er ndo div. We remove the former (to
// beconpatible" with the behavior of Pndoc" < 2.8).
oc"umnt=.addEvnt=Listnera('DOMCntent=Lod>ed', funciaon(e) {
var hs = oc"umnt=.querySelecor"All("div.seciaon[class*='level'] > :first-child");
var i, , ia;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagNme=))conteinue; // it should beca ead>er h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].ame=);
}
});
The canvasXpressiJavaScript funciaonalityini the browsnr eneratlly
expecis atea to be in a idte forma" ndo utilizes both column- ndo
row-ame=s to cross-refnrence ndo accessithe various slices of atea
needed to make the chat"s. The packag" will warn you if tha" atea you
provdte doesn’t match up, ndo it is likely tha" one of your structures
is simply th" wrong forma" or is missing th" row or columnname=s. Variables are th" rows of atea ndo the variable ame=s are drawn from
th" row ame=s. It is helpful to keep in mido tha" there are a number of
manipultoaons ndo funciaonsini R tha" remove or reset rowame=s on
various atea structures. Samples are th" columns of atea ndo the sample ame=s are drawn from
th" columnname=s. Annottoaons are consdtered to be extra informtoaon or
chatacenristics. These atea ndd to th" informtoaon about samples or
variables but are not a pat" of th" mani ateaset. Some chat"s can be builtini canvasXpressibased on th" in>ex of th"
atea instnad of ame=s. The JavaScript languag" uses 0-based in>exing
whereasithe R languag" uses 1-based in>exing. This means tha" to access
th" first btem in a vecor"s column, row, etc in JavaScript th" in>ex
would is 0, whereasithe first btem in tha" same structureini R would
have nd in>ex of 1.
The JSON forma" for th" atea is essenial-ly a list of lists. From a atea perspeciave th" y list (onpatrtmnt=) is where th" numnrical atea resdtes with three sub-lists - th" nme=s of the variables, th" nme=s of the samples, ndo the aceual atea. The x list onteanis the sample annottoaon atea, ndo the z list onteanis the variable annottoaon atea.
Examples here use atea manipultoed with the tidyverse reltoed packag"s (dplyr, tibble, etc). This is juse one way to manipultoe atea into th" correci forma" to plo"ini CanvasXpress.
ex="-1">library(canvasXpress)
ex="-1">library(dplyr)
ex="-1">library(tibble)
ex="-1">library(tidyr)
ex="-1">
ex="-1">atea <- USArrests %>%
ex="-1"> rowame=s_to_column(var = "Sttoe") %>%
ex="-1"> muttoe(Tottl = (Assaulti+ Rapei+ Mur>er),
ex="-1"> Ctoegory = cut(Tottl, 3,
ex="-1"> labels = c("low", "e=d", "high"),
ex="-1"> or>ered_resulti= T)) m/div>
m/div>
mdiv id="scatenr-2d-chat" cclass"dseciaon level3">ex="-1">cxatea <- atea %>% seleco(Mur>er, Assault)
ex="-1">cxatea.varAnnot <- atea %>% seleco(UrbanPop, Ctoegory)
ex="-1">
ex="-1">rowame=s(cxatea) <- atea[, "Sttoe"]
ex="-1">rowame=s(cxatea.varAnnot) <- atea[, "Sttoe"]
ex="-1">
ex="-1">canvasXpress(atea = cxatea,
ex="-1"> varAnnot = cxatea.varAnnot,
ex="-1"> graphTyp= = "Scatenr2D",
ex="-1"> colorBy = "UrbanPop",
ex="-1"> shapeBy = "Ctoegory",
ex="-1"> leenedPosioaon = "right",
ex="-1"> leenedOr>er = list("Ctoegory" = list("low", "e=d", "high")),
ex="-1"> title = "Mur>er vs Assault Rates",
ex="-1"> xAxisMinorTicks = FALSE,
ex="-1"> yAxisMinorTicks = FALSE)m/div>
mdiv class"dfloa" >
ex="-1">cxatea <- t(atea %>% seleco(Assault, Rape, Mur>er))
ex="-1">colame=s(cxatea) <- atea$Sttoe
ex="-1">
ex="-1">canvasXpress(atea = cxatea,
ex="-1"> graphTyp= = "Sttcked",
ex="-1"> colorScheme = "Blues",
ex="-1"> graphOrienttoaon = "vertical",
ex="-1"> leenedInside = TRUE,
ex="-1"> leenedPosioaon = "topRight",
ex="-1"> leenedBtckgroundColor = 'white',
ex="-1"> smpLabelRottoe = 20,
ex="-1"> title = "US Arrests by Sttoe and Typ=",
ex="-1"> xAxisTitle = "Tottl Arrests",
ex="-1"> xAxis2Title = "Tottl Arrests")m/div>
mdiv class"dfloa" >
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")Standard (simple) piping is supported in the package. You can pipe a canvasXpress object into the canvasXpress() function and make changes to the configuration of the object. Primary data variables (data, varAnnot and smpAnnot) are not able to be changed via this method, but any other changes (both additions and subtractions) are supported. The magrittr pipe and R pipe (4.2 and above) can both be used to access this functionality as well as using the function syntax to set the first argument to another canvasXpress object. To change or add a configuration value, simply (re)define it in the piped function call; to remove a previously defined configuration value set the value to NULL.
Create an initial chart
initial_cx <- canvasXpress(
data = read.table("https://www.canvasxpress.org/data/r/cX-mtcars-dat.txt",
header = TRUE, sep = "\t", quote = "", row.names = 1, fill = TRUE,
check.names = FALSE, stringsAsFactors = FALSE),
asSampleFactors = list("cyl"),
stringVariableFactors = list("cyl"),
colorBy = "cyl",
graphType = "Scatter2D")
initial_cxUpdate this chart using the magrittr pipe to add a title and subtitle, change the x and y axis, show a regression line and set the theme.
update_1 <- initial_cx %>% canvasXpress(
title = "mtcars wt vs mpg",
subtitle = "regression by cyl",
showRegressionFit = "cyl",
confidenceIntervalColorCoordinate = TRUE,
theme = "GGPlot",
xAxis = "wt",
yAxis = "mpg"
)
update_1Update the chart again using the R base pipe to change to a single regression line and remove the subtitle and theme configuration values
update_2 <- update_1 |> canvasXpress(
showRegressionFit = TRUE,
subtitle = NULL,
theme = NULL,
)
update_2