Skip to content

Instantly share code, notes, and snippets.

@ryanburge
Last active April 3, 2017 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanburge/1947379ce719f06605589e8d63c3322e to your computer and use it in GitHub Desktop.
Save ryanburge/1947379ce719f06605589e8d63c3322e to your computer and use it in GitHub Desktop.
Instructions for 4/3/2017
## RUN ALL THIS SYNTAX BEFORE WE START
## This will install and load all the packages you need for class today.
ipak <- function(pkg){
new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if (length(new.pkg))
install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}
packages <- c("nlme", "ggplot2", "dplyr", "car", "ggmap", "leaflet", "rvest", "xml2")
ipak(packages)
## Let's work on doing some mapping today in R.
## Here's a link to a dataset:
conflict <- read.csv("https://raw.githubusercontent.com/ryanburge/pls2003_sp17/master/conflict.csv")
## Here's a link to the codebook:
https://github.com/ryanburge/pls2003_sp17/raw/master/Conflict_Site_Dataset_Codebook.pdf
## The package we are going to work with is called leaflet and here is the basic structure:
leaflet()%>%
addTiles()%>%
addMarkers(lng = LONGITUDE HERE, lat= LATITUDE HERE)
## Here are ways to make tweaks to your leaflet map
Take out the addTiles() line and replace it with: addProviderTiles(providers$Stamen.Toner)
## Here's a link to other map options that are available: https://rstudio.github.io/leaflet/basemaps.html
## You can also add a popup, so that when a user clicks that will get additional information
## Just add:
, popup = VARIABLE TO DISPLAY inside the addMarkers() function above
## Let's say that you wanted to change the markers to dots
## Simply change
addMarkers() to add addCircleMarkers()
## Then, add the following to the inside of the addCircleMarkers() function
, radius = RADIUS VARIABLE
## Now, onto something else
## Let's say that you have a column that just contains the names of a location but you do not have the long or lat coordinates
## Load this data first:
geo <- data.frame("location" =c("One World Trade NYC", "Willis Tower", "Mar-a-Lago", "Corn Palace", "Eastern Illinois University", "Apple Headquarters", "Area 51", "Big Sur", "Rodeo Drive", "Mile High Statdium"),
count = c(11,2,13,55,21,6,23,98,10,4), stringsAsFactors = FALSE)
## Take a look at what you have, so far
## How we are going to use a function called geocode()
coords <- geocode(VARIABLE YOU WANT GEOCODED)
## This is going to hit the Google Maps API, search the name of each location, and then return a dataset of longitude and latitude.
## It will take a few seconds, but when you are done check out your new dataset
## We need to combine our two datasets however
## Here's how to do that, the cbind function which stands for "column bind"
cbind(FIRST DATASET, SECOND DATASET)
## Now you can map this!!
## Go here:
https://en.wikipedia.org/wiki/List_of_rallies_for_the_Donald_Trump_presidential_campaign,_2016
## See that table, let's scrape that. Then map those locations.
url <- "PUT THE URL HERE"
trump <- url %>%
read_html() %>%
html_nodes(xpath='PUT THE XPATH HERE') %>%
html_table()
trump <- trump[[1]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment