I want to show with an interactive ggvis
plot the impact of outliers on the estimated OLS regression line. For this I want a slider which updates only one y value of my dataframe e.g df$y[10] + 1
, df$y[10] + 2
and so on. How can I achieve this?
Some data to play arround with:
set.seed(123)
x <- sort(runif(15, min = 30, max = 70 ))
y <- rnorm(15 , mean = 200, sd = 50)
df <- data.frame(x, y)
My current static ggvis code:
Library(ggvis)
df %>%
ggvis(x = ~x, y = ~y) %>%
layer_points() %>%
layer_model_predictions(model = "lm", formula = y ~ x)
It is possible to combine the dplyr
functionality with ggvis.
The following link describes how it works:
Applying a dplyr verb to a ggvis object creates a reactive transformation: whenever the underlying data changes the transformation will be recomputed.
With that I ended up using mutate with a self written function outlier
.
The important point is to wrap the reactive slider input in eval()
. I am sure there still is potential for improvement but this might be a good starting point.
library(ggvis)
library(dplyr)
set.seed(123)
x <- sort(runif(15, min = 30, max = 70 ))
y <- rnorm(15 , mean = 200, sd = 50)
df <- data.frame(y, x)
v <- input_slider(0, 250, value = 0, step = 25, animate = TRUE)
outlier <- function(y, v) y + c(rep(0, 14), v)
df %>%
ggvis(x = ~x, y = ~y) %>%
mutate(y = outlier(y, eval(v))) %>%
layer_points() %>%
layer_model_predictions(model = "lm", formula = y ~ x)