Summary
How to create plots that get filtered by the variables not participating in the plot.
Details,
- I have an input data with 3 variables and one prediction,
- I want to show the conditional trend lines in
predvsvar_xplots,
Till now,
- I have made 3 plots -
p_x(one for each var_x) and 3 filters -f_x, - Each plot is affected by two filters, eg plot
p1gets filters fromf2+f3, - I don't want the
p1to get affected byf1because it's not anymore a trendline, - Similar, each plot should get filtered only by the variables not participating in the plot.
Example:
---
title: "Untitled"
format: html
editor: visual
---
```{r, eval = T,echo=F, message=F}
# Libraries
library(plotly)
library(crosstalk)
library(tidyverse)
```
```{r input-prep, eval=T, echo=F, warning=F, message=F, fig.height=4, fig.width=5}
## Data to model
inputData <- mtcars %>%
select(var1 = vs, var2 = am, var3 = hp, mpg)
## The model
lm1 <- lm(mpg ~ var1 + var2 + var3,
data = inputData)
## The grid of different input variable combinations(to be used for predicitons)
graph_data <- expand_grid(
var1 = seq(min(inputData$var1), max(inputData$var1), length.out = 7),
var2 = seq(min(inputData$var2), max(inputData$var2), length.out = 7),
var3 = seq(min(inputData$var3), max(inputData$var3), length.out = 7))
## The new predicted value for the specific input + 95%CI
pred <-
predict(lm1,
newdata = graph_data,
se = T,
interval="confidence",
level=0.95)$fit %>%
as_tibble() %>%
rename(Prediction = fit, `Lower limit` = lwr, `Upper limit` = upr)
## Join the input grid with the predictions - This dataset is to be used for the
## dashboard
graph_data2 <- bind_cols(graph_data, pred)
```
```{r question, eval=T, echo=F, warning=F, message=F, fig.height=4, fig.width=5}
## Turn the data.frame to SharedData class
mtcars_lowcarb_shared <- SharedData$new(graph_data2, group = "Choose")
## Preaper the filters - One filter for each variable
f1 <- filter_select(id = "lowcarb_selector",
label = "Select var1",
sharedData = mtcars_lowcarb_shared,
group = ~as.character(var1 %>% round(2)),
multiple = F
)
f2 <- filter_select(id = "lowcarb_selector",
label = "Select var2",
sharedData = mtcars_lowcarb_shared,
group = ~as.character(var2 %>% round(2)),
multiple = F
)
f3 <- filter_select(id = "lowcarb_selector",
label = "Select var3",
sharedData = mtcars_lowcarb_shared,
group = ~as.character(var3 %>% round(2)),
multiple = F
)
# Make the line plot for var1
p1 <- mtcars_lowcarb_shared %>%
ggplot(mapping = aes(x = var1, y = Prediction, group =1, fill =1)) +
geom_line()
p1 <- ggplotly(p1)
## The var1 should not be filtered (by cb1)
bscols(list(f2, f3), list(p1), widths = c(7, 7))
## 2nd variable
# Prepare data and create shared data objects
mtcars_lowcarb_shared2 <- SharedData$new(graph_data2, group = "Choose")
## Line plot for var2
p2 <- mtcars_lowcarb_shared2 %>%
ggplot(mapping = aes(x = var2, y = Prediction, group =1, fill =1) ) +
#geom_point(data = ) +
geom_line()
p2 <- ggplotly(p2)
## The var2 should not be filtered (by cb2) - But with this code it does :(
bscols(list(f1, f3), list(p2), widths = c(7))
# Prepare data and create shared data objects
mtcars_lowcarb_shared3 <- SharedData$new(graph_data2, group = "Choose")
## Line plot for var3
p3 <- mtcars_lowcarb_shared3 %>%
ggplot(mapping = aes(x = var3, y = Prediction, group =1, fill =1) ) +
#geom_point(data = ) +
geom_line()
p3 <- ggplotly(p3)
## The var2 should not be filtered (by cb3) - But with this code it does :(
bscols(list(f1, f2), list(p3), widths = c(7))
```
The outcome
The plot of var1 get filtered nicely from filters f2 + f3 (which are filtering var2 + var3)

The plot for var2
The same issue applied for var3 which is affected by f3. Instead it should be affected by f1 + f2
