-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathatmpt2_werise.Rmd
54 lines (36 loc) · 1.94 KB
/
atmpt2_werise.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
---
title: "App to Map Tutorial Part 2"
output: html_notebook
---
<h1>App to Map Tutorial Part 2: Mapping Your Tweets on a Leaflet Map</h1>
Now that you have some data, you can now display it in a map. The particular R map package we will be using is <a href = "https://rstudio.github.io/leaflet/">Leaflet</a>. Leaflet is a popular JavaScript based library in which you interactive maps. If you have used OpenStreetMap, you have seen the Leaflet interface before. For this tutorial, we will make only a basic map using Leaflet. One thing worth mentioning is that Leaflet uses the <a href = "https://github.com/smbache/magrittr">magrittr</a> pipe operator (%>%) which makes it easier to write code because the syntax is more natural. It might seem strange at first, but it does cut down on the amount of work you would have to do when writing code.
<h3>Tutorial Objective:</h3>
<i>1. To add collected Twitter data to a Leaflet map.</i>
<h3>Instructions</h3>
<b>1. Open up R Studio and go to File > New File > R Script.
<b>2. Install and load these packages:</b>
```{r}
#install these packages
install.packages("leaflet")
install.packages("maps")
install.packages("magrittr")
#load these packages
library (leaflet)
library (maps)
library (magrittr)
```
<b>3. Now you need a way for Leaflet to access your data:</b>
```{r}
mymap <- read.csv("C:\Users\YourName\Documents\ApptoMap\tweets.csv",
stringsAsFactors = FALSE)
```
<b>4. Now it's time to make a Leaflet map! We are going to use the OpenStreetMap base map for
our map and add the tweets to this base map.</b>
```{r}
leaflet(mymap) %>%
addTiles() %>% #loading OpenStreetMap basemap
addCircles(lng = ~lon, lat = ~lat, popup = mymap$type, weight = 8, radius = 40, color = "#fb3004",
stroke = TRUE, fillOpacity = 0.8) #adding the tweets to the map.
```
<b>5. Click "Run." </b>
<b>6. When you run your code, a web browser should pop-up and display your map.</b>