【问题标题】:How do I create weighted scatter plot / bubble chart with color gradient using plotly R如何使用 plotly R 创建带有颜色渐变的加权散点图/气泡图
【发布时间】:2019-10-21 03:29:34
【问题描述】:

我正在尝试使用 plotly 生成加权散点图/气泡图。我的数据集包含 4 列:

1) 基准测试 2) 型号 3) 改进 4) 重量

我试图以 x 轴为基准,以 y 轴为模型。点的大小是权重,颜色是基于改进值的渐变。除了渐变,我可以让它全部工作。

见下面的代码:

BenchmarkQuant <- c("A","A","A","B","B","B","C","C","C")
ModelQuant     <- c("X","Y","Z","X","Y","Z","X","Y","Z")
ModelImprovement <- c(runif(9))
SumExposure <-    c(runif(9))*100

data <- as.data.frame(cbind(BenchmarkQuant,ModelQuant,ModelImprovement,SumExposure))
data$SumExposure <- as.numeric(data$SumExposure)

p <- plot_ly(data,
             x = ~BenchmarkQuant,
             y = ~ModelQuant,
             type = 'scatter',
             mode = 'markers',  
             size = ~SumExposure,  
             color = (ModelImprovement), #These are the 2 lines causing issues
             colors = 'Reds',            #These are the 2 lines causing issues
             #Choosing the range of the bubbles' sizes:
             sizes = c(20, 75), 
             marker = list(opacity = 0.5, sizemode = 'diameter')) %>%
  layout(title = 'Model Comparison',
         xaxis = list(showgrid = FALSE),
         yaxis = list(showgrid = FALSE),
         showlegend = TRUE)

p

我收到以下错误消息:

Error in Summary.factor(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), na.rm = TRUE) : 
  ‘range’ not meaningful for factors
In addition: Warning message:
`line.width` does not currently support multiple values. 

运行上述代码时,如果我不包含关于颜色的 2 行,则它可以工作,除非没有渐变。

【问题讨论】:

    标签: r plotly scatter-plot r-plotly bubble-chart


    【解决方案1】:

    你们很亲密。在您回复后做了一些调整。希望它是您正在寻找的。​​p>

    p <- plot_ly(data,
                 x = ~BenchmarkQuant,
                 y = ~ModelQuant,
                 type = 'scatter',
                 mode = 'markers',  
                 size = ~SumExposure, 
                 colors = 'Reds',
                 hoverinfo = 'x+y+text', text = ~paste("Improvement: ", ModelImprovement, "<br>"),
    
                 #Choosing the range of the bubbles' sizes:
                 sizes = c(20, 75), 
                 marker = list(opacity = 0.5, sizemode = 'diameter',
                               color = ~ModelImprovement,
                               line = list(
                                 color = ~ModelImprovement,
                                 width = 1),
                               colorbar=list(title='Colorbar'))) %>%
      layout(title = 'Model Comparison',
             xaxis = list(showgrid = FALSE),
             yaxis = list(showgrid = FALSE),
             showlegend = FALSE)
    
    p
    

    【讨论】:

    • 这行得通,但不是我想要的。问题在于,创建的图表似乎将 ModelImprovement 视为分类变量,如图例所示,是单个值以及相关颜色,而不是渐变比例。
    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多