我在 Plotly(针对 R)中制作了一个气泡图,但我不断得到重叠的标记。有没有办法“缩小”所有标记,以便保留它们的相对大小但没有重叠?我想保持绘图的尺寸相同。这是一个测试用例:
test <- data.frame(matrix(NA, ncol=3, nrow=14))
colnames(test) <- c("Group", "Numbers", "Days")
loop<- 1
for(i in 1:7){
test[i,] <- c(1, i, loop)
loop <- loop * 1.5
}
loop <- 1
for(i in 1:7){
test[i+7,] <- c(2, i, loop)
loop <- loop * 1.3
}
plot_ly(test, x=Group, y=Numbers, size=Days, mode="markers")
最佳答案
做这种事情的一种方法是调整 marker
中的 sizeref
(和 size
)参数:
plot_ly(test, x=Group, y=Numbers, mode="markers",
marker = list(size = Days, sizeref = 0.15))
plot_ly(test, x=Group, y=Numbers, mode="markers",
marker = list(size = Days/2, sizeref = 0.1))
plot_ly(test, x=Group, y=Numbers, size = Days, mode="markers",
marker = list(sizeref = 2.5)) # Days data in the hoverinfo with this method
来自 https://plot.ly/r/reference/ :
sizeref (number)
default: 1
Has an effect only ifmarker.size
is set to a numerical array. Sets the scale factor used to determine the rendered size of marker points. Use withsizemin
andsizemode
.
如果您希望悬停文本与您的原始 plotly 相匹配,您可以明确定义它:
plot_ly(test, x=Group, y=Numbers, mode="markers",
marker = list(size = Days, sizeref = 0.15),
hoverinfo = "text",
text = paste0("(", Group, ", ", Numbers, ")<br>", "Days (size): ", Days))
关于R plotly : Smaller markers in bubble plot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38400343/