本文介绍了散点图中的颜色编码误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个林地,其中R plotly我想用相应的p值对效果大小(点)及其误差线进行颜色编码.

I'm trying to create a forest plot with R plotly where I want to color code the effect sizes (points) and their error bars by their corresponding p-values.

以下是玩具数据:

set.seed(1)

factors <- paste0(1:25,":age")
effect.sizes <- rnorm(25,0,1)
effect.errors <- abs(rnorm(25,0,1))
p.values <- runif(25,0,1)

这就是我要尝试的:

library(dplyr)
plotly::plot_ly(type='scatter',mode="markers",y=~factors,x=~effect.sizes,color=~p.values,colors=grDevices::colorRamp(c("darkred","gray"))) %>%
      plotly::add_trace(error_x=list(array=effect.errors),marker=list(color=~p.values,colors=grDevices::colorRamp(c("darkred","gray")))) %>%
      plotly::colorbar(limits=c(0,1),len=0.4,title="P-Value") %>%
      plotly::layout(xaxis=list(title="Effect Size",zeroline=T,showticklabels=T),yaxis=list(title="Factor",zeroline=F,showticklabels=T))

这给了我

除了:

  1. 我希望误差条的颜色类似于效果大小(通过相应的p值).
  2. 删除colorbar
  3. 下的两个trace图例
  4. 在y轴上的标签顺序应为factors
  1. I'd like the error bars to be colored similar to the effect sizes (by the corresponding p-values).
  2. Remove the two trace legends below the colorbar
  3. Have the order of the labels on the y-axis be that of factors

有什么主意吗?

推荐答案

好的,我花了一些时间来锻炼我的plotly技能.由于您的第一点是最难的,因此我将反向介绍您的观点.

Okay it took me a while to warm up my plotly skills. Since your first point was the most difficult, I will go reversely through your points.

  1. 可以通过使用categoryorder操纵layout来实现和yaxis-列表中的categoryarray(请参见motos回答此处)
  1. That can be achied by manipulating the layout using categoryorderand categoryarray in the yaxis-list (cf. motos answer here)

    1. 设置showlegend=FALSE

    1. 那很棘手.我必须在第一行中移动第二行(误差线).向其添加了颜色矢量.将其放在plot_ly函数中.使用split允许按组正确着色.在marker-列表中添加了点的颜色.此外,我还通过colorRamp >十六进制转换了p.values -因为每个简单的解决方案都不适合我.
    1. That was tricky. I had to move your second line (the error bars) in the first. Added a color vector to it. Put it in the plot_ly-function. Used split to allow the correct coloring by group. Added the color for the points in a marker-list. In additon I converted the p.values via the colorRamp to hex-because every simpler solution didn't work for me.

  • 看起来像这样:

    代码(颜色栏创建了一些问题):

    The code (the colorbar created some issues):

    ### Set category order
    yform <- list(categoryorder = "array",
                  categoryarray = rev(factors),
                  title="Factor",zeroline=F,showticklabels=T)
    
    ### set the color scale and convert it to hex
    library(grDevices)
    mycramp<-colorRamp(c("darkred","gray"))
    mycolors<-rgb(mycramp(p.values),maxColorValue = 255)
    
    ### plot without the adjusted colorbar
    library(plotly)
    ### Without colorbar adjustment
      plot_ly(type='scatter',mode="markers",y=~factors,x=~effect.sizes,
              color=~p.values,colors=grDevices::colorRamp(c("darkred","gray")),
              error_x=list(array=effect.errors,color=mycolors),split=factors,showlegend=FALSE,marker=list(color=mycolors)) %>%
          layout(xaxis=list(title="Effect Size",zeroline=T,showticklabels=T),yaxis=yform)
    
      ### The colorbar-adjustment kicks out the original colors of the scatter points. Either you plot them over
      plot_ly(type='scatter',mode="markers",y=~factors,x=~effect.sizes,
              color=~p.values,colors=grDevices::colorRamp(c("darkred","gray")),
              error_x=list(array=effect.errors,color=mycolors),split=factors,showlegend=FALSE,marker=list(color=mycolors)) %>%
          layout(xaxis=list(title="Effect Size",zeroline=T,showticklabels=T),yaxis=yform) %>%
      colorbar(limits=c(0,1),len=0.4,title="P-Value",inherit=FALSE) %>%
          add_trace(type='scatter',mode="markers",y=~factors,x=~effect.sizes,
                showlegend=FALSE,marker=list(color=mycolors),inherit=FALSE) %>%
        layout(xaxis=list(title="Effect Size",zeroline=T,showticklabels=T),yaxis=yform)
    
      ### or you try to set the colorbar before the plot. This results in some warnings
      plot_ly() %>%
      colorbar(limits=c(0,1),len=0.4,title="P-Value",inherit=FALSE) %>%
          add_trace(type='scatter',mode="markers",y=~factors,x=~effect.sizes,
              color=~p.values,colors=grDevices::colorRamp(c("darkred","gray")),
              error_x=list(array=effect.errors,color=mycolors),split=factors,showlegend=FALSE,marker=list(color=mycolors)) %>%
          layout(xaxis=list(title="Effect Size",zeroline=T,showticklabels=T),yaxis=yform)
    

    奇怪的是,这第一点很难解决并且导致了如此庞大的代码括号,因为通常plotly很好地支持管道逻辑,并且您获得了具有所有add功能的可读性很强的代码.

    Just odd that this first point was so difficult to solve and results in such a big code bracket, because normally plotly supports that pipe logic quite well and you get a very readable code with all the add-functions.

    我期望例如某些add_errorbar函数,但是显然您必须在plot_ly函数中添加错误栏,并且错误的颜色向量仅在使用split函数时才有效.如果有人想对此发表评论或发表具有更易读代码的替代答案,那将很有趣.

    I expected e.g., some add_errorbar-function, but apparently you have to add the errorbars in the plot_ly-function and the color-vector for the errors only works if you use the split-function. If someone would like to comment or post an alternative answer with more readable code on this, that would be interesting.

    这篇关于散点图中的颜色编码误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-11 14:28