library(knitr) #opts_chunk$set(cache=TRUE, cache.path = 'DocumentName_cache/', fig.path='figure/')
pdquickmap
is a small tool to make it easier to visualize geographic data.
If you start out with a data set that includes addresses, pdquickmap
has
functions that can append geocodes to the data and create a map in the
javascript library leaflet.js. These maps can be saved as HTML files and sent to clients.
To get started, you'll need to have devtools
installed in order to install
packages from GitHub, and you'll need to instal a few other packages:
install.packages(c("devtools", "htmlwidgets", "htmltools", "jsonlite")) devtools::install_github('tarakc02/pdquickmap')
And finally, you'll need to load the pdquickmap
library:
library(pdquickmap)
Now you're ready to go! Let's start with a sample data frame that's included, called urel
:
data(urel)
urel
This is a pretty common setup, whether the data has come from a report or webi
or what have you. Importantly, notice that we have address information, but no geocodes for our data. The address fields are street
, city
, state
, and zip
. In order to map these addresses, we'll need to add geocodes using append_geocode
. In addition to the entire data frame, we need to let the function know which of the fields are the address fields. We do this by listing the names of the fields:
cols = c("street", "city", "state", "zip") geo_data <- append_geocode(urel, cols) geo_data
Now we're ready to map, and for that we just need to run make_leaflet
:
make_leaflet(geo_data)
Note the way that make_leaflet
automatically added the data from the data frame into the popups on the map, so that whe you click on a dot you see the name, address, and so on (the "latitude" and "longitude" columns are automatically excluded). You may not want every single column of your data frame to be part of the popups on the map, though, and you can control this behavior by selecting the columns from your data that should be in the pop-up, using popup_columns
:
make_leaflet(geo_data, popup_columns = c("names", "giving"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.