r - 如何使用plotly R创建带有颜色渐变的加权散点图/气泡图

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

我正在尝试使用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. 

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

最佳答案

你们非常接近。收到回复后做了一些调整。希望这是您正在寻找的。

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

关于r - 如何使用plotly R创建带有颜色渐变的加权散点图/气泡图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56463562/

相关文章:

plot - 如何从 Plotly 中删除轴和数字

Matplotlib Colorbar 缺少 1 个必需的位置参数 : 'mappable'

javascript - 散点图 html5/javascript 库,允许为每个点设置颜色和 alpha

截距为零的随机效应方差

javascript - Plotly 根据方程画线

r - data.table 中的 fwrite 将我的字符串列更改为整数,如何修复?

jquery - 在 R Shiny 中使用 jQuery 时 Plotly 消失

3d - Web 应用程序的 3D 散点图

r - 如何更改树状图的节点标签

r - 按成员姓名加入列表