好吧,我花了一段时间才热身起来plotly
技能。由于你的第一点是最难的,我将反向讨论你的观点。
- 这可以通过操纵来实现
layout
using categoryorder
and categoryarray
in the yaxis
-list(参见 motos 答案here)
- Set
showlegend=FALSE
- 这很棘手。我必须移动第一行中的第二行(误差线)。添加了一个颜色向量。将其放入
plot_ly
-功能。用过的split
以允许按组正确着色。为 a 中的点添加了颜色marker
-列表。另外我转换了p.values
通过colorRamp
to hex-因为每个更简单的解决方案都不适合我。
看起来像这样:
代码(颜色条造成了一些问题):
### 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
- 错误的函数和颜色向量仅在使用时才有效split
-功能。如果有人想对此发表评论或发布具有更可读代码的替代答案,那会很有趣。