Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
894 views
in Technique[技术] by (71.8m points)

python - size legend for plotly bubble map/chart

Here is a plotly "bubble" map (i.e. a map with markers on it, whose size is mapped to a variable). However, the legend only shows the color categories, but does not show how size relates to value.

library(plotly)

DF = data.frame(
  Group = c("A",  "B",  "A",  "B", "A", "C", "C"), 
  Value = c(100,  80,   90,  150, 120,  60, 110), 
  lat =  c( 40,   32,   36,   44,  31,  39,  37), 
  long = c(-90, -100, -120, -110, -90, -80,-105))


plot_geo(DF, locationmode = 'USA-states') %>%
  add_markers(y=~lat, x=~long, color=~Group, size=~Value, 
    marker=list(sizeref=0.1, sizemode="area")) %>%
  layout(geo=list(scope = 'usa'))

enter image description here

This question shows how to control the size of markers, but does not answer how to show these sizes in the legend. In this and this questions, we can see that if each category has only a single marker size associated with it, then the legend will show markers scaled to the size they are in the bubble plot. But that does not help here. The plotly website has examples of bubble charts and bubble maps, but none of these have a size legend.

Is there a way to add a legend for marker sizes to bubble charts/maps in plotly? The examples above use the R api, but answers using another plotly api (such as python) will also be acceptable.

Edit: Why this is not a duplicate of this question

I had already linked to the question in my original post and explained why it was different. but let me try to explain the difference a little more clearly, since someone has marked it as a possible duplicate anyway...

The linked question relates to someone who was suffering from having the different bubble sizes shown in the legend which happened because they have only one size per category in their data. In contrast, the categories in this example each have bubbles of varying sizes. The OP in the linked question wanted to know how to get rid of the different sizes in the legend - not how to map a value to size in the legend. The answers in the linked question give workarounds of various quality to achieve that. But, in this question I already have a legend in which markers are all the same size. What I want is to add a legend which shows bubbles of a range of sizes each labeled with the value to which that size corresponds. nothing in the linked post asks or shows how to achieve that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After seeing a few comments on the question suggesting that this can't be done, I had another go a it myself, and here is one approach that works pretty nicely.

legend.sizes = seq(80, 160, 20)
ax = list(zeroline = FALSE, showline = FALSE, showticklabels = FALSE, showgrid = FALSE)
mk = list(sizeref=0.1, sizemode="area")

p.map = plot_geo(DF, locationmode = 'USA-states') %>%
  add_markers(x = ~long, y = ~lat, color = ~Group, size = ~Value, marker = mk) %>%
  layout(geo = list(scope = 'usa'))

p.legend = plot_ly() %>%
  add_markers(x = 1, y = legend.sizes, size = legend.sizes, showlegend = F, marker = mk) %>%
  layout(xaxis = ax, yaxis = list(showgrid = FALSE))

subplot(p.legend, p.map, widths = c(0.1, 0.9))

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...