如何使用 plotly R 创建带有颜色渐变的加权散点图/气泡图
How do I create weighted scatter plot / bubble chart with color gradient using plotly R
我正在尝试使用 plotly 生成加权 scatterplot/bubble 图表。我的数据集包含 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
我正在尝试使用 plotly 生成加权 scatterplot/bubble 图表。我的数据集包含 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