I am trying to make a bubble plot where each color represents a different coral species code, and the size of the bubble represents the size of an individual in meters but is also scaled to either the x or the y-axis which are also scaled in meters.
I'd also like to add an additional legend for the size of individual I am currently using plotly
in R but would be open to using other modules in R or Python.
I've been able to get the color by species relatively easily but am struggling with scaling of the bubbles by size. Has anyone done this before or know any cheats to get it to work?
#Example Data
Species <- c('SSID','PAST','CNAT','SSID','MMEA','PAST')
Dist <- c(7.1,4.0,6.4,8.0,8.1,8.9)
XDist <- runif(6, 0.0, 1.0)
Transect <- c(1,2,1,1,3,2)
Width <- c(10,15,100,45,60,27)
Data <- data.frame(Transect, Species, Dist, XDist, Width)
XDist <- Data$Transect - 1
Data$XDist <- Data$XDist + XDist
library(plotly)
k <- plot_ly(Data, x = ~XDist, y = ~Dist, type = 'scatter', mode =
'markers',
size = ~Width ,marker = list(sizemode = 'diameter', opacity = 1,
symbol = ifelse(data$Disease == 'Y', "circle-open", "circle"),
line = list(width = 5)),
color = ~Species, colors = 'Set1',
hoverinfo = 'text',
text = ~paste('Width:', Width, '<br>Species:', Species)
) %>%
layout(title = 'Coral',
xaxis = list(showgrid = FALSE),
yaxis = list(showgrid = FALSE))
After much thought and finding a pair of calipers I think I've answered my own question. Assuming a width = 1000 and height = 1000, 100 cm on my plot = 3.72 mm. Plotly seems to scale the bubbles based on the sizes of the largest bubble and the smallest bubble. The largest bubble on the default size is 2.45mm and the smallest is 0.21 mm. You can create a ratio using these measurements to scale the bubbles absolutely to distance.
#Data
Species <- c('SSID','PAST','CNAT','SSID','MMEA','PAST')
Coral <- c(1,2,3,4,5,6)
Dist <- c(1,2.4,4.6,3.2,1.2,4.1)
XDist <- c(2,3,0.5,2.3,4.1,2.5)
Transect <- c(1,2,1,1,3,2)
Width <- c(10,15,100,45,60,27)
Disease <- c(0,0,0,1,0,0)
Data <- data.frame(Coral, Transect, Species, Dist, XDist, Width, Disease)
Datamax <- (((max(Data$Width, na.rm = T)*3.72)/100)/2.45)*100
Datamin <- (((min(Data$Width, na.rm = T)*3.72)/100)/0.21)*10
k <- plot_ly(Data, x = ~XDist, y = ~Dist, type = 'scatter',
mode ='markers',
size = ~Width ,marker = list(sizemode = 'diameter', opacity = 0.75,
symbol = ifelse(Data$Disease == 1, "circle-open", "circle"),
line = list(width = 10)),
color = ~Species, colors = 'Set1',
hoverinfo = 'text',
text = ~paste('Width:', Width, '<br>Species:', Species),
width = 1000,
height = 1000,# ) %>%
sizes = c(Datamin,Datamax)) %>%
add_annotations(text = Data$Coral,
xanchor = 'center', showarrow = F) %>%
layout(title = 'Coral',
xaxis = list(showgrid = TRUE,
range = c(-0.25,5.25),
zeroline = T,
showline = T,
mirror = "ticks",
gridcolor = toRGB("grey50"),
gridwidth = 2),
yaxis = list(showgrid = FALSE,
scaleanchor = "x",
rangemode = "nonnegative"))
k
export(k,file = "Test.CoralMap.png")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With