Skip to contents

Create a Map

Usage

vmap(
  data,
  mapping = NULL,
  ...,
  projection = NULL,
  width = NULL,
  height = NULL,
  elementId = NULL
)

Arguments

data

An sf object.

mapping

efault list of aesthetic mappings to use for map.

...

Configuration options for the map.

projection

Geographical mapping type. See online documentation for the various possible choices.

width

Fixed width for widget (in css units). The default is NULL, which results in intelligent automatic sizing based on the widget's container.

height

Fixed height for widget (in css units). The default is NULL, which results in intelligent automatic sizing based on the widget's container.

elementId

Use an explicit element ID for the widget (rather than an automatically generated one). Useful if you have other JavaScript that needs to explicitly discover and interact with a specific widget instance.

Value

A vmap() htmlwidget object.

Examples


if (rlang::is_installed(c("sf", "geojsonio"))) {
  
  library(vchartr)
  library(sf)
  
  # Create map from sf object
  vmap(co2_world)
  
  # Draw data on the map
  vmap(co2_world, aes(name = name, fill = co2_per_capita))
  
  # Change projection and colors
  vmap(
    co2_world,
    aes(name = name, fill = co2_per_capita), 
    projection = "equalEarth"
  ) %>% 
    v_specs_colors(
      range = c(
        "#FFFFE5", "#FFF7BC", "#FEE391", 
        "#FEC44F", "#FE9929", "#EC7014", 
        "#CC4C02", "#993404", "#662506"
      )
    ) %>% 
    v_specs_legend(
      orient = "bottom",
      type = "color",
      field = "fill"
    )
  
  # Map discrete data
  vmap(
    co2_world[!is.na(co2_world$co2_per_capita), ], 
    aes(
      name = name,
      fill = ifelse(co2_per_capita >= 2.3, "Above", "Under")
    )
  ) %>% 
    v_specs(
      area = list(
        style = list(
          stroke = "#FFFFFF"
        )
      )
    )
  
}
#> Registered S3 method overwritten by 'geojsonsf':
#>   method        from   
#>   print.geojson geojson
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE
#> sf/sfc/sfg class detected; using type="auto"
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> sf/sfc/sfg class detected; using type="auto"
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> sf/sfc/sfg class detected; using type="auto"
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> sf/sfc/sfg class detected; using type="auto"
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi
#> Loading required namespace: stringi