我正在尝试使用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行内容,那么除了没有渐变之外,其他方法都可以工作。
答案 0 :(得分: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