Skip to content

Instantly share code, notes, and snippets.

@sabineri
Created February 17, 2020 17:25
Show Gist options
  • Save sabineri/12bb0c5fff39804a06af406e09ebe0ed to your computer and use it in GitHub Desktop.
Save sabineri/12bb0c5fff39804a06af406e09ebe0ed to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://www.bigdatauniversity.com\"><img src = \"https://ibm.box.com/shared/static/wbqvbi6o6ip0vz55ua5gp17g4f1k7ve9.png\" width = 300, align = \"center\"></a>\n",
"\n",
"\n",
"<h1 align=center><font size = 5>Maps</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"<li><p><a href=\"#ref0\">Creating a map</a></p></li>\n",
"<li style=\"margin-left: 40px;\"><p><a href=\"#ref1\">Basic settings</a></p></li>\n",
"<li style=\"margin-left: 40px;\"><p><a href=\"#ref2\">Adding base map</a></p></li>\n",
"<li><p><a href=\"#ref3\">Adding markers</a></p></li>\n",
"<li><p><a href=\"#ref4\">Adding shapes</a></p></li>\n",
"<li><p><a href=\"#ref5\">Colors and legends</a></p></li>\n",
"<br>\n",
"<p></p>\n",
"Estimated Time Needed: <strong>20 min</strong>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Overview\n",
"\n",
"Among all the ways to visualize data, using maps is the best one if you have data that is related to spatial information, like the locations of the restaurants in a city.<br>\n",
"R has some packages that allow you to create the maps you always wanted, like maps (built-in), ggmaps and leaflet.<br>\n",
"Here, we will learn how to use the leaflet package."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"also installing the dependencies ‘gridExtra’, ‘crosstalk’, ‘png’, ‘raster’, ‘sp’, ‘viridis’, ‘leaflet.providers’\n",
"\n",
"Updating HTML index of packages in '.Library'\n",
"Making 'packages.html' ... done\n"
]
}
],
"source": [
"install.packages(\"leaflet\")\n",
"library(leaflet)\n",
"#Packages used to display the maps in this notebook\n",
"library(htmlwidgets)\n",
"library(IRdisplay)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Files used in this notebook\n",
"Please run the code below to download the data sets into your workbench which we used in this notebook."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Warning message in download.file(\"https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt\", :\n",
"“cannot open URL 'https://ibm.ent.box.com/public/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt': HTTP status was '404 Not Found'”"
]
},
{
"ename": "ERROR",
"evalue": "Error in download.file(\"https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt\", : cannot open URL 'https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt'\n",
"execution_count": 2,
"output_type": "error",
"traceback": [
"Error in download.file(\"https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt\", : cannot open URL 'https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt'\nTraceback:\n",
"1. download.file(\"https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt\", \n . destfile = \"/resources/data/countries.txt\", quiet = TRUE)"
]
}
],
"source": [
"download.file(\"https://ibm.box.com/shared/static/9sfgqavy3bvr9epihw87p7x7vcpfywjn.txt\",\n",
" destfile = \"/resources/data/countries.txt\", quiet = TRUE)\n",
"download.file(\"https://ibm.box.com/shared/static/9u5rrzdlzalsbjewkmqqho4ohoe13g82.csv\",\n",
" destfile = \"/resources/data/energy.csv\", quiet = TRUE)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref0\"></a>\n",
"## <center>Creating a map<center>\n",
"\n",
"Basically, it takes three steps to make a map visualization:\n",
"1. Create a map widget\n",
"1. Customize your map settings and add content (layers) to the map widget using layer functions\n",
"1. Display and enjoy your map\n",
"\n",
"First, let's see how to create the map widget."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Creating a map widget\n",
"\n",
"The creation of a map widget is pretty simple and straight forward. You just need to call the function leaflet()."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"map <- leaflet()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After the function call, you will have an object that represents an empty world map: it will be an empty space that follows the same coordinate system used in all world maps.\n",
"\n",
"One interesting point about the leaflet() function is that it has an optional parameter called data. This parameter exists so you can pass spatial data to your map (like the coordinates of your town) and use it when adding the layers. If you pass a data frame as your source of spatial data, there are two ways of accessing and using that information in the layer functions: the first one is done automatically if your data frame has columns named as *lat/latitude* and *lng/long/longitude* (case insensitive); the second way is to explicitly declare the latitude and longitude values when callng the layer functions.\n",
"\n",
"Here is an example of a map widget creation with the built-in quakes data frame from R."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"map <- leaflet(quakes)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing the map\n",
"\n",
"Before talking about how to customize your maps, there is an interesting tool that you should discover first: the pipe operator %>% from magrittr package. Basically, it passes whatever is on its left side as the first parameter of the function that comes on its right side and then returns the result. When working with the leaflet package, the pipe operator is very useful, since most of the functions in this package need a map as its first parameter and return a new map. However, since the operator just returns the result of the function on its right, your new map will be displayed but will not be recorded unless you use an assignment statement, just like you do for other functions.\n",
"While studying the first customization functions, we will have examples both with and without the pipe operator but, later on, we will just use the pipe operator."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref1\"></a>\n",
"## <center> Basic settings</center>\n",
"\n",
"After creating your map, the first thing you can do is set the first view of your map and it can be done in two ways. <br>\n",
"##### Setting the first view\n",
"The first way is by calling the function setView(map, lng, lat, zoom), which will define the center of the first view and the zoom."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#Using the pipe operator\n",
"map <- leaflet() %>% setView(lng = 86.92, lat = 27.99, zoom = 5)\n",
"#Without the pipe operator\n",
"map <- leaflet()\n",
"map <- setView(map, lng = 86.92, lat = 27.99, zoom = 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Setting the first view using bounds\n",
"The second way is by setting the bounds of the first view by calling the function fitBounds(map, lng1, lat1, lng2, lat2). This will fit your first view inside the rectangle defined by the coordinates you gave as arguments."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"#Using the pipe operator\n",
"map <- leaflet() %>% fitBounds(86.8, 27.9, 87, 28)\n",
"#Without the pipe operator\n",
"map <- leaflet()\n",
"map <- fitBounds(map, 86.8, 27.9, 87, 28)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref2\"></a>\n",
"## <center> Adding base map</center>\n",
"Even after setting the first view, the map will still be empty, since we have not added a base yet. The most common way to do it is by calling the addTiles(map) function, which uses the OpenStreetMap by default."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map1.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Using default base map\n",
"map1 <- leaflet() %>% fitBounds(86.8, 27.9, 87, 28) %>% addTiles()\n",
"saveWidget(map1, file=\"map1.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map1.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also use third-party tiles by calling addProviderTiles(map, tile) (you can find some tiles here http://leaflet-extras.github.io/leaflet-providers/preview/index.html).<br> \n",
"It is also possible to combine multiple tiles by exploring the opacity parameter of those functions, so feel free to play with it."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map2.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Using a third-party base map\n",
"map2 <- leaflet() %>% fitBounds(86.8, 27.9, 87, 28) %>% addProviderTiles(\"Stamen.Watercolor\")\n",
"saveWidget(map2, file=\"map2.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map2.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref3\"></a>\n",
"## <center> Adding markers</center>\n",
"\n",
"It is possible to add markers on your map to highlight a special location. To do so, you just need to call the function addMarkers(map, lng, lat). If during the map creation you gave the spatial data, ALL that data will be automatically read and marked on the map if you don't specify the parameters *lng* and *lat*."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map3.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"map3 <- leaflet(quakes) %>% addTiles(\n",
" ) %>% addMarkers(lng = quakes$long[1:10],\n",
" lat = quakes$lat[1:10])\n",
"saveWidget(map3, file=\"map3.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map3.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Changing the markers\n",
"It is also possible to change the marker. You can call addCircleMarkers() to have circular markers or create your own markers using the function makeIcon(image_file, height, width) and passing it as the *icon* argument in the addMarkers() function."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map4.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Circle markers examples\n",
"map4 <- leaflet(quakes) %>% addTiles(\n",
" ) %>% addCircleMarkers(lng = quakes$long[1:10],\n",
" lat = quakes$lat[1:10])\n",
"saveWidget(map4, file=\"map4.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map4.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Popups\n",
"You can also enable popups for your markers so, when they are clicked, some content is displayed by passing this content as the popup argument in the addMarkers() call. "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map5.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"map5 <- leaflet() %>% fitBounds(86.8, 27.9, 87, 28) %>% addTiles(\n",
" ) %>% addMarkers(lng = 86.92, lat = 27.99, popup = \"Mount Everest\")\n",
"saveWidget(map5, file=\"map5.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map5.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Clustering markers\n",
"If we try to use all the quakes data, the map will become unreadble due to the huge number of markers in the same region. When this happens, you can call the clustreOptions parameter from add markers, which will group your markers by region and display the number of markers in each region."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Assuming 'long' and 'lat' are longitude and latitude, respectively\n"
]
},
{
"data": {
"text/html": [
"<iframe src=' map6.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Assuming 'long' and 'lat' are longitude and latitude, respectively\n"
]
},
{
"data": {
"text/html": [
"<iframe src=' map7.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Without clustering the markers\n",
"map6 <- leaflet(quakes) %>% addTiles() %>% addCircleMarkers()\n",
"saveWidget(map6, file=\"map6.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map6.html', \" ' width='100%' height='300'\",\"/>\"))\n",
"#Clustering the markers\n",
"map7 <- leaflet(quakes) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())\n",
"saveWidget(map7, file=\"map7.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map7.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref4\"></a>\n",
"## <center> Adding shapes </center>\n",
"Another possible way to customize your map is by adding shapes like circles, rectangles or any kind of polygon you can imagine. First, let's talk about adding circles. <br>\n",
"##### Circles\n",
"Circles can be added by calling the function addCircles() and they are very similar to the circle markers, with the only difference being that the circle markers have its radius given in pixels while the circles have its radius in meters, which mean that the circles are rescaled with the whole map while the markers have a constant size."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map8.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#Try changing the zoom to see the difference between circles and circle markers\n",
"map8 <- leaflet(quakes) %>% addTiles() %>% addCircleMarkers(lng = quakes$long[1:5],\n",
" lat = quakes$lat[1:5]) %>% addCircles(lng = quakes$long[5:10],\n",
" lat = quakes$lat[5:10], color = 'red')\n",
"saveWidget(map8, file=\"map8.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map8.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Rectangles\n",
"If you want to highlight an area, adding a rectangle may be what you need. To do so, just call the function addRectangles() and pass the coordinates of two points, which will be the delimiters of your rectangle."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map9.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"map9 <- leaflet() %>% addTiles() %>% addMarkers(lng = 86.92, lat = 27.99,\n",
" popup = \"Mount Everest\") %>% addRectangles(86.9, 27.95, 87, 28.05)\n",
"saveWidget(map9, file=\"map9.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map9.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Polygons with matrix\n",
"Now, if you want to add a polygon, you will have to call addPolygons() and give the polygon information. One way to give this information is by creating a matrix with rows (lng, lat) where each row correspond to a vertice of your polygon. If want more than one polygon, separate them in the matrix with a NA row."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map10.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tri <- matrix(c(86.87, 27.95, 86.97, 27.95, 86.92, 28.05), ncol = 2, byrow = TRUE)\n",
"map10 <- leaflet() %>% addTiles() %>% addMarkers(lng = 86.92, lat = 27.99,\n",
" popup = \"Mount Everest\") %>% addPolygons(lng = tri[, 1],\n",
" lat = tri[, 2])\n",
"saveWidget(map10, file=\"map10.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map10.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Polygons with sp object\n",
"Another way to give the polygon information is by passing a sp object (from the sp package) with that data. A great example of this is to plot the countries borders in the map."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading required package: sp\n",
"rgdal: version: 1.2-4, (SVN revision 643)\n",
" Geospatial Data Abstraction Library extensions to R successfully loaded\n",
" Loaded GDAL runtime: GDAL 1.10.1, released 2013/08/26\n",
" Path to GDAL shared files: /usr/share/gdal/1.10\n",
" Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]\n",
" Path to PROJ.4 shared files: (autodetected)\n",
" Linking to sp version: 1.2-3 \n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"OGR data source with driver: GeoJSON \n",
"Source: \"/resources/data/countries.txt\", layer: \"OGRGeoJSON\"\n",
"with 177 features\n",
"It has 63 fields\n"
]
},
{
"data": {
"text/html": [
"<iframe src=' map11.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#This example uses the library rgdal to read a geojson file \n",
"#with the countries' borders information and convert it to \n",
"#a sp object\n",
"library(rgdal)\n",
"countries <- readOGR(\"/resources/data/countries.txt\", \"OGRGeoJSON\")\n",
"map11 <- leaflet(countries) %>% addTiles() %>% addPolygons(weight = 1)\n",
"saveWidget(map11, file=\"map11.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map11.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref5\"></a>\n",
"## <center> Colors and legends</center>\n",
"When creating your own map, you can show your data through colors and, to do so, you will have to create a color object according to your data. The most useful color objects you can create are:\n",
"\n",
"- colorNumeric\n",
"- colorFactor\n",
"\n",
"They are, basically, created the same way, in the sense that both of them require a palette (the color scale you want to use; check http://colorbrewer2.org/ for some palette options) and a domain (the values to match with the colors).<br>\n",
"After adding the color, you can add legends to improve your map visualization. To do so, you call the function addLegend(map, position, palette, values) where palette and values are the same you used to create the color object."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### colorNumeric\n",
"The main use for colorNumeric is to represent continuous data, since it match the domain interval to the palette interval."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Warning message:\n",
"In doColorRamp(colorMatrix, x, alpha, ifelse(is.na(na.color), \"\", : '.Random.seed' is not an integer vector but of type 'NULL', so ignored"
]
},
{
"data": {
"text/html": [
"<iframe src=' map12.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"energy <- read.csv(\"/resources/data/energy.csv\")\n",
"#merge the energy data frame with countries\n",
"countries <- sp::merge(countries, energy[, c(1, 3)], by = \"geounit\", all.x = T)\n",
"color <- colorNumeric(\"YlOrRd\", energy$Value)\n",
"map12 <- leaflet(countries) %>% addTiles() %>% addPolygons(stroke = FALSE, \n",
" fillColor = ~color(Value),\n",
" fillOpacity = 1) %>% addLegend(\"topright\",\n",
" pal = color,\n",
" values = countries@data$Value,\n",
" title = \"kWh per capita\",\n",
" opacity = 1)\n",
"\n",
"saveWidget(map12, file=\"map12.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map12.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### colorFactor\n",
"Just like it sounds, colorFactor is better used to represent factors or categorical data."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"data": {
"text/html": [
"<iframe src=' map13.html ' width='100%' height='300' />"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"color <- colorFactor(\"Set1\", countries$continent)\n",
"map13 <- leaflet(countries) %>% addTiles() %>% addPolygons(stroke = F,\n",
" fillColor = ~color(continent),\n",
" fillOpacity = 1) %>% addLegend(\"bottomleft\",\n",
" pal = color,\n",
" values = ~continent,\n",
" opacity = 1,\n",
" title = \"Continent\")\n",
"saveWidget(map13, file=\"map13.html\", selfcontained = F)\n",
"display_html(paste(\"<iframe src=' \", 'map13.html', \" ' width='100%' height='300'\",\"/>\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### About the Author: \n",
"Hi! It's [Martha Aghili](https://www.linkedin.com/in/marta-aghili-2b184b71) the author of this notebook. I hope you found R easy to learn! There's lots more to learn about R but you're well on your way. Feel free to connect with me if you have any questions."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"Copyright &copy; 2016 [Big Data University](https://bigdatauniversity.com/?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "R",
"language": "R",
"name": "conda-env-r-r"
},
"language_info": {
"codemirror_mode": "r",
"file_extension": ".r",
"mimetype": "text/x-r-source",
"name": "R",
"pygments_lexer": "r",
"version": "3.5.1"
},
"widgets": {
"state": {},
"version": "1.1.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment