R,如何更改绘制的3d曲面的颜色? [英] R, How to change color of plotly 3d surface?

查看:52
本文介绍了R,如何更改绘制的3d曲面的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将色标从默认的紫色更改为黄色?我尝试将颜色和色标参数添加到add_trace()中,但是会引发错误.

具有默认颜色的可复制代码:

  library(plotly);库(reshape2);图书馆(tidyverse)睡眠<-read.table("http://www.statsci.org/data/general/sleep.txt",标头= T)睡眠<-na.omit(睡眠)睡眠<-mutate(睡眠,logTotalSleep = log(TotalSleep))sleep_mod<-lm(logTotalSleep〜Gestation + Danger,data = sleep)#图形分辨率(对于更复杂的形状更重要)graph_reso<-0.5#0.05#设定轴axis_x<-seq(min(sleep $ Gestation),max(sleep $ Gestation),by = graph_reso)axis_y<-seq(最小(sleep $ Danger),最大(sleep $ Danger),by = graph_reso)#采样点sleep_surface<-expand.grid(Gestation = axis_x,危险= axis_y,KEEP.OUT.ATTRS = F)sleep_surface $ TotalSleep<-exp(predict.lm(sleep_mod,newdata = sleep_surface))#exp从y中删除ln()sleep_surface<-广播(sleep_surface,Danger〜Gestation,value.var ="TotalSleep")#y〜x# 阴谋p<-plot_ly(数据=睡眠)%&%add_trace(x =〜Gestation,y =〜Danger,z =〜TotalSleep,类型="scatter3d",模式=标记",透明度= .8)%>%add_trace(z = sleep_surface,x = axis_x,y = axis_y,类型=表面")%&%;%布局(场景=列表(x轴=列表(标题=妊娠期(天)")),yaxis = list(标题=危险指数"),zaxis = list(title ='Hours of Sleep')))p 

解决方案

您可以定义自己的

完整代码

 库(可打印)库(reshape2)睡眠<-read.table("http://www.statsci.org/data/general/sleep.txt",标头= T)睡眠<-na.omit(睡眠)睡眠<-mutate(睡眠,logTotalSleep = log(TotalSleep))sleep_mod<-lm(logTotalSleep〜Gestation + Danger,data = sleep)graph_reso<-0.5axis_x<-seq(min(sleep $ Gestation),max(sleep $ Gestation),by = graph_reso)axis_y<-seq(最小(sleep $ Danger),最大(sleep $ Danger),by = graph_reso)sleep_surface<-expand.grid(Gestation = axis_x,危险= axis_y,KEEP.OUT.ATTRS = F)sleep_surface $ TotalSleep<-exp(predict.lm(sleep_mod,newdata = sleep_surface))#exp从y中删除ln()sleep_surface<-广播(sleep_surface,Danger〜Gestation,value.var ="TotalSleep")#y〜xp<-plot_ly(数据=睡眠)%&%add_trace(x =〜Gestation,y =〜Danger,z =〜TotalSleep,类型="scatter3d",模式=标记",透明度= .8)%>%add_trace(z = sleep_surface,x = axis_x,y = axis_y,类型=表面",色标=列表(c(0,1),c(棕褐色",蓝色")))p 

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.

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

解决方案

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

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

This gives you the following raph

Complete code

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

这篇关于R,如何更改绘制的3d曲面的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆