6

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.

Reproducible example:

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
  )
moman822
  • 1,904
  • 3
  • 19
  • 33

2 Answers2

9

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)

enter image description here

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

enter image description here

dww
  • 30,425
  • 5
  • 68
  • 111
  • @moman822 did this solve your problem btw? feel free to upvote and/or accept this solution if it was helpful. – dww Oct 09 '16 at 15:08
  • Hi, is there a way to plot the size as a legend so it indicates an understanding meaning of the scaling/size? – Ben Nov 17 '17 at 17:59
  • @Ben you should ask that as a new question – dww Nov 18 '17 at 03:10
  • Probably, but after a question a week ago that caused 6 down votes I'm not able to ask questions anymore.. :( – Ben Nov 19 '17 at 10:42
  • 1
    @Ben - its a good question though, so I asked it for you - here https://stackoverflow.com/questions/47383244/size-legend-for-plotly-bubble-map – dww Nov 19 '17 at 23:44
3

The problem with using size is that you're literally specifying an exact size for all of the markers.

OTOH you can use the sizes parameter to proportionally scale the mapping between the base sizes and pixels.

For example:

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
  )

This is what the map looked like before the scaling (i.e. using the code in your question):

enter image description here

This is what the map looks like after the scaling (i.e. the map from the code above):

enter image description here

Of course, I chose the specific values pretty arbitrarily and you might need to tweak it. I'm also thinking that it might be advantageous to specify different colors for A and B.

Here's an example where I use colors to help increase the visual distinction between A and B while keeping the scaling proportional:

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

another variation:

enter image description here

and another:

enter image description here

Hack-R
  • 22,422
  • 14
  • 75
  • 131