Reputation: 586
I'm making a bubble plot in Plotly (for R) and I keep getting overlapping markers. Is there a way to "scale down" all markers, so that their relative sizes are preserved but there is no overlap? I want to keep the dimensions of plot the same. Here's a test case:
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")
Upvotes: 3
Views: 2692
Reputation: 17611
One way to do this sort of thing is to adjust the sizeref
(and size
) argument in marker
:
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
From 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
.
If you wanted the hover text to match your original plot, you could define it explicitly:
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))
Upvotes: 6