1
votes

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

1
I think its just the way its ordered. geom_path will plot them based on the order presented in the data.frame rows. I don't think group should serve any purpose here as you are producing one line segment. If possible I would set the order of the rows from the start of the country sketch, to the end and then use geom_path without group. Should work okJonny Phelps
Problem is that your mainland group does not start in the south and end in the north, but starts just north of Trondheim, does the north and then starts at the south.Richard Telford
@JonnyPhelps the group will keep the island separate from the mainlandRichard Telford
ah right, I thought it was just one line, my mistakeJonny Phelps
@RichardTelford you're right about both the problem the geographic area well done! I did sort my data but it seems I have a problem with the fjords as illustrated in the edit. I'll try your answer.Drosof

1 Answers

1
votes

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")