散点图中的颜色编码误差条 [英] Color coding error bars in a plotly scatter plot
问题描述
我正在尝试使用 R
这与我想要的非常接近,除了:
- 我希望误差条的颜色与效果大小相似(通过相应的 p 值).
- 移除
colorbar
下面的两个 - y 轴上标签的顺序是否为
factors
trace
图例有什么想法吗?
好吧,我花了一段时间来预热我的 plotly
技能.由于您的第一点是最困难的,所以我将反向通过您的点.
- 这可以通过使用
categoryorder
操作layout
来实现和yaxis
-list 中的categoryarray
(参见 motos 回答代码(颜色条产生了一些问题):
### 设置分类顺序yform <- list(categoryorder = "array",categoryarray = rev(因子),title="因子",zeroline=F,showticklabels=T)### 设置色阶并将其转换为十六进制库(grDevices)mycramp<-colorRamp(c("darkred","gray"))mycolors<-rgb(mycramp(p.values),maxColorValue = 255)### 没有调整颜色条的绘图图书馆(情节)### 没有颜色条调整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)) %>%布局(xaxis=list(title="效果大小",zeroline=T,showticklabels=T),yaxis=yform)###颜色条调整踢出散点的原始颜色.要么你把它们画出来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)) %>%布局(xaxis=list(title="效果大小",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) %>%布局(xaxis=list(title="效果大小",zeroline=T,showticklabels=T),yaxis=yform)### 或者您尝试在绘图之前设置颜色条.这会导致一些警告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)) %>%布局(xaxis=list(title="效果大小",zeroline=T,showticklabels=T),yaxis=yform)
奇怪的是,这第一点是如此难以解决并导致如此大的代码括号,因为通常
plotly
很好地支持该管道逻辑,并且您会得到一个非常可读的代码,其中包含所有 <代码>添加代码>-功能.我希望,例如,一些
add_errorbar
-function,但显然你必须在plot_ly
-function 中添加错误栏,并且错误的颜色向量仅在以下情况下才有效您使用split
功能.如果有人想对此发表评论或发布具有更易读代码的替代答案,那将很有趣.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.Here are toy data:
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)
Here's what I'm trying:
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))
which gives me:
Which is pretty close to what I want except for:
- I'd like the error bars to be colored similar to the effect sizes (by the corresponding p-values).
- Remove the two
trace
legends below thecolorbar
- Have the order of the labels on the y-axis be that of
factors
Any idea?
解决方案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.- That can be achied by manipulating the
layout
usingcategoryorder
andcategoryarray
in theyaxis
-list (cf. motos answer here)
- That can be achied by manipulating the
- Set
showlegend=FALSE
- Set
- 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. Usedsplit
to allow the correct coloring by group. Added the color for the points in amarker
-list. In additon I converted thep.values
via thecolorRamp
to hex-because every simpler solution didn't work for me.
- 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
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)
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 theadd
-functions.I expected e.g., some
add_errorbar
-function, but apparently you have to add the errorbars in theplot_ly
-function and the color-vector for the errors only works if you use thesplit
-function. If someone would like to comment or post an alternative answer with more readable code on this, that would be interesting.这篇关于散点图中的颜色编码误差条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 这可以通过使用