在图中设置标记大小

时间:2016-10-04 18:49:13

标签: r plotly

如何在地图上的R中以图形方式更改标记大小?如果我将size参数设置为任何数字,它会使它变得相同,大小太大。如果我将它映射到我的数据中的变量,那么标记就会很小,以便能够首先区分它们。理想情况下,我希望通过映射到变量来增加基本大小并保持比例方面。

可重复的例子:

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
  )

2 个答案:

答案 0 :(得分:5)

最简单,也许是规范的方法是使用marker.sizeref属性。你把它包装在marker=list(...)里面就像这样

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)

enter image description here

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

enter image description here

答案 1 :(得分:3)

使用size的问题在于您确实为所有标记指定了确切的大小。

OTOH您可以使用sizes参数按比例缩放基本尺寸和像素之间的映射。

例如:

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(
    sizes=c(1000,100),
    y=~lat, x=~long, hoverinfo="text",
    color=~Group,
    text=~Group, size=~Value
  ) %>%
  layout(
    title='plotly marker map',
    geo=x
  )

这就是地图在缩放之前的 >(即使用问题中的代码):

enter image description here

这是地图在之后的缩放(即上面代码中的地图):

enter image description here

当然,我选择了任意的特定值,你可能需要调整它。我还认为为AB指定不同的颜色可能更有利。

这是一个示例,我使用颜色来帮助增加AB之间的视觉区别,同时保持比例缩放:

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

enter image description here

另一种变化:

enter image description here

和另一个:

enter image description here