Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set marker size in plotly

Tags:

r

plotly

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
  )
like image 768
moman822 Avatar asked Oct 04 '16 18:10

moman822


People also ask

How do you change markers in plotly?

Set the marker_symbol attribute equal to that name or number to change the marker symbol in your figure.

What is PX scatter?

With px. scatter , each data point is represented as a marker point, whose location is given by the x and y columns. # x and y given as array_like objects import plotly.express as px fig = px.

How many points can plotly handle?

Due to browser limitations, the Plotly SVG drawing functions have a hard time graphing more than 500k data points for line charts, or 40k points for other types of charts.

How do you make a scatter plot plotly?

The syntax to create a scatterplot with Plotly Express is fairly simple. In the simple case, you simply call the function as px. scatter, provide the name of the dataframe you want to plot, and them map variables to the x and y axes. Note that this assumes that you've imported Plotly Express as px .


1 Answers

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

like image 161
dww Avatar answered Oct 29 '22 02:10

dww