气泡图/图表的大小图例 [英] size legend for plotly bubble map/chart

查看:1085
本文介绍了气泡图/图表的大小图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个可绘制的气泡"图(即上面有标记的图,其大小映射到一个变量).但是,图例仅显示颜色类别,而没有显示尺寸与值之间的关系.

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'))

此问题显示了如何控制标记的大小,但没有回答如何在图例中显示这些尺寸.在这个问题,我们可以可以看到,如果每个类别仅具有一个与之关联的标记大小,则图例将显示缩放至气泡图中其大小的标记.但这对这里没有帮助.情节严重的网站上有气泡图

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.

是否可以将标记大小的图例添加到绘图中的气泡图/地图?上面的示例使用了R api,但是使用其他plotly api(例如python)的答案也是可以接受的.

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.

我已经在原始帖子中链接了该问题,并解释了为什么它与众不同.但让我尝试更清楚地解释这种差异,因为有人将其标记为可能的重复...

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...

链接的问题与某人患有图例中显示的气泡大小不同而发生的情况有关,这是因为他们的数据中每个类别只有一个大小.相反,此示例中的类别每个都有大小不同的气泡.链接问题中的OP想要知道如何摆脱图例中的不同大小-不是如何将值映射到图例中的大小.链接问题中的答案给出了解决此问题的各种方法.但是,在这个问题中,我已经有了一个传说,其中标记的大小都相同.我想要添加一个图例,该图例显示一定范围大小的气泡,每个气泡都标有该大小所对应的值.链接的帖子中没有任何内容询问或说明如何实现.

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))

这篇关于气泡图/图表的大小图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆