12/16/2020

New Slide

Some content

My title

  • Bullet 1
  • Bullet 2
  1. Item a
  2. Item b

A top level subheading

A second level subheading

Formatting Options

Plain Text

Italicized text

Bold Text

for (i %in% 1:10)

Some R codes that work

head(mtcars)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

I don’t like the hash sign at the beginning of the rows. comment="", removes those signs

Some R codes that work1

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

That worked

Let’s not echo the codes

My Awesome Figure

My Awesome Figure

Publishing the Slides

(GitHub is probably the best one to do so, but takes a little time to set up)

Leaflet Codes and Examples

An important aspect to developing data products is having a way to create interactive maps. Leaflet is a JavaScript library, and then there is an associated Leaflet package for R that makes use of this JavaScript library to create interactive maps within our R environment. And it’s especially convenient within RStudio because it brings it up in the RStudio window. So we’re just going to go ahead and go through some Leaflet examples and get you started with the bare minimum to get going on this, and I think you’ll get the extended rather quickly.

Let’s embed an interactive map using leaflet

library(leaflet)
my_map<-leaflet()%>% addTiles()#This can be written the following way

#my_map=leaflet()
#my_map=addTiles(my_map)

my_map

We see an interactive world map. We can zoom in and zoom out. If we zoom in we can see the countries, etc. The first part ‘leaflet’ just generates the map. And then the ‘addTiles’ is adding the first set of content.

Adding Markers

So that was pretty quick, we were able to actually generate a map, a JavaScript widget basically without actually knowing any JavaScript.So, just to reiterate the leaflet function we can think of is just actually creating like, if we’re familiar with Photoshop, it’s almost like creating a layer or something like that. Okay, it’s basically just creating the background, and then we have to add the actual content on top of it, so add tiles is adding specifically the mapping data from Open Street Map.

But we probably actually want to do things like add markers, add some direct interactivity. And so the command to add a marker is really easy. It’s just add markers, and the first one we’re going to do is just we’re going to add one at a specific longitude and latitude. And we are going to give it a little label, so let’s do that.

Let’s Add Markers

library(leaflet)
my_map<-leaflet() %>% 
  addMarkers(lat = 39.2980803, lng = -76.5898801,
             popup="Orlando, Florida, United States")
my_map

So remember, my_map and we redid it. My_map was already defined when we ran it from the code from the first set of slides. So, we’ve overwritten it back to where it’s just added the content with no markers.

Adding a Few More Markers

Adding one marker at a time is often not practical if you want to display many markers. If we have a data frame with columns latitude and longitudinal, we can pipe that data frame into leaflet() to add all the points at once.

set.seed(2020-12-16)
df<-data.frame(lat=runif(20, min=39.2, max=39.3),
               lng=runif(20, min=-76.6, max=-76.5))
df %>%
  leaflet()%>%
  addTiles()%>%
  addMarkers()

Adding Several Markers and PopUps

hopkinsIcon<-makeIcon(
  iconUrl="http://brand.jhu.edu/content/uploads/2014/06/university.shield.small_.blue_.png",
  iconWidth = 31*215/230, 
  iconHeight = 31,
  iconAnchorX = 31*215/230/2,
  iconAnchorY = 16
)
hopkinsLatLog<-data.frame(
  lat=c(39.2973166, 39.3288851, 39.2906617, 39.2970681, 39.2824806),
  lng=c(-76.5929798, -76.6206598, -76.5469683, -76.6150537, -76.6016766))

hopkinsLatLog %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon=hopkinsIcon)

Now lets add links

hopkinsSites<-c(
  "<a herf='http://www.jhsph.edu/'>East Baltimore Campus</a>",
  "<a herf='https://apply.jhu.edu/visit/homewood'>Homewood Campus</a>",
  "<a herf='http://www.hopkinsmedicine.org/johns_hopkins_bayview/'>Bayview Medical Center</a>",
  "<a herf='http://www.peabody.jhu.edu/'>Peabody Institute</a>",
  "<a herf='http://carey.jhu.edu/'>Carey Business School</a>"
)
hopkinsLatLog %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(icon=hopkinsIcon, popup=hopkinsSites)

Mapping Clusters

We are going to define a data frame with 500 close-knit latitude and longitude points. We then, take my data frame, and pass it as the argument to leaflet; then pass the output of the leaflet evaluated at the data frame to the addTiles command, and pass the output of that to the addMarkers command with default cluster options.

df<-data.frame(lat=runif(500, min=39.25, max=39.35), lng=runif(500, min=-76.65, max=-76.55))
df %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers(clusterOptions=markerClusterOptions())

Here we go. We see the cluster in it. It gives us the number of points in this cluster. So let’s look at this big one.

Creating Circle Markers

Instead of adding clusters or standard pop-ups, we can add circles. And the way to do that is just add circles markers in our syntax. In this example, we’re going to show a couple of 20 markers, and then, circle markers.

df1<-data.frame(lat=runif(20, min=39.25, max=39.35), lng=runif(20, min=-76.65, max=-76.55))
df1 %>%
  leaflet() %>%
  addTiles() %>%
  addCircleMarkers()

There they are. They look like little circles.

We may digitally draw shape on the map. Let’s learn how to do them.

1. Drawing Circles

We can draw arbitrary shapes on the maps we create, including circles and squares. The code below draws a map where the circle on each city is proportional to the population of that city.

md_cities<-data.frame(name=c("Balitmore", "Fredric", "Rockville", "Gaithersburg", "Bowie", "Hagerstown", "Annapolis", "College Park", "Salisbury", "Laurel"),
pop=c(619493, 66169, 62334, 61045,55232, 39890,38880, 30587, 30484,25346),
lat=c(39.2920592, 39.4143921, 39.0840, 39.1434, 39.0068, 39.6418, 38.9784, 38.9897, 38.3607, 39.0993),
lng=c(-76.6077852, -77.4204875, -77.1528, -77.2014, -76.7791, -77.7200, -76.4922, -76.9378, -75.5994, -76.8483))
md_cities %>%
  leaflet() %>%
  addTiles() %>%
  addCircles (weight=1, radius=sqrt(md_cities$pop)*30)
## Assuming "lng" and "lat" are longitude and latitude, respectively

So we have the cities, their names are Baltimore, Frederick, Rockville, Gaithersburg, these are all cities right around Baltimore. Okay, and it gives us the population. Okay, and it gives us the latitude and the longitude. So, now we’re going to take our cities dataset, we’re going to pass it to leaflet, we’re going to pass the output of that to add tiles.

2. Drawing Rectangles

leaflet() %>%
  addRectangles(lat1=37.3858, lng1=-122.0595,
                lat2=37.3890, lng2=-122.0625)

We just gave two corners for a rectangle.

3. Adding Legends

Adding a legent can be useful if we have markers on our map with different colors

df2<- data.frame(lat=runif(20, min=39.25, max=39.35),
lng=runif(20, min=-76.65, max=-76.55),
col=sample(c("red", "blue", "green"), 20, replace=TRUE),
stringsAsFactors=FALSE)
head(df2)#lets check the structure of the data frame
##        lat       lng   col
## 1 39.29962 -76.60759 green
## 2 39.26837 -76.61192 green
## 3 39.29603 -76.60671   red
## 4 39.25845 -76.55381  blue
## 5 39.32085 -76.56042   red
## 6 39.28645 -76.56097   red

Map Here

df2 %>%
leaflet() %>%
addTiles() %>%
addCircleMarkers(color=df2$col) %>%
addLegend(labels=LETTERS[1:3], colors=c("blue", "red", "green"))
## Assuming "lng" and "lat" are longitude and latitude, respectively