
Logical queries
R.W. Oldford
September 5, 2021
Source:vignettes/logicalQueries.Rmd
logicalQueries.RmdOne of the principal strengths of linked plots is the ease with which one can form complex logical queries on the data.
The cars of the 1974 Motor Trends magazine
Begin with a classic data set in R –
mtcars.
For the sake of illustration, some enrichment of the variables and their
values will be made:
data(mtcars, package = "datasets")
mtcars$country <- c("Japan", "Japan", "Japan", "USA", "USA", "USA", "USA",
"Germany", "Germany", "Germany", "Germany", "Germany",
"Germany", "Germany", "USA", "USA", "USA", "Italy",
"Japan", "Japan", "Japan", "USA", "USA", "USA", "USA",
"Italy", "Germany", "UK", "USA", "Italy", "italy", "Sweden")
mtcars$continent <- c("Asia", "Asia", "Asia", "North America", "North America",
"North America", "North America", "Europe", "Europe",
"Europe", "Europe", "Europe", "Europe", "Europe",
"North America", "North America", "North America",
"Europe", "Asia", "Asia", "Asia", "North America",
"North America", "North America", "North America",
"Europe", "Europe", "Europe", "North America",
"Europe", "Europe", "Europe" )
mtcars$company <- c("Mazda", "Mazda", "Nissan", "AMC", "AMC", "Chrysler",
"Chrysler", "Mercedes", "Mercedes", "Mercedes", "Mercedes",
"Mercedes", "Mercedes", "Mercedes", "GM", "Ford",
"Chrysler", "Fiat", "Honda", "Toyota", "Toyota", "Chrysler",
"AMC", "GM", "GM", "Fiat", "Porsche", "Lotus", "Ford",
"Ferrari", "Maserati", "Volvo")
mtcars$Engine <- factor(c("V-shaped", "Straight")[mtcars$vs +1],
levels = c("V-shaped", "Straight"))
mtcars$Transmission <- factor(c("automatic", "manual")[mtcars$am +1],
levels = c("automatic", "manual"))
mtcars$vs <- NULL # These are redundant now
mtcars$am <- NULL # For this illustration, it will be convenient to separate categorical from continuous data.
varTypes <- split(names(mtcars),
sapply(mtcars,
FUN = function(x){
if(is.factor(x)|is.character(x)){
"categorical"
} else {"numeric"} } ))varTypes is a list with two named components:
categorical and numeric.
Some interactive plots
To explore the data, several interactive plots will likely have been constructed. Typically, these will have been constructed one at a time and assigned to the same linking group (perhaps via the inspector).
Below, histograms/barplots are constructed for each categorical
variable and assigned to that variable name now prefixed by
h_ for “histogram”.
for (varName in varTypes$categorical) {
assign(paste0("h_", varName),
l_hist(mtcars[ , varName], showFactors = TRUE,
xlabel = varName, title = varName,
linkingGroup = "Motor Trend"))
}These are not evaluated in this vignette. Note that all are in the
same linkingGroup.
Other linked plots might exist as well – for example, a scatterplot
of gear (the number of forward gears) versus
disp (the engine displacement in cubic inches).
p <- with(mtcars, l_plot(disp, cyl,
xlabel = "engine displacement", ylabel = "number of cylinders",
title = "1974 Motor Trend cars",
linkingGroup = "Motor Trend",
size = 10, showScales = TRUE,
itemLabel = rownames(mtcars), showItemLabels = TRUE
))Note that - each car’s name appears as the itemLabel for
that point in the plot (to be revealed as a “tooltip” style pop up), and
that - the plot p is in the same linking group as the
histograms.
Through a combination of selection, inversion, deactivation, and reactivation, logical queries may be made interactively on the data.
For simplicity, the basic logical operators are illustrated below
using only the histograms. More generally, these apply to any
interactive loon graphic.
Interactive logical operations
Five logical conditions/operations illustrated here are the basic ones:
-
AisTRUE - Negation: (NOT
A) isTRUE - Inclusive OR: (
AORB) isTRUE(one or the other or both), - Conjunction: (
AANDB) are bothTRUE - Exclusive OR: (
AXORB) meaning (AisTRUE) or (BisTRUE) but (AANDB) is FALSE
Each of these corresponds to a sequence of actions on the plots and/or inspector. Whatever is highlighted in the end corresponds to the result.
Again, for simplicity all operations are illustrated by interacting with values of categorical variates in the various histograms. Any of the logical elements could also have been that satisfying numerical constraints by undertaking the corresponding actions on a scatterplot (or histogram of continuous values).
Each logical operator is illustrated in turn:
-
A()on the plot select
A,e.g., click on
"manual"bar from theTransmissionhistogramhighlighted
Transmission == "manual"isTRUE
-
NOT
A( or )on a plot select
A,from the inspector click
invert-
e.g., click on
"North America"bar from thecontinenthistogram,then invert
highlighted
continent == "North America"isFALSEall that is highlighted is not
"North America", namely"Asia"or"Europe"
-
-
AORB( or ),on a plot select
A,on the same (or a different but linked) plot
<SHIFT>- selectB-
e.g., click on
"manual"bar fromTransmissionhistogram,then while holding down the
<SHIFT>key,click on the
Mercedesbar in thecompanyhistogram highlighted
Transmission == "manual"isTRUEORcompany = "Mercedes"isTRUE(or both)
-
-
AANDB( or )lots of solutions, here is one that always works
on a plot select
A,from the inspector,
invertthendeactivate(onlyAremains),from a plot of the remaining select
B,from the inspector
reactivateallelements are highlighted
e.g. try highlighting all European cars with manual transmissions.
-
AXORB( or )following steps in 4, select
AANDB,from the inspector
invertthendeactivate(only remains)following steps in 3, select
AORB,from the inspector
reactivate(onlyAXORBis highlighted)
Other logical conditions (including numerical ones such as
disp > 300 on the scatterplot p) are
constructed as a combination of the above (as in exclusive or).
These can be quite complex and it may help, after some number of steps, to mark intermediary results by colour (or also glyph in scatterplots).
Note that because of possibly missing data, not all linked plots may share the same set of observations.
Missing data and linking keys
The mtcars data is an example of a complete data set.
Had there been missing values, then these would not appear in loon plots
that require them.
For example, suppose data has four variables
A, B, C, and D,
and
data <- data.frame(A = sample(c(rnorm(10), NA), 10, replace = FALSE),
B = sample(c(rnorm(10), NA), 10, replace = FALSE),
C = sample(c("firebrick", "steelblue", NA), 10, replace = TRUE),
D = sample(c(1:10, NA), 10, replace = FALSE))
p_test <- l_plot(x = data$A, y = data$B, color = data$C, linkingGroup = "test missing")
h_test <- l_hist(x = data$D, color = data$C, linkingGroup = "test missing")Then
-
wherever an
NAappears in any ofA,B, orC, that point will be missing fromp_testNote that it is generally not a good idea to use
Cfor any simple display characteristic likecolorif indeedChas missing values since this will remove non-missingxandyvalues from the plot. Not all values ofxandywould then be accessible from the plot for logical queries, wherever an
NAappears in either ofCorD, that point will be missing fromh_test
Using logical operations on the original data to change
plot properties (e.g. select values) can be challenging when data values
are missing in the plot (since what is missing depends on what was
missing at the time of its construction).
For example,
p_test["selected"] <- (data$A > 0)may not work!
The logical operation on the data (
data$A > 0) will typically be longer than the corresponding x valuep_test["x"]in the plot and so will not work.Even if the logical vector is of the right length (and contains no
NAs itself), the values may not correctly match the data points.
There are two general approaches to logical queries
when data contains NAs.
-
Using complete data
If, like
mtcars, the data being used contains noNAs then conducting logical queries on the plot will be identical to conducting them on the data.If the data is not complete (contains one or more
NA), it can be made complete by removing all observations (rows) that contain anNA. E.g. replacingdatabyc_data <- na.omit(data).any logic on
c_datawill match that on plots made fromc_data.depending on the amount and pattern of missing data, this could critically reduce the amount of data in the analysis.
-
Using the information in the loon plots. Of course, this is the recommended approach when data is missing.
Logical queries can then be made
-
directly on the plots, either
- interactively as described in the previous sections, or,
- programmatically as in
p_test["x"] > 0in place ofdata$A > 0.
or
-
directly on the data and applied to the plots
To help manage this, the
linkingKeyof each plot can be used.-
the default value for each plot is a character vector with entries
from
"0"to"n-1"wheren =nrow(data)`.These are easily turned into the row numbers for the original data.
E.g. in
p_testthe row numbers ofdatathat correspond to the points is1 + as.numeric(p_test["linkingKey"])Logical values for the rows of
datacan then select points inpas followsLogVal <- data$A > data$B p["selected"] <- logVal[1 + as.numeric(p_test["linkingKey"])]Similarly for
h_test. E.g., comparep_test["linkingKey"]andh_test["linkingKey]".
-
-
Note: the user can always provide their own character vector
linkingKeyfor their plots.- E.g.,
linkingKey = rownames(data)
If so, then more care may be needed to use these to identify rows in a logical vector.
- E.g.,
-
loon’s linking model
Loon’s linking model has the following three parts
- a
linkingGroupwhich identifies which plots are linked - a
linkingKey, a character vector where each element is a key uniquely identifying a single observation in the plot (no two observations in the same plot can have the same value in the linking key), and - the linked display states each plot has declared (e.g. see
l_getlinkedStates()).
Observations in different plots (in the same linking group) are linked (in that their linked states change together) if and only if they have the same linking key.
Points appearing in different plots (in the same
linkingGroup) which matched on the value of their
linkingKey will share the same value for their linked
states.