SlideShare a Scribd company logo
How to create interactive
maps in R & Python
Martin Bago, Instarea #PyDataBA27. 11. 2017
(PyData Bratislava Meetup #5, Nervosa)
HOW TO CREATE
INTERACTIVE MAPS IN R
& PYTHON?
Martin Bago, Instarea
PyData Bratislava #5, 27.11.2017
1/20
Martin Bago
■ Ing. @ Process Automation and Informatization in Industry (2016, MTF STU BA)
■ Bc. @ Applied Informatics (2014, FEI STU BA)
■ 2017-now Data Scientist, Instarea s.r.o., Market Locator
■ 2015-2016 Head of Analyst, News and Media Holding a.s.
■ 2014-2015 SEO Analyst, Centrum Holdings a.s.
■ 2011-2014 Automix.sk, Centrum Holdings a.s.
■ 2010-2013 Editor-in-chief OKO Casopis (FEI STU BA)
■ Passionate driver, beer&coffee&football lover
2/20
■ Why an interactive map?
■ Why to use Python or R?
■ Which libraries are necessary
■ What are the key issues
■ How to integrate polygons of districts, how to color your map, etc...
■ What kind of data do we use and what are the typical use cases we covered with
interactive maps
■ Q&A
3/20
Why an interactive map?
■ Can show incredible amount of data
■ Visual & very intuitive way to understanding data
■ Available online
■ No graphic design skills needed
■ Easy to play with
■ Porn for analysts
4/20
Why to use R or Python?
■ Handling big data
■ SQL select directly from code
■ Saving as HTML
■ Few lines of code
■ No javascript needed
5/20
What do you need?
■ R/R Studio
■ Python – Jupyter notebook
■ Basic coding skills
■ Idea
■ Data
■ Internet
6/20
Libraries
7/20
library(leaflet) # using Leaflet in R
library(readxl) # reading excel in R
library(rgdal) # reading JSON in R
install.packages(“…") # installing packages in R
Other important libraries:
library(dplyr) - data operations
library(htmltools) - HTML popups/labels
library(geojsonio) – working with geoJSONs
Working with 2D maps (images):
library(ggplot2)
library(plotly)
library(maptools)
library(rnaturalearth)
Data formats
■ .txt, .csv or .xls/.xlsx is fine, but check the encoding
■ One of the benefits of using R/Python is easy use of data transformation
■ You can use SQL select directly in R/Python and use data from your RAM
8/20
Source: http://www.statistics.sk/pls/elisw/metainfo.explorer?obj=41&cmd=go&s=1002&sso=2&so=15
Reading data&polygons
9/20
SVK_districts <- rgdal::readOGR("districts_epsg_4326.geojson", "OGRGeoJSON")
okresy <- read_excel(“okresy.xlsx")
Districts of Slovakia in geoJSON at GITHUB (thx to drakh)
All used files will be available to download
Generating map
10/20
leaflet() %>%
addTiles()
addPolygons(data = SVK_districts)
Setting bins & colors
11/20
bins <- c(550, 650, 750, 800, 850, 900, 950, 1000, Inf)
pal <- colorBin("YlOrRd", domain = okresy$Mzdy, bins = bins) #ColorBrewer
Adding labels
12/20
library(htmltools)
labels <- sprintf(
"<strong>%s</strong><br/>Average income in 2015: %g",
okresy$Okresy,okresy$Mzdy) %>% lapply(htmltools::HTML)
Generating map II.
13/20
leaflet() %>%
addTiles() %>%
setView(lng = 19.411622, lat = 48.696708, zoom = 7) %>%
addPolygons(data = SVK_districts, opacity = 0.25, weight = 1, color =
"black", fillColor = ~pal(okresy$Mzdy), fillOpacity =
0.50,smoothFactor = 1, stroke = 0.1, label = labels, highlightOptions
= highlightOptions(color = "black", weight = 5, bringToFront = TRUE))
%>%
addProviderTiles('CartoDB.Positron') %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addLayersControl( baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(pal = pal, values = okresy$Mzdy, opacity = 0.7, title =
"Priemerne mzdy na rok 2015",
position = "bottomright")
AddPolylines & Add ... & Add..
■ https://rstudio.github.io/leaflet/
14/20
addMarkers(~long, ~lat, icon = …)
addMarkers(clusterOptions = marketClusterOptions())
addCircleMarkers()
addCircles(lng = ~Long, lat = ~lat, weight = 1, radius = ~sqrt(Pop) * 30)
addRectangles()
addPolylines()
Complete code
15/20
library(readxl) # reading excel
library(leaflet) # working with leaflet
library(rgdal) # working with json
library(htmltools) # adding labels / popups
setwd("C/…") # set working directory
SVK_districts <- rgdal::readOGR("districts_epsg_4326.geojson", "OGRGeoJSON") # reading JSON polygons
okresy <- read_excel(“okresy.xlsx") # reading offline data
bins <- c(550, 650, 750, 800, 850, 900, 950, 1000, Inf) # setting bins
pal <- colorBin("YlOrRd", domain = okresy$Mzdy, bins = bins) # setting colors
labels <- sprintf(
"<strong>%s</strong><br/>Average income in 2015: %g",
okresy$Okresy,okresy$Mzdy) %>% lapply(htmltools::HTML) # adding labels
leaflet() %>% # map generating
addTiles() %>%
setView(lng = 19.411622, lat = 48.696708, zoom = 7) %>% # set default view
addPolygons(data = SVK_districts, opacity = 0.25, weight = 1, color = "black", # adding polygon tiles
fillColor = ~pal(okresy$Mzdy), fillOpacity = 0.50,smoothFactor = 1,
stroke = 0.1, label = labels,
highlightOptions = highlightOptions(color = "black", weight = 5, bringToFront = TRUE)) %>%
addProviderTiles('CartoDB.Positron') %>% # adding other backgroung maps
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addLayersControl( baseGroups = c("OSM (default)", "Toner", "Toner Lite),
options = layersControlOptions(collapsed = TRUE)) %>%
addLegend(pal = pal, values = okresy$Mzdy, opacity = 0.7, title = “Average income in 2015", # adding Legend
position = "bottomright")
Alternatives
16/20
Other types of background maps here:
http://leaflet-extras.github.io/leaflet-
providers/preview/index.html
…and how does it look in Python?
■ Folium - Python Data, Leaflet.js Maps
■ https://github.com/python-visualization/folium
17/20
import folium
import json
import pandas as pd
import numpy as np
import colorbrewer
.
.
.
with open('districts2.geojson') as f:
js = json.load(f)
m = folium.Map(location=[48.5, 17.5], zoom_start=7)
folium.Marker(
[lat, long]
, popup= ‘TEXT'
, icon=folium.Icon(color='red',icon='info-sign')
).add_to(m)
m
m.save('mapa_final.html')
Other options
■ Getting routes from Google Maps (library googlemaps)
■ Adding HTML popups with images (library htmlwidgets)
■ Distance measurements
18/20
Find more at:
analyzy.marketlocator.sk
19/20
THANKS FOR YOUR ATTENTION
martin.bago@gmail.com
martin.bago@instarea.com
https://www.linkedin.com/in/martinbago/
https://www.facebook.com/martin.bago
FEEL FREE TO ASK
20/20

More Related Content

Recently uploaded

Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
alex933524
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
Subhajit Sahu
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
ewymefz
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
correoyaya
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
MaleehaSheikh2
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
James Polillo
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
haila53
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 

Recently uploaded (20)

Tabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflowsTabula.io Cheatsheet: automate your data workflows
Tabula.io Cheatsheet: automate your data workflows
 
Adjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTESAdjusting primitives for graph : SHORT REPORT / NOTES
Adjusting primitives for graph : SHORT REPORT / NOTES
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
FP Growth Algorithm and its Applications
FP Growth Algorithm and its ApplicationsFP Growth Algorithm and its Applications
FP Growth Algorithm and its Applications
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
Jpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization SampleJpolillo Amazon PPC - Bid Optimization Sample
Jpolillo Amazon PPC - Bid Optimization Sample
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdfCh03-Managing the Object-Oriented Information Systems Project a.pdf
Ch03-Managing the Object-Oriented Information Systems Project a.pdf
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
GetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Martin Bago: How to create interactive maps in R & Python?

  • 1. How to create interactive maps in R & Python Martin Bago, Instarea #PyDataBA27. 11. 2017 (PyData Bratislava Meetup #5, Nervosa)
  • 2. HOW TO CREATE INTERACTIVE MAPS IN R & PYTHON? Martin Bago, Instarea PyData Bratislava #5, 27.11.2017 1/20
  • 3. Martin Bago ■ Ing. @ Process Automation and Informatization in Industry (2016, MTF STU BA) ■ Bc. @ Applied Informatics (2014, FEI STU BA) ■ 2017-now Data Scientist, Instarea s.r.o., Market Locator ■ 2015-2016 Head of Analyst, News and Media Holding a.s. ■ 2014-2015 SEO Analyst, Centrum Holdings a.s. ■ 2011-2014 Automix.sk, Centrum Holdings a.s. ■ 2010-2013 Editor-in-chief OKO Casopis (FEI STU BA) ■ Passionate driver, beer&coffee&football lover 2/20
  • 4. ■ Why an interactive map? ■ Why to use Python or R? ■ Which libraries are necessary ■ What are the key issues ■ How to integrate polygons of districts, how to color your map, etc... ■ What kind of data do we use and what are the typical use cases we covered with interactive maps ■ Q&A 3/20
  • 5. Why an interactive map? ■ Can show incredible amount of data ■ Visual & very intuitive way to understanding data ■ Available online ■ No graphic design skills needed ■ Easy to play with ■ Porn for analysts 4/20
  • 6. Why to use R or Python? ■ Handling big data ■ SQL select directly from code ■ Saving as HTML ■ Few lines of code ■ No javascript needed 5/20
  • 7. What do you need? ■ R/R Studio ■ Python – Jupyter notebook ■ Basic coding skills ■ Idea ■ Data ■ Internet 6/20
  • 8. Libraries 7/20 library(leaflet) # using Leaflet in R library(readxl) # reading excel in R library(rgdal) # reading JSON in R install.packages(“…") # installing packages in R Other important libraries: library(dplyr) - data operations library(htmltools) - HTML popups/labels library(geojsonio) – working with geoJSONs Working with 2D maps (images): library(ggplot2) library(plotly) library(maptools) library(rnaturalearth)
  • 9. Data formats ■ .txt, .csv or .xls/.xlsx is fine, but check the encoding ■ One of the benefits of using R/Python is easy use of data transformation ■ You can use SQL select directly in R/Python and use data from your RAM 8/20 Source: http://www.statistics.sk/pls/elisw/metainfo.explorer?obj=41&cmd=go&s=1002&sso=2&so=15
  • 10. Reading data&polygons 9/20 SVK_districts <- rgdal::readOGR("districts_epsg_4326.geojson", "OGRGeoJSON") okresy <- read_excel(“okresy.xlsx") Districts of Slovakia in geoJSON at GITHUB (thx to drakh) All used files will be available to download
  • 12. Setting bins & colors 11/20 bins <- c(550, 650, 750, 800, 850, 900, 950, 1000, Inf) pal <- colorBin("YlOrRd", domain = okresy$Mzdy, bins = bins) #ColorBrewer
  • 13. Adding labels 12/20 library(htmltools) labels <- sprintf( "<strong>%s</strong><br/>Average income in 2015: %g", okresy$Okresy,okresy$Mzdy) %>% lapply(htmltools::HTML)
  • 14. Generating map II. 13/20 leaflet() %>% addTiles() %>% setView(lng = 19.411622, lat = 48.696708, zoom = 7) %>% addPolygons(data = SVK_districts, opacity = 0.25, weight = 1, color = "black", fillColor = ~pal(okresy$Mzdy), fillOpacity = 0.50,smoothFactor = 1, stroke = 0.1, label = labels, highlightOptions = highlightOptions(color = "black", weight = 5, bringToFront = TRUE)) %>% addProviderTiles('CartoDB.Positron') %>% addProviderTiles(providers$Stamen.Toner, group = "Toner") %>% addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>% addLayersControl( baseGroups = c("OSM (default)", "Toner", "Toner Lite"), options = layersControlOptions(collapsed = FALSE)) %>% addLegend(pal = pal, values = okresy$Mzdy, opacity = 0.7, title = "Priemerne mzdy na rok 2015", position = "bottomright")
  • 15. AddPolylines & Add ... & Add.. ■ https://rstudio.github.io/leaflet/ 14/20 addMarkers(~long, ~lat, icon = …) addMarkers(clusterOptions = marketClusterOptions()) addCircleMarkers() addCircles(lng = ~Long, lat = ~lat, weight = 1, radius = ~sqrt(Pop) * 30) addRectangles() addPolylines()
  • 16. Complete code 15/20 library(readxl) # reading excel library(leaflet) # working with leaflet library(rgdal) # working with json library(htmltools) # adding labels / popups setwd("C/…") # set working directory SVK_districts <- rgdal::readOGR("districts_epsg_4326.geojson", "OGRGeoJSON") # reading JSON polygons okresy <- read_excel(“okresy.xlsx") # reading offline data bins <- c(550, 650, 750, 800, 850, 900, 950, 1000, Inf) # setting bins pal <- colorBin("YlOrRd", domain = okresy$Mzdy, bins = bins) # setting colors labels <- sprintf( "<strong>%s</strong><br/>Average income in 2015: %g", okresy$Okresy,okresy$Mzdy) %>% lapply(htmltools::HTML) # adding labels leaflet() %>% # map generating addTiles() %>% setView(lng = 19.411622, lat = 48.696708, zoom = 7) %>% # set default view addPolygons(data = SVK_districts, opacity = 0.25, weight = 1, color = "black", # adding polygon tiles fillColor = ~pal(okresy$Mzdy), fillOpacity = 0.50,smoothFactor = 1, stroke = 0.1, label = labels, highlightOptions = highlightOptions(color = "black", weight = 5, bringToFront = TRUE)) %>% addProviderTiles('CartoDB.Positron') %>% # adding other backgroung maps addProviderTiles(providers$Stamen.Toner, group = "Toner") %>% addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>% addLayersControl( baseGroups = c("OSM (default)", "Toner", "Toner Lite), options = layersControlOptions(collapsed = TRUE)) %>% addLegend(pal = pal, values = okresy$Mzdy, opacity = 0.7, title = “Average income in 2015", # adding Legend position = "bottomright")
  • 17. Alternatives 16/20 Other types of background maps here: http://leaflet-extras.github.io/leaflet- providers/preview/index.html
  • 18. …and how does it look in Python? ■ Folium - Python Data, Leaflet.js Maps ■ https://github.com/python-visualization/folium 17/20 import folium import json import pandas as pd import numpy as np import colorbrewer . . . with open('districts2.geojson') as f: js = json.load(f) m = folium.Map(location=[48.5, 17.5], zoom_start=7) folium.Marker( [lat, long] , popup= ‘TEXT' , icon=folium.Icon(color='red',icon='info-sign') ).add_to(m) m m.save('mapa_final.html')
  • 19. Other options ■ Getting routes from Google Maps (library googlemaps) ■ Adding HTML popups with images (library htmlwidgets) ■ Distance measurements 18/20
  • 21. THANKS FOR YOUR ATTENTION martin.bago@gmail.com martin.bago@instarea.com https://www.linkedin.com/in/martinbago/ https://www.facebook.com/martin.bago FEEL FREE TO ASK 20/20