To visualise a model, it is very useful to be able to generate an evenly spaced grid of points from the data. data_grid helps you do this by wrapping around tidyr::expand().

data_grid(data, ..., .model = NULL)

Arguments

data

A data frame

...

Variables passed on to tidyr::expand()

.model

A model. If supplied, any predictors needed for the model not present in ... will be filled in with "typical" values.

See also

seq_range() for generating ranges from continuous variables.

Examples

data_grid(mtcars, vs, am)
#> # A tibble: 4 x 2 #> vs am #> <dbl> <dbl> #> 1 0 0 #> 2 0 1 #> 3 1 0 #> 4 1 1
# For continuous variables, seq_range is useful data_grid(mtcars, mpg = seq_range(mpg, 10))
#> # A tibble: 10 x 1 #> mpg #> <dbl> #> 1 10.4 #> 2 13.0 #> 3 15.6 #> 4 18.2 #> 5 20.8 #> 6 23.5 #> 7 26.1 #> 8 28.7 #> 9 31.3 #> 10 33.9
# If you optionally supply a model, missing predictors will # be filled in with typical values mod <- lm(mpg ~ wt + cyl + vs, data = mtcars) data_grid(mtcars, .model = mod)
#> # A tibble: 1 x 3 #> wt cyl vs #> <dbl> <dbl> <dbl> #> 1 3.32 6 0
data_grid(mtcars, cyl = seq_range(cyl, 9), .model = mod)
#> # A tibble: 9 x 3 #> cyl wt vs #> <dbl> <dbl> <dbl> #> 1 4 3.32 0 #> 2 4.5 3.32 0 #> 3 5 3.32 0 #> 4 5.5 3.32 0 #> 5 6 3.32 0 #> 6 6.5 3.32 0 #> 7 7 3.32 0 #> 8 7.5 3.32 0 #> 9 8 3.32 0