How do I create weighted scatter plot / bubble chart with color gradient using plotly R

VincentChartier93

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.

Claudiu Papasteri

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plot a bubble chart using plotly

How do I get the color used by plotly express for each bubble in a scatter map?

How do I create a scatter plot with ordered character variables on the x-axis using plot() in r?

How do I create a 2d color gradient plot using matplotlib?

Change legend marker size for Plotly scatter plot (bubble chart) in Python

Plotly: How to plot rectangle with gradient color in Plotly?

How do I plot a bubble chart with two different size aesthetics

Interactive Scatter plot in R using Plotly and Shiny

How to label bubble chart/scatter plot with column from pandas dataframe?

How can i have color axis in bubble chart using Highchart?

Change Plotly scatter plot color

How to add gradient color fill to the points of scatter plot using google Charts?

How can I manually color each point in my scatter-plot in plotly?

Issue changing marker color in R plotly scatter plot

how to change the grid line color in plotly scatter plot?

How to plot scatter plot for specific rows using plotly

How to give color to a class in scatter plot in R?

How to color clusters in scatter plot using an array?

How do I create a grouped percent plot in R using ggplot?

How do I create a clustered chart using position dodge in R

R Plotly: How to set the color of Individual Bars of a Waterfall Chart in R Plot.ly?

How do I create a 2 dimensional color gradient in android

How can I create a tile plot using plotly, where each tile has a text label and a variable color intensity?

How do I plot a 3D surface and data points using plotly and R?

Color code a scatter plot by group with a gradient

How can I create a gradient effect using color divisions?

How would I create a scatter plot in R like the one below in excel? (See picture and my attempt at using R)

How plot a pie chart colored with one scaled color and using plotly package

How do I add legends in a groupby line plot in plotly in R?