gpt4 book ai didi

r - 散点图中的颜色编码误差条

转载 作者:行者123 更新时间:2023-12-03 16:52:09 25 4
gpt4 key购买 nike

我正在尝试创建一个 forest plotR plotly我想通过相应的 p 值对效果大小(点)及其误差线进行颜色编码。

以下是玩具数据:

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

这给了我:

enter image description here

这非常接近我想要的,除了:
  • 我希望误差线的颜色类似于效果大小(通过相应的 p 值)。
  • 删除两个 trace下面的传说colorbar
  • y 轴上标签的顺序是 factors

  • 任何的想法?

    最佳答案

    好吧,我花了一些时间来预热 plotly技能。由于你的第一点是最困难的,我将通过你的观点反向。

  • 这可以通过操作 layout 来实现。使用 categoryordercategoryarrayyaxis -list(参见 motos 答案 here)
  • 套装showlegend=FALSE
  • 那很棘手。我不得不在第一行移动你的第二行(误差线)。为它添加了一个颜色向量。把它放在plot_ly -功能。二手 split允许按组正确着色。为 marker 中的点添加颜色-列表。此外,我转换了 p.values通过 colorRamp to hex - 因为每个更简单的解决方案都不适合我。

  • 看起来像这样:

    enter image description here

    代码(颜色条产生了一些问题):
    ### 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 的非常易读的代码。 -职能。

    我预计,例如,一些 add_errorbar -function,但显然您必须在 plot_ly 中添加错误栏-function 和用于错误的颜色向量仅在您使用 split 时才有效-功能。如果有人想对此发表评论或发布具有更多可读代码的替代答案,那将会很有趣。

    关于r - 散点图中的颜色编码误差条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50870903/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com