在情节中设置标记大小标记、情节、大小

2023-09-06 07:32:08 作者:中指朝天一声操

如何在地图上的 R 中更改标记大小?如果我将 size 参数设置为任何数字,它会使其相同,太大.如果我将它映射到我的数据中的一个变量,标记太小以至于真正能够首先分辨出差异.理想情况下,我想通过映射到变量来增加基本大小并保持比例方面.

How can I change the marker size in plotly in R on a map? If I set the size argument to any number it makes it the same, too big size. And if I map it to a variable in my data, the markers are to small to really be able to tell the difference in the first place. Ideally I would like to increase the base size and keep the proportional aspect through mapping to the variable.

可重现的例子:

library(data.table)
library(plotly)
library(dplyr)

sample <- data.table(Region=c("Illinois","Illinois","California","California","Texas","Texas"),
                     code=c("IL","IL","CA","CA","TX","TX"),
                     Group=c("A","B"),
                     Value=rnorm(6, mean=100, sd=6))

sample[Region=="Illinois", c('lat', 'long') := list(40.3363, -89.0022)]
sample[Region=="California", c('lat', 'long') := list(36.17, -119.7462)]
sample[Region=="Texas", c('lat', 'long') := list(31.106, -97.6475)]


x <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = F,
  lakecolor = toRGB('lightblue')
)

sample %>%
  plot_geo(
    locationmode='USA-states'
  ) %>%
  add_markers(
    y=~lat, x=~long, hoverinfo="text",
    color=~Group,
    text=~Group, size=~Value
  ) %>%
  layout(
    title='plotly marker map',
    geo=x
  )

推荐答案

最简单且可能是规范的方法是使用 marker.sizeref 属性.你像这样把它包裹在 marker=list(...) 里面

The simplest, and probably canonical way is to use the marker.sizeref attribute. You wrap this inside marker=list(...) like this

plot_geo(sample, locationmode='USA-states') %>%
  add_markers(y=~lat, x=~long, hoverinfo="text",
    color=~Group, text=~Group, size=~Value, 
    marker=list(sizeref=0.1, sizemode="area")) %>%
  layout(title='plotly marker map', geo=x)

请注意,sizeref 越小,标记越大.例如,使用 sizeref=0.02 我们得到

Note that, the smaller sizeref, the bigger the markers. E.g with sizeref=0.02 we get