绘图散点图中的误差条大小

时间:2018-06-15 17:28:58

标签: r plotly errorbar r-plotly forestplot

此问题遵循this post

我正在使用R plotly绘制forst plot,其中我按照相应的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)

我正在添加hoverover文字:

text.vec <- paste0(paste0("Factor: ",factors),"\n","Effect Size: ",effect.sizes,"\n","Effect Error: ",effect.errors,"\n","P-Value: ",p.values)

然后将其全部绘制出来:

yform <- list(categoryorder = "array",
              categoryarray = rev(factors),
              title="Factor",zeroline=F,showticklabels=T)

library(grDevices)
mycramp <- colorRamp(c("darkred","gray"))
mycolors <- rgb(mycramp(p.values),maxColorValue = 255)

library(plotly)

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,text=~text.vec,hoverinfo="text",
            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)

给出:

enter image description here

因此颜色编码效果很好。但是,似乎图中误差条的长度与effect.errors不对应。例如:

> factors[7]
[1] "7:age"
> effect.sizes[7]
[1] -0.8721795
> effect.errors[7]
[1] 0.1324996

> factors[11]
[1] "11:age"
> effect.sizes[11]
[1] -1.198534
> effect.errors[11]
[1] 0.8714454

但是在图中你看到7:age的误差条比11:age的误差条长得多。

在致电error_x function时,我是否遗漏了一些参数?

0 个答案:

没有答案