2017-04-27 170 views
4

一個客戶要我做像this一個陰謀。該引用使用rgl-package,但導出數字的質量太低。於是,我開始情緒低落。我可以管理大部分我想做的事情,但出於某種原因,所有酒吧都有不同的顏色。顏色在plotly

一個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 

雖然我希望在紅色和黑色的東西,我得到這個五彩結果: enter image description here

什麼實際上,我尋找的是類似於下面的東西,用每個因子水平的顏色,但現在,結果與上面的相同:

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(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=ColFactor,colors=c("red","blue")) 

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=ColFactor,colors=c("red","blue"))} 
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 

回答

7

看起來有一些親屬當您打算使用plot_ly時,問題d。 然而,有一種解決方法(https://github.com/ropensci/plotly/issues/413)。 爲了工作的事情,你必須重寫由plotly_build功能的平均值由plot_ly提供的默認值。

下面的代碼應努力獲得圖,其中水平和垂直線有不同的顏色:

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 

Without factor

關於使用ColFactor下面的代碼工作的(當然有更好的方式,但我不知道是哪一個)

#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 

enter image description here

如果你想豎條全部由黑因子和水平條,然後你只需要兩個選項結合起來:

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 

enter image description here

希望它可以幫助你!