This R package wraps d3-cartogram in a reusable Shiny component. The concept and R Shiny binding implementation borrows heavily from leaflet-shiny.
cartogram.js is a JavaScript implementation of an algoritm to construct continuous area cartograms, by James A. Dougenik, Nicholas R. Chrisman and Duane R. Niemeyer, ©1985 by the Association of American Geographers.
This example replicates the original demo that Shawn Allen made for his d3-cartogram. This R package has made it possible for you to only focus on providing the dataset and column definitions rather than writing boilerplate codes that creates the map and associated visual updates.
Use the two functions cartogramOutput()
and createCartogram()
from your Shiny
app to create cartogram maps.
# in ui.R cartogramOutput(outputId, width = "100%", height = "500px", translateX = 0, translateY = 0, scale = 1, topojson = "cartogram/data/us-states-segmentized.topojson", colors = rev(brewer.pal(3, "RdYlBu"))) # in server.R createCartogram(session, outputId)
TopoJSON is an extension of GeoJSON that encodes topology.
We use TopoJSON here to define boundaries of geographic regions and their names.
You can find built-in .topojson files at system.file("www/data", package = "shinyCartogram")
.
There is also a Makefile that helps you convert .geojson files into .topojson.
Currently we use albersUsa as default map projection.
TODO: We intend to open up API to customize/update map projections. This allows you to arrange tiles in different ways. If you have such need, please let me know so we can better understand the best way to expose this ability.
Color string^[https://github.com/mbostock/d3/wiki/Colors] can be:
You can pass in:
The following are methods that can be called on the map object that is created
using createCartogram()
. For example:
map <- createCartogram(session, "myMap") map$setView(0, 0, 1.5)
Below is a list of currently available methods:
library(shinyCartogram) res <- capture.output(str(unclass(createCartogram(list(output = list()), 'foo')))) cat(gsub(':function ', '', res[-1]), sep = '\n')
Detailed documentation for all methods:
setView(x, y, scale, forceReset = FALSE)
Sets the transalte offsets and zoom level of the map. forceReset
will completely reset
the map view.
setData(data)
Sets the underlying dataset that cartogram reads scale and color variable from.
data
needs to be a data.frame and must contains a name
column,
which will be used to join with map tile ids. (e.g. state names)
setColumns(columns)
Sets the column definitions. columns
needs to be a data.frame and must at least
contain name
column and can have optional title
and format
column which define
the title and format used in tooltip.
columns
are not mandatory and if not supplied title
will defaults to column name
and no number format will be applied.
Number format must be character vector and can either contain
scaleBy(column)
Sets the column used to scale area.
If no scale column exists but a valid color column is selected, then original tile area size is preserved. In this case, this is equivalent to a choropleth map.
colorBy(column)
Sets the column used to scale color used to fill area.
If no color column exists but a valid scale column is selected, then that scale column will also be used to color map tile using its values.
TODO: We plan to add click and hover events for map tiles
Population data is from US Census Bureau:
Location data is from Natural Earth:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.