Generating Interactive Maps | Leaflet

[This article was first published on r – Recommended Texts, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

#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

#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.

To leave a comment for the author, please follow the link and comment on their blog: r – Recommended Texts. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)