Chris
Chris

Reputation: 3986

Fixing gaps in osm roads when plotting with ggplot2/ geom_sf

When plotting road data downloaded from osm using osmdata, the resulting plot has gaps in it when using larger values of size in geom_sf (see image).

Here is a reproducible example using a section of road in SW London. How can I remove the white gaps in the line when plotting?

library(tidyverse)
library(sf)
library(osmdata)

# define bounding box for osm data
my_bbox <- 
  matrix(c(-0.2605616, -0.2605616,
           -0.2004485, -0.2004485,
           -0.2605616, 51.4689943,
           51.4288980, 51.4288980,
           51.4689943, 51.4689943),
         ncol = 2)
bbox_sf <- st_geometry(st_polygon(x = list(my_bbox)))
st_crs(bbox_sf) <- 4326

#get osm road data for bounding box
osm_roads_secondary_sf <- 
  opq(bbox = st_bbox(bbox_sf)) %>%
  add_osm_feature(key = 'highway', value = 'secondary') %>%
  osmdata_sf()

ggplot() + 
  geom_sf(data=osm_roads_secondary_sf$osm_lines,size=4)

road plot has gaps in it

session info:

R version 3.5.0 (2018-04-23)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.6

other attached packages:
 [1] osmdata_0.0.7        sf_0.6-3             forcats_0.3.0        
stringr_1.3.1       
 [5] dplyr_0.7.5          purrr_0.2.5          readr_1.1.1          
tidyr_0.8.1         
 [9] tibble_1.4.2         ggplot2_3.0.0        tidyverse_1.2.1.9000

Upvotes: 3

Views: 567

Answers (1)

alistaire
alistaire

Reputation: 43334

The ideal solution is to pass lineend = "round" to geom_sf, which should get passed along to geom_path (its underlying layer, really) to round off the end of the lines, causing slight overlaps and a smooth appearance. That sadly doesn't work, though:

ggplot(osm_roads_secondary_sf$osm_lines) + 
    geom_sf(size = 4, lineend = "round")
#> Warning: Ignoring unknown parameters: lineend

I've filed an issue on GitHub, but since ggplot just had a release, any fix won't make it to CRAN for a while yet.

In the mean time, workarounds include extracting the paths from the geometry column with st_coordinates. The resulting matrix, coerced to a data frame, can be plotted with geom_path, which happily accepts a lineend parameter:

osm_roads_secondary_sf$osm_lines %>% 
    st_coordinates() %>% 
    as.data.frame() %>% 
    ggplot(aes(X, Y, group = L1)) + 
    geom_path(size = 4, lineend = "round") + 
    coord_sf(crs = 4326)

Change the color to a suitable shade of gray for a more geom_sf-like appearance.

A more sf-native approach is to merge the line segments into continuous lines, which, of course, have no gaps. st_line_merge does the heavy lifting, but you'll need to aggregate them into multilines beforehand so it has the necessary data:

osm_roads_secondary_sf$osm_lines %>% 
    st_union() %>% 
    st_line_merge() %>% 
    ggplot() + 
    geom_sf(size = 4)

Note that this is mostly, but not entirely better. The gaps within the lines are gone, but st_line_join doesn't know how to fix the three-way intersection, so there's still a tiny gap there. If your real data has lots of such intersections (which is quite possible), this approach won't yield good results.

A last approach is to simply use base sf plotting, which defaults to a round line-end:

plot(osm_roads_secondary_sf$osm_lines$geometry, lwd = 10)

Whether such an approach is practical depends on what else remains to be done with the plot and how facile you are with base plotting.

Upvotes: 6

Related Questions