library(plotly)
mymodel <- lm(r_shift ~ median_income16 + any_college, data = cali_counties)
xvars <- data.frame(x1 = seq(min(cali_counties$median_income16, na.rm=TRUE),
max(cali_counties$median_income16, na.rm=TRUE),
length.out=10),
x2 = seq(min(cali_counties$any_college, na.rm=TRUE),
max(cali_counties$any_college, na.rm=TRUE),
length.out=10))
predicted_xvars_data <- create_y_estimates(x_vals = xvars,
model = mymodel,
coefficient_names = c(y = "r_shift",
x1= "median_income16",
x2= "any_college") )
plot_ly( data = cali_counties,
x = ~median_income16,
y = ~any_college,
z = ~r_shift) %>%
add_markers(size = ~pop_estimate16, color = I('black')) %>%
add_3d_surface(model = mymodel)%>%
add_direction(model = mymodel, direction_data = predicted_xvars_data)
Run the code above in your browser using DataLab