Kira Tebbe
Kira Tebbe

Reputation: 586

R Plotly: Smaller markers in bubble plot

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")

booo overlapping markers

Upvotes: 3

Views: 2692

Answers (1)

Jota
Jota

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 if marker.size is set to a numerical array. Sets the scale factor used to determine the rendered size of marker points. Use with sizemin and sizemode.

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

Related Questions