我正在尝试使用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 行,则除了没有渐变之外,它可以工作。
你非常接近。在您回复后进行了一些调整。希望这是你要找的。
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