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
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
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
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),]
geom_path
will plot them based on the order presented in the data.frame rows. I don't thinkgroup
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 ok – Jonny Phelpsgroup
will keep the island separate from the mainland – Richard Telford