简体   繁体   English

图表气泡图/图表的大小图例

[英]size legend for plotly bubble map/chart

Here is a plotly "bubble" map (ie 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'))

在此输入图像描述

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. 上面的例子使用R api,但使用另一个plotly api(例如python)的答案也是可以接受的。

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. 链接问题中的OP想知道如何摆脱图例中的不同大小 - 而不是如何将值映射到图例中的大小。 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))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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