私が作成しようとしている森林のプロットとR
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))
それは私に与えます:
これは、次の点を除いて、私が望むものにかなり近いものです。
trace
下の2つの凡例を削除しますcolorbar
factors
何か案が?
さて、plotly
スキルをウォームアップするのに少し時間がかかりました。最初のポイントが一番難しかったので、逆にポイントを見ていきます。
layout
using categoryorder
and categoryarray
in the yaxis
-list (cf. motos answer here)showlegend=FALSE
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.Looks like this:
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
関数を含む非常に読みやすいコードを取得するからです。
たとえば、いくつかadd_errorbar
の-functionを期待していましたが、どうやら-functionにplot_ly
エラーバーを追加する必要があり、エラーのカラーベクトルは-functionを使用した場合にのみsplit
機能します。誰かがこれについてもっと読みやすいコードでコメントしたり、別の答えを投稿したいのであれば、それは興味深いでしょう。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加