Visualization methods for points data
There are many approaches for visualization of point data. Some of them are unusual. In this article, I’ll try to show different methods for visualization of the same data on maps. For this, I’ll use only ggplot2
package, but for data preprocessing I will need other packages (for example, maptools, sf, getcartr
etc.).
First of all, let’s take a look at how we can show the same geographical information. We have data, which consist of coordinates. This is data about locations of McDonald’s restaurants in Europe, which I collected from Open Street Maps (OSM). They include only long and lat column. Also, we can:
I think this is not a complete list of what we can do with this data. Also, let’s get this data first.
I described how we could get data from OSM in one of my previous posts. But in this post, I collected lines data (about cycle paths). Here I want to get data about McDonald’s locations in Europe. Therefore I need to receive points, not lines.
This can be done directly using osmdata
package. I recommend running this script as Job (This feature is in the preview versions Rstudio).
require(osmdata)
# for limit
b <- combn(seq(-17, 50, 2), 2) %>% t()
b <- b[!duplicated(b[, 1]), ]
mcdonalds_points <- lapply(1:33, function(i) {
cw <- opq(
bbox = c(b[i, 1], 35, b[i, 2], 70),
timeout = 300, memsize = 1073741824
) %>%
add_osm_feature("amenity", "fast_food") %>%
add_osm_feature(
key = "name",
value = c("McDonald's", "McDonalds"),
value_exact = FALSE, match_case = F
)
cwmap <- osmdata_sp(cw)
cat(i / 33, "\n")
cwmap$osm_points
})
coords <- purrr::safely(function(x) {
f <- dplyr::as_tibble(x@coords)
colnames(f) <- c("lon", "lat")
f
})
mcdonalds_points <- purrr::map_dfr(
mcdonalds_points, function(x) {
coords(x)$result
}
) %>% distinct()
# save data into rds format
readr::write_rds(mcdonalds_points, "mcdonalds.rds")
Hurray! We collected all European McDonald’s locations, which is marked in OSM.
Now it is possible to visualize data.
The simplest way to visualize points on the map is scatter map. In ggplot2
we can do it using borders
and geom_point
.
require(ggplot2)
require(hrbrthemes)
scatterplot <- ggplot() +
borders("world", xlim = c(-10, 50),
ylim = c(33, 71), size = 0.05,
fill = "black") +
geom_point(data = mcdonalds_points, aes(lon, lat),
color = "white", size = 0.1) +
labs(
title = "McDonald's locations in Europe",
subtitle = "Scatter map",
caption = "Based on OSM data"
) +
theme_ipsum(base_family = "PT Sans") +
theme(
panel.grid = element_blank(),
axis.text = element_blank(),
axis.title = element_blank()
) +
coord_map(
projection = "gilbert",
xlim = c(-10, 50),
ylim = c(33, 71)
)
scatterplot