2

I'm trying to produce a weighted scatterplot/bubble chart using plotly. My dataset contains 4 columns:

1) Benchmark 2) Model 3) Improvement 4) Weight

I'm trying to have the x-axis be the benchmark, the y-axis the model. The size of the points would be the weight and the color a gradient based on the value of improvement. I can get it all to work except the gradient.

See code below:

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

I get the following error message:

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. 

When running the above code but if I don't include the 2 lines about color it works except without the gradient.

1 Answers1

2

You were very close. Made some adjustments after your reply. Hope it is what you are looking for.

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
Claudiu Papasteri
  • 2,469
  • 1
  • 17
  • 30
  • 1
    This works but it's not quite what I was looking for. The issue is that the graph created seems to treat ModelImprovement as a categorical variable as seen by the legend being the individual values along with the associated color rather than a gradient scale. – VincentChartier93 Jun 05 '19 at 17:11