-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet.Rmd
95 lines (73 loc) · 3.49 KB
/
leaflet.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---
title: "playingWithLeaflet"
author: "Kristin Robinson"
date: "April 30, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(stringsAsFactors = FALSE)
library(devtools)
library(leaflet)
library(dplyr)
library(ggplot2)
library(rjson)
library(jsonlite)
library(rgdal)
library(tidytext)
library(stringr)
```
```{r}
# load data - longitude and latitude already transformed
twitter_data <- read.csv("data/twitter_31days.csv")
twitter_data$Timestamp<-as.POSIXct(twitter_data$Timestamp)
## manipulate long & lat into usable form ##
# split coordinates into two columns and remove special characters
twitter_data$lon <- sapply(strsplit(as.character(twitter_data$Coordinates),','), "[", 1)
twitter_data$lon <- (sub('^\\[', '', twitter_data$lon)) %>% as.numeric(twitter_data$lon)
twitter_data$lat <- sapply(strsplit(as.character(twitter_data$Coordinates),','), "[", 2)
twitter_data$lat <- (sub(']$', '', twitter_data$lat))
# cast strings as num
twitter_data$lon <-as.numeric(twitter_data$lon)
twitter_data$lat <-as.numeric(twitter_data$lat)
twitter_data <- subset(twitter_data,
twitter_data$Timestamp >= ('2014-08-24 04:20:44') &
twitter_data$Timestamp <= ('2014-09-24 04:20:43') )
# set eruption date and time
day1 <- as.Date("2014-08-24")
hour1 <- as.POSIXct("2014-08-24 04:20:44")
# add columns for elapsed days
twitter_data$days <- as.numeric(difftime (as.Date(twitter_data$Timestamp), day1, units = "days"))
twitter_data$days2 <- as.numeric(difftime(twitter_data$Timestamp, hour1, units = "days"))
# add columns for elapsed minutes
twitter_data$minutes <- as.numeric(difftime(twitter_data$Timestamp, hour1, units = "mins"))
twitter_data$seconds <- as.numeric(difftime(twitter_data$Timestamp, hour1, units = "secs"))
# Subset twitter data to 10 minutes post earthquake
twitter_data_10min <- subset(twitter_data,
twitter_data$Timestamp >= ('2014-08-24 04:20:44') &
twitter_data$Timestamp <= ('2014-08-24 04:30:43') )
# clean up text and break down tweets by word
replace_reg <- "https://t.co/[A-Za-z\\d]+|http://[A-Za-z\\d]+|&|<|>|RT|https"
unnest_reg <- "([^A-Za-z_\\d#@']|'(?![A-Za-z_\\d#@]))"
tidy_tweets <- twitter_data_10min %>%
mutate(Text = str_replace_all(Text, replace_reg, "")) %>%
unnest_tokens(word, Text, token = "regex", pattern = unnest_reg) %>%
filter(!word %in% stop_words$word,
str_detect(word, "[a-z]"))
```
```{r cars}
leaflet(tidy_tweets) %>%
addTiles() %>%
addCircleMarkers(lng=~lon, lat=~lat,
popup=~paste0(word))
```
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r}
CalCen <- readOGR("data/CaliCensus/Cal_Cnty_RacePop.shp")
leaflet(CalCen) %>% addProviderTiles(providers$OpenMapSurfer.Roads) %>% addAwesomeMarkers( clusterOptions = markerClusterOptions()
,twitter_data_10min, lat=twitter_data_10min$lat, lng=twitter_data_10min$lon, popup=~twitter_data_10min$Text) %>% addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,opacity = 1.0, fillOpacity = 0.5, fillColor = ~colorQuantile("YlOrRd", CalCen$Total)(CalCen$Total), popup=paste0("<b>Total County Population: </b>", CalCen$Total)
,highlightOptions = highlightOptions(color = "white", weight = 2, bringToFront = TRUE))
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.