Вы можете определить свою собственную цветовую шкалу и добавить ее к add_trace
, где вы определяете график поверхности.
colorscale = list(c(0, 1), c("tan", "blue"))
Это дает вам следующий раф
Полный код
library(plotly)
library(reshape2)
sleep <- read.table("http://www.statsci.org/data/general/sleep.txt", header=T)
sleep <- na.omit(sleep)
sleep <- mutate(sleep, logTotalSleep = log(TotalSleep))
sleep_mod <- lm(logTotalSleep ~ Gestation + Danger, data=sleep)
graph_reso <- 0.5
axis_x <- seq(min(sleep$Gestation), max(sleep$Gestation), by = graph_reso)
axis_y <- seq(min(sleep$Danger), max(sleep$Danger), by = graph_reso)
sleep_surface <- expand.grid(Gestation = axis_x,
Danger = axis_y,
KEEP.OUT.ATTRS = F)
sleep_surface$TotalSleep <- exp(predict.lm(sleep_mod, newdata = sleep_surface)) # exp to remove ln() from y
sleep_surface <- acast(sleep_surface, Danger ~ Gestation, value.var = "TotalSleep") #y ~ x
p <- plot_ly(data = sleep) %>%
add_trace(x = ~Gestation, y = ~Danger, z = ~TotalSleep,
type = "scatter3d", mode = "markers",
opacity = .8) %>%
add_trace(z = sleep_surface,
x = axis_x,
y = axis_y,
type = "surface", colorscale = list(c(0, 1), c("tan", "blue")))
p