简体   繁体   中英

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

布尔重叠标记

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM