Search code examples
rplotlyscatter-plotggplotlyr-plotly

R plotly: not enough granularity for the size of the bubbles?


In the bubble graph generated by the code below, I don't have enough granularity for the sizes of the bubble. There is a ratio of 20,000 between the smallest (size 100) bubble and the largest bubble (size 2,000,000).
As a consequence the bubble of size 100 has the same area than the bubble of size 12,000.

How could I increase the granularity for the sizes of the bubble?

I played a bit with the parameter sizes of plot_ly but it didn't give anything good (I do not totally understand how to use this parameter)
A quick fix would be to use the logarithm of the size instead of the size itself but I would like to keep the proportionality.
A solution with plot_ly would be my first choice but maybe ggplotly is the way to do it.

df <- data.frame(c(1,2,3,4,5,6,7,8), 
c(5,20,-10,40,-3,6,10,-15),c(1e2,1.2e4,9e4,4.2e5,2e6,1.5e6,5.4e5,8e5))
colnames(df) <- c("X", "Y", "size")

p=plot_ly(df, x = ~X, y = ~Y, type="scatter", text = paste("size:", 
df$size), mode = "markers", size = ~abs(df$size), 
marker=list(sizemode='diameter'))
p

Solution

  • I don't see how it could be done in plot_ly. Scaling does not fix the problem.

    But ggplot2 gives you a nice difference between 100 and 12000:

    a <- ggplot(df, aes(x=X, y=Y, size = size)) + geom_point(colour = "dodgerblue3") + theme_bw()
    ggplotly(a)