# Both fitted surfaces on a grid of the two features.
wy_weight_range <- range(auto$weight)
wy_weights <- seq(wy_weight_range[[1]], wy_weight_range[[2]], length.out = 20)
wy_years <- 70:82
wy_grid <- tidyr::expand_grid(year = wy_years, weight = wy_weights)
wy_pred_additive <- predict(fit_wy_additive, newdata = wy_grid)
wy_pred_interaction <- predict(fit_weight_year, newdata = wy_grid)
# One row of heights for each year.
wy_nyears <- length(wy_years)
wy_z_additive <- matrix(wy_pred_additive, nrow = wy_nyears, byrow = TRUE)
wy_z_interaction <- matrix(wy_pred_interaction, nrow = wy_nyears, byrow = TRUE)
# One z range for both panels, holding the cars and both surfaces.
wy_zlim <- range(auto$mpg, wy_z_additive, wy_z_interaction)
wy_slice_rows <- seq(1, wy_nyears, by = 2)
wy_is_html <- knitr::is_html_output()
# Draws one fitted surface z (one row per year) over the cars, with its
# slices at every second model year: an interactive plotly widget in HTML,
# and the same picture from persp() in static formats.
# nolint start: function_location_linter. lds has no R/ directory.
wy_surface <- function(cars, z, fill, slice_colour, alt) {
if (wy_is_html) {
fills <- c(fill, fill)
stops <- c(0, 1)
scale <- list(stops, fills)
slices <- list(
show = TRUE, start = 70, end = 82, size = 2,
color = slice_colour, width = 4
)
contours <- list(y = slices)
marker <- list(size = 2, color = "grey")
# A distant camera, aimed a little off centre, keeps every axis label
# inside a half-width panel.
eye <- list(x = 2.6, y = -2.6, z = 1.3)
aim <- list(x = -0.15, y = -0.05, z = 0)
camera <- list(eye = eye, center = aim)
tick_font <- list(size = 9)
title_font <- list(size = 11)
weight_title <- list(text = "weight", font = title_font)
year_title <- list(text = "year", font = title_font)
mpg_title <- list(text = "mpg", font = title_font)
xaxis <- list(title = weight_title, nticks = 4, tickfont = tick_font)
yaxis <- list(title = year_title, nticks = 4, tickfont = tick_font)
zaxis <- list(
title = mpg_title, nticks = 4, tickfont = tick_font, range = wy_zlim
)
# The three axes have different units, so the box is a cube.
scene <- list(
xaxis = xaxis, yaxis = yaxis, zaxis = zaxis,
camera = camera, aspectmode = "cube"
)
# Plotly's default margins would leave the half-width panel little room.
no_margin <- list(l = 0, r = 0, t = 0, b = 0)
widget <- plotly::plot_ly(height = 320) |>
plotly::add_surface(
x = wy_weights,
y = wy_years,
z = z,
colorscale = scale,
contours = contours,
showscale = FALSE,
opacity = 0.6
) |>
plotly::add_trace(
data = cars,
type = "scatter3d",
mode = "markers",
x = ~weight,
y = ~year,
z = ~mpg,
marker = marker
) |>
plotly::layout(scene = scene, margin = no_margin, showlegend = FALSE)
htmltools::div(role = "img", `aria-label` = alt, widget)
} else {
# persp() wants one row of heights for each weight.
z_persp <- t(z)
facet_fill <- adjustcolor(fill, 0.4)
margins <- c(1, 1, 0, 1)
flat <- list(mar = margins)
withr::with_par(flat, {
view <- persp(
wy_weights, wy_years, z_persp,
zlim = wy_zlim, theta = -35, phi = 25, expand = 0.8,
col = facet_fill, border = NA, ticktype = "detailed", nticks = 4,
xlab = "weight", ylab = "year", zlab = "mpg",
cex.axis = 0.7, cex.lab = 0.8
)
for (k in wy_slice_rows) {
year_k <- wy_years[[k]]
slice <- trans3d(wy_weights, year_k, z_persp[, k], view)
lines(slice, col = slice_colour, lwd = 1.5)
}
car_points <- trans3d(cars$weight, cars$year, cars$mpg, view)
points(car_points, pch = 16, cex = 0.3, col = "grey40")
})
}
}
# nolint end
wy_surface(auto, wy_z_additive, "#0072B2", "#003F63", wy_alt_additive)