簡體   English   中英

繪圖散點圖中的顏色編碼誤差條

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

我正在嘗試使用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))

這給了我:

在此輸入圖像描述

這與我想要的非常接近,除了:

  1. 我希望錯誤條的顏色與效果大小相似(通過相應的p值)。
  2. 刪除colorbar下方的兩個trace圖例
  3. y軸上的標簽順序是factors的順序

任何的想法?

好吧我花了一段時間來溫暖我的plotly技巧。 由於你的第一點是最困難的,我會反過來看你的觀點。

    1. 這可以通過操縱來achied layout使用categoryordercategoryarrayyaxis -list(參見MOTOS回答在這里
    1. 設置showlegend=FALSE
    1. 這很棘手。 我不得不在第一行中移動你的第二行(錯誤條)。 添加了顏色向量。 把它放在plot_ly函數中。 使用split以允許按組正確着色。 添加了marker的點的顏色。 另外,我通過colorRampp.values轉換為十六進制 -因為每個更簡單的解決方案對我都不起作用。

看起來像這樣:

在此輸入圖像描述

代碼(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)

奇怪的是,這第一點難以解決並導致如此大的代碼括號,因為通常plotly支持管道邏輯,並且您獲得了具有所有add functions的非常易讀的代碼。

我期望例如,一些add_errorbar ,但顯然你必須在plot_ly -function中添加錯誤欄,並且只有在使用split plot_ly錯誤的顏色向量才有效。 如果有人想用更可讀的代碼評論或發布替代答案,那將會很有趣。

這是一個想法,首先構建一個ggplot2圖並使用ggplotly

創建數據框:

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)

創建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)

在此輸入圖像描述

數據:

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