3

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
Chris
  • 3,836
  • 1
  • 16
  • 34
  • Supply a `lineend` parameter. `"round"` would probably give you the nicest joins. – alistaire Aug 12 '18 at 20:11
  • @alistaire thanks, `geom_sf` doesn't look to have a `lineend` parameter though – Chris Aug 12 '18 at 20:18
  • Yeah, it should pass it through to `geom_line`, but it doesn't look like it's working properly. – alistaire Aug 12 '18 at 20:21
  • If you `ggsave` the image with a high resolution, are the gaps still there? – camille Aug 12 '18 at 21:18
  • @camille turning `dpi` up makes no difference – Chris Aug 12 '18 at 21:43
  • If you don't care about projection, you could pull out the coordinates and use `geom_path` so you can specify `lineend` properly: `osm_roads_secondary_sf$osm_lines %>% st_coordinates() %>% as.data.frame() %>% ggplot(aes(X, Y, group = L1)) + geom_path(size = 5, lineend = "round") + coord_equal()` – alistaire Aug 12 '18 at 21:52
  • A more natural approach is to join the lines: `osm_roads_secondary_sf$osm_lines %>% st_union() %>% st_line_merge() %>% ggplot() + geom_sf(size = 5)` It doesn't work for intersections, but the gaps within the segments are gone. – alistaire Aug 12 '18 at 21:58
  • @alistaire thanks. Converting to points and using `geom_path` is a nice solution for my purposes. Can you write up as an answer? Would still be nice to see a solution using `geom_sf`! – Chris Aug 12 '18 at 22:17

1 Answers1

6

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.

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • 1
    It should be possible to combine the `geom_path` solution with `coord_sf()` to get a result with proper gratiules and axis tick labels. – Claus Wilke Aug 13 '18 at 01:44