R,如何改变明显的3d曲面的颜色?

时间:2021-02-04 14:57:02

How do I change the colorscale from the default of purple to yellow? I tried adding color and colorscale parameters to add_trace(), but it throws up errors.

如何将颜色从紫色改为黄色?我尝试向add_trace()添加颜色和颜色比例参数,但它会抛出错误。

Reproduceable code with default colors:

默认颜色的可再生代码:

library(plotly); library(reshape2); library(tidyverse)
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 Resolution (more important for more complex shapes)
graph_reso <- 0.5 #0.05

# Setup Axis
axis_x <- seq(min(sleep$Gestation), max(sleep$Gestation), by = graph_reso)
axis_y <- seq(min(sleep$Danger), max(sleep$Danger), by = graph_reso)

# Sample points
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


# Plot
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") %>% 
  layout(scene = list(xaxis = list(title = 'Gestation Period (days)'),
                      yaxis = list(title = 'Danger Index'),
                      zaxis = list(title = 'Hours of Sleep')))
p

1 个解决方案

#1


3  

You could add an array with your colors to the plot_ly statement, e.g.

您可以在plot_ly语句中添加一个带有颜色的数组,例如。

p <- plot_ly(data = sleep,
             c('tan','blue'))

or define your own colorscale and add it to add_trace where you define the surface plot.

或者定义自己的colorscale,并将其添加到add_trace中,在那里定义表面图。

colorscale = list(c(0, 1), c("tan", "blue"))

Both methods give you the following raph

这两种方法都提供了以下raph

R,如何改变明显的3d曲面的颜色?

#1


3  

You could add an array with your colors to the plot_ly statement, e.g.

您可以在plot_ly语句中添加一个带有颜色的数组,例如。

p <- plot_ly(data = sleep,
             c('tan','blue'))

or define your own colorscale and add it to add_trace where you define the surface plot.

或者定义自己的colorscale,并将其添加到add_trace中,在那里定义表面图。

colorscale = list(c(0, 1), c("tan", "blue"))

Both methods give you the following raph

这两种方法都提供了以下raph

R,如何改变明显的3d曲面的颜色?