Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 绘图中每个因子级别的颜色_R_Plotly - Fatal编程技术网

R 绘图中每个因子级别的颜色

R 绘图中每个因子级别的颜色,r,plotly,R,Plotly,一个客户想让我做一个像这样的情节。此参考使用rgl包,但导出的数字质量太低。于是,我就开始了精心策划。我可以管理我想做的大部分事情,但出于某种原因,所有的条都是不同的颜色 A MWE: X<-1:60 Y<-sort(runif(60,-3,3)) Z<-runif(60,0,50) p<-plot_ly(x = c(X[1],X[1]+1,X[1]+1,X[1]), y = c(0,0,Y[1],Y[1]), z=c(0,0,0,0),type = "mesh

一个客户想让我做一个像这样的情节。此参考使用rgl包,但导出的数字质量太低。于是,我就开始了精心策划。我可以管理我想做的大部分事情,但出于某种原因,所有的条都是不同的颜色

A MWE:

X<-1:60
Y<-sort(runif(60,-3,3))
Z<-runif(60,0,50)

p<-plot_ly(x = c(X[1],X[1]+1,X[1]+1,X[1]),
    y = c(0,0,Y[1],Y[1]), z=c(0,0,0,0),type = "mesh3d",color=I("red"))

for(i in X){p<-add_trace(p,x = c(i,i+1,i+1,i), 
          y = c(0,0,Y[i],Y[i]),  z=c(0,0,0,0),type = "mesh3d",color=I("red"))}
for(i in X){p<-add_trace(p,x = c(i,i+1,i+1,i), 
          y = c(0,0,0,0), z=c(0,0,Z[i],Z[i]),type = "mesh3d",i=c(0,0),
         j=c(1,2),k=c(2,3),color=I("black"))}

p

X如果按您打算的方式使用绘图,似乎会出现某种问题。
但是,有一个变通方法()。
要使事情顺利进行,您必须通过plotly构建函数覆盖plot______________;y提供的默认值

以下代码可用于获得水平条和垂直条颜色不同的绘图:

X<-1:60
Y<-sort(runif(60,-3,3))
Z<-runif(60,0,50)
ColFactor<-sample(c(0,1),60,replace = TRUE)

p<-plot_ly(color=I("black")) #This plot a layout where to add the traces and adds
                        #the attribute color needed when overriding  default. If it isn't included it doesn't work
                        #Which color you use here is unimportant, it will be override

#next lines add the bars, if you plot the result will be the same that you already know
for(i in X){p<-add_trace(p,x = c(i,i+1,i+1,i), 
                     y = c(0,0,Y[i],Y[i]),  z=c(0,0,0,0),type = "mesh3d")}
for(i in X){p<-add_trace(p,x = c(i,i+1,i+1,i), 
                     y = c(0,0,0,0), z=c(0,0,Z[i],Z[i]),type = "mesh3d",i=c(0,0),
                     j=c(1,2),k=c(2,3))}

#next step: override the defaults options using plotly_build()

p.optionslist<-plotly_build(p)

#you have to change every trace individually that's what the for loop is

#horizontal bars    
for(j in 1:((length(p.optionslist$x$data))/2)){
    p.optionslist$x$data[[j]]$color=toRGB("red")
}

#horizontal vertical bars
for(j in (((length(p.optionslist$x$data)/2)+1):length(p.optionslist$x$data))){
p.optionslist$x$data[[j]]$color=toRGB("blue")
}

#The plot
p.optionslist
X
X<-1:60
Y<-sort(runif(60,-3,3))
Z<-runif(60,0,50)
ColFactor<-sample(c(0,1),60,replace = TRUE)

p<-plot_ly(color=I("black")) #This plot a layout where to add the traces and adds
                        #the attribute color needed when overriding  default. If it isn't included it doesn't work
                        #Which color you use here is unimportant, it will be override

#next lines add the bars, if you plot the result will be the same that you already know
for(i in X){p<-add_trace(p,x = c(i,i+1,i+1,i), 
                     y = c(0,0,Y[i],Y[i]),  z=c(0,0,0,0),type = "mesh3d")}
for(i in X){p<-add_trace(p,x = c(i,i+1,i+1,i), 
                     y = c(0,0,0,0), z=c(0,0,Z[i],Z[i]),type = "mesh3d",i=c(0,0),
                     j=c(1,2),k=c(2,3))}

#next step: override the defaults options using plotly_build()

p.optionslist<-plotly_build(p)

#you have to change every trace individually that's what the for loop is

#horizontal bars    
for(j in 1:((length(p.optionslist$x$data))/2)){
    p.optionslist$x$data[[j]]$color=toRGB("red")
}

#horizontal vertical bars
for(j in (((length(p.optionslist$x$data)/2)+1):length(p.optionslist$x$data))){
p.optionslist$x$data[[j]]$color=toRGB("blue")
}

#The plot
p.optionslist
#overriding color option according to the value of ColFactor
p.optionslist2<-plotly_build(p)


for(j in 1:((length(p.optionslist2$x$data))/2)){
  if(ColFactor[j]==1){
    p.optionslist2$x$data[[j]]$color=toRGB("red")
  }else{
    p.optionslist2$x$data[[j]]$color=toRGB("blue")
  }
}

for(j in (((length(p.optionslist2$x$data))/2)+1):length(p.optionslist2$x$data)){
  i=j-length(ColFactor)
  if(ColFactor[i]==1){
    p.optionslist2$x$data[[j]]$color=toRGB("red")
  }else{
    p.optionslist2$x$data[[j]]$color=toRGB("blue")
  }
}

#The plot with color by 
p.optionslist2
p.optionslist3<-plotly_build(p)


for(j in 1:((length(p.optionslist3$x$data))/2)){
  if(ColFactor[j]==1){
    p.optionslist3$x$data[[j]]$color=toRGB("red")
  }else{
    p.optionslist3$x$data[[j]]$color=toRGB("blue")
  }
}

for(j in (((length(p.optionslist3$x$data))/2)+1):length(p.optionslist3$x$data)){
  p.optionslist3$x$data[[j]]$color=toRGB("black")
}

#The plot with color by 
p.optionslist3