English 中文(简体)
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.

最佳回答

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

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签