rggplot2rgdalr-maptools

Leaving the path open from geom_path


I am plotting data on a coastline map using geom_path and I can't remove the line linking the first and last data point. The data set for this is quite large but can be found here.

The problem was reported and fixed on this thread, although it didn't help in my case.

LHplot <- ggplot(data = LH, aes(x = long, y = lat, group=group)) +
  geom_path(aes(group=group), size = 1, color = "darkgrey") + 
  theme_bw() +
  theme(axis.line.y=element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    panel.grid.major = element_blank(), # switch off major gridlines
    panel.grid.minor = element_blank(), # switch off minor gridlines
    panel.border = element_blank(),
    text=element_text(family="Times New Roman", size=11)
  )
LHplot

enter image description here

I also noticed that when plotting a subset of the data, the path may or not be open

LH2 <- LH[1:16000,]
LH2plot <- ggplot(data = LH2, aes(x = long, y = lat, group=group)) +
  geom_path(aes(group=group), size = 1, color = "darkgrey") + 
  theme_bw() +
  theme(axis.line.y=element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    panel.grid.major = element_blank(), # switch off major gridlines
    panel.grid.minor = element_blank(), # switch off minor gridlines
    panel.border = element_blank(),
    text=element_text(family="Times New Roman", size=11)
  )
LH2plot

enter image description here

LH2 <- LH[1:50000,]
LH2plot <- ggplot(data = LH2, aes(x = long, y = lat, group=group)) +
  geom_path(aes(group=group), size = 1, color = "darkgrey") + 
  theme_bw() +
  theme(axis.line.y=element_blank(),
    axis.line.x = element_blank(),
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    panel.grid.major = element_blank(), # switch off major gridlines
    panel.grid.minor = element_blank(), # switch off minor gridlines
    panel.border = element_blank(),
    text=element_text(family="Times New Roman", size=11)
  )
LH2plot

enter image description here

Does the problem come from gaps in the data set? or simply the way it is organized in the data frame and how geom_path reads it?

Edit

From the comment below: I sorted the data by latitude to draw the path from south to north:

LH <- LH[order(LH$lat),]

Which fixes the line problem, but creates another problem: enter image description here


Solution

  • The problem is that your mainland coastline does not start at one end and go to the other, but starts somewhere in the middle.

    First thing is to identify the jump. Below I identify it using latitude alone, but both latitude and longitude could be used together if needed (with geodesic distances) but that would be much more work for no gain.

    Then we need to re-arrange the data, moving the rows from above the break to the end of the dataset (in this case because the rows go clockwise round Norway).

    library(tidyverse)
    #Find the largest change in latitude
    LH %>% 
      group_by(group) %>% 
      mutate(llat = lag(lat), dlat = abs(lat - llat)) %>% 
      ungroup() %>% 
      mutate( n = 1:n()) %>% 
      slice(which.max(dlat))
    
    #re-arrange data
    bind_rows(LH %>% slice(-(1:16015)),
          LH %>% slice(1:16015)) %>% 
          ggplot(aes(x = long, y = lat, group=group)) +
          geom_path(size = 1, color = "darkgrey")