【问题标题】:Colors per factor-level in plotlyplotly 中每个因子级别的颜色
【发布时间】:2017-04-27 12:32:13
【问题描述】:

一位客户希望我制作像this 这样的情节。此参考使用 rgl-package,但导出的图形质量太低。于是,我开始了阴谋。我可以管理我想做的大部分事情,但由于某种原因,所有的条都具有不同的颜色。

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

【问题讨论】:

    标签: r plotly


    【解决方案1】:

    在按照您打算使用的方式使用 plot_ly 时似乎存在某种问题。 但是,有一种解决方法 (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
    

    关于 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
    

    如果您希望垂直条全部为黑色,而水平条按因子显示,那么您只需将这两个选项结合起来:

    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
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2020-08-21
      • 2018-02-25
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2021-05-14
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多