Generating Interactive Maps | Leaflet

I’ve been trying to figure out how to generate interactive maps using R, and so naturally i’ve ended up spending an unhealthy amount of time scouring the web looking for a reasonably simple way of getting that done. Finally, I came across the Leaflet package which allows you to do just that. Considering the fact that many people actually use Leaflet to generate these sort of maps, i’m ashamed it took me this long to learn about it’s existence.

I had plenty of data saved from previous scraping projects, so i thought i should try plotting one using the same data i used in the Swedish property prices post.

After a lot of googling, reading, and trial/error; i finally ended with the following code to generate the interactive map.

#load packages
library(leaflet)
library(ggmap)
library(htmltools)
library(dplyr)
library(htmlwidgets)

#load property file
read.csv("Property_Prices_File.csv", sep = ",") -> Complete

#select necessary columns
select(Complete, Latitudes, Longitudes, Property_Type, Rooms, Living_Area, Estimate, Links) -> df

#check classes
str(df)

#change prices to numeric
df$Estimate <- as.numeric(as.character(df$Estimate))

#For easier plotting, removing NA rows
na.omit(df) -> df_plot

#filter out properties <= SEK 700k, <= 3 rooms, and > 1 room (This is purely arbitrary)
filter(df_plot, Estimate <= 700000, Rooms <= 3, Rooms > 1) -> df_plot

#set colors
colorNumeric(palette = rainbow(3), domain = df_plot$Estimate) -> pal

#generate map and assign
leaflet(df_plot) %>% 
  addTiles() %>% 
  
  addCircles(lng = ~Longitudes, lat = ~Latitudes, 
             weight = 1, color = ~pal(Estimate), 
             fillColor = ~pal(Estimate), radius = 5000, 
             fillOpacity = 0.5, popup = ~htmlEscape(Links)) %>% 
  
  addLegend("bottomright", pal = pal, values = ~Estimate,
            title = "Property Price",
            labFormat = labelFormat(prefix = "SEK "),
            opacity = 1
  ) -> map

#export map files to wd
saveWidget(map, file = "map.html", selfcontained = FALSE)

Since this is the first time i’ve ever used this package, the circle sizes are fixed. I originally planned on plotting the radius condition to the number of rooms; but then the 1 bedroom categories were barely visible. I also chose to limit the prices to less than or equal to SEK 700,000 for the sole reason that there are plenty of outliers that turn the legend into a disproportionate scale. The resulting map would show circles that all have the same color.

I’m certain that there is a simple solution to all of this, but i’ll need to get used to all that this package has to offer before i can plot something that’s actually insightful. In the meantime, i hope you find the code useful in plotting your first interactive map using R.

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

One Response to Generating Interactive Maps | Leaflet

  1. Jonathan Schwartz says:

    This is awesome – looking to do the exact same thing – thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *