简体   繁体   English

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

[英]Color coding error bars in a plotly scatter plot

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. 我正在尝试使用R plotly创建一个森林图 ,我想用它们对应的p值对效果大小(点)及其误差条进行颜色编码。

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: 这与我想要的非常接近,除了:

  1. I'd like the error bars to be colored similar to the effect sizes (by the corresponding p-values). 我希望错误条的颜色与效果大小相似(通过相应的p值)。
  2. Remove the two trace legends below the colorbar 删除colorbar下方的两个trace图例
  3. Have the order of the labels on the y-axis be that of factors y轴上的标签顺序是factors的顺序

Any idea? 任何的想法?

Okay it took me a while to warm up my plotly skills. 好吧我花了一段时间来温暖我的plotly技巧。 Since your first point was the most difficult, I will go reversely through your points. 由于你的第一点是最困难的,我会反过来看你的观点。

    1. That can be achied by manipulating the layout using categoryorder and categoryarray in the yaxis -list (cf. motos answer here ) 这可以通过操纵来achied layout使用categoryordercategoryarrayyaxis -list(参见MOTOS回答在这里
    1. Set showlegend=FALSE 设置showlegend=FALSE
    1. 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. 把它放在plot_ly函数中。 Used split to allow the correct coloring by group. 使用split以允许按组正确着色。 Added the color for the points in a marker -list. 添加了marker的点的颜色。 In additon I converted the p.values via the colorRamp to hex -because every simpler solution didn't work for me. 另外,我通过colorRampp.values转换为十六进制 -因为每个更简单的解决方案对我都不起作用。

Looks like this: 看起来像这样:

在此输入图像描述

The code (the colorbar created some issues): 代码(colorbar创建了一些问题):

### 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 the add -functions. 奇怪的是,这第一点难以解决并导致如此大的代码括号,因为通常plotly支持管道逻辑,并且您获得了具有所有add functions的非常易读的代码。

I expected eg, some add_errorbar -function, but apparently you have to add the errorbars in the plot_ly -function and the color-vector for the errors only works if you use the split -function. 我期望例如,一些add_errorbar ,但显然你必须在plot_ly -function中添加错误栏,并且只有在使用split plot_ly错误的颜色向量才有效。 If someone would like to comment or post an alternative answer with more readable code on this, that would be interesting. 如果有人想用更可读的代码评论或发布替代答案,那将会很有趣。

Here is an idea by constructing first a ggplot2 graph and using ggplotly : 这是一个想法,首先构建一个ggplot2图并使用ggplotly

create a data frame : 创建数据框:

df <- data.frame(factors = factor(factors, levels = factors), #note the order of the levels which determines the order of the y axes
                 effect.sizes = effect.sizes,
                 effect.errors = effect.errors,
                 p.values = p.values)

create the ggplot graph: 创建ggplot图:

library(ggplot2)
library(plotly)

ggplot(df)+
  geom_vline(xintercept = 0, color = "grey50") +
  geom_point(aes(y = factors,
                 x = effect.sizes,
                 color = p.values)) +
  geom_errorbarh(aes(y = factors,
                     xmin = effect.sizes - effect.errors,
                     xmax = effect.sizes + effect.errors,
                     x = effect.sizes,
                     color = p.values)) +
  scale_color_continuous(low = "darkred", high = "gray")+
  theme_bw() +
  xlab("Effect Sizes")+
  ylab("Factors") +
  theme(panel.border = element_blank(),
        plot.margin = margin(1, 1, 1, 1, "cm")) -> p1


ggplotly(p1)

在此输入图像描述

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM