1

enter image description hereI have this code which prints the Galápagos Islands. But inside the islands, there are lines that I want to get rid off. How is this possible in R?

library(rgdal)
library(ggplot2)

URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
fil <- "gal.json"
if (!file.exists(fil)) download.file(URL, fil)
gal <- readOGR(fil, "OGRGeoJSON")    
gal_map <- fortify(gal)
ggplot() + 
  geom_map(map=gal_map, data=gal_map, aes(x=long, y=lat, map_id=id)) + 
  coord_equal()

Look at the picture: I don't want to show these lines (all of them).

  • Do you want to 'get rid of' the lines by changing the geometry or just the display? – Simbamangu Mar 16 '16 at 16:30
  • I don't want to display the lines inside the islands. I just want the contour and a color to fill the islands – M. Beausoleil Mar 16 '16 at 16:46
2

You can aggregate the polygons and then disaggregate to the 182 individual islands.

URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
fil <- "gal.json"
if (!file.exists(fil)) download.file(URL, fil)

library(rgdal)
gal <- readOGR(fil, "OGRGeoJSON")   

library(raster)
library(rgeos)

a <- aggregate(gal)
b <- disaggregate(a)

plot(b, col=sample(rainbow(length(b))))
2

I assume that the 'lines' you are referring to are simply the polygon boundaries separating one feature from another. If you just want to display the data and you do not necessarily have to stick with ggplot2, a possible option would be to use spplot. Just create a plot of the non-unified polygons with "transparent" line color (see ?sp.polygons) and add the island boundary afterwards using layer from latticeExtra. Note the use of unionSpatialPolygons from maptools to merge all polygons into one, thus ending up with the coastline only.

## add 'id' column
gal@data$id <- seq(gal@data$fid) - 1

## unify polygons
library(maptools)
gal_union <- unionSpatialPolygons(gal, IDs = rep(1, length(gal)))

## display data with island boundaries only
library(latticeExtra)
spplot(gal, "id", col = "transparent", scales = list(draw = TRUE), 
       at = seq(gal@data$id), col.regions = rainbow(nrow(gal@data))) + 
  layer(sp.polygons(gal_union, fill = "transparent"))

spplot

If I got you wrong and you are trying to merge particular polygons e.g. based on their spatial location, I highly recommend having a deeper look at ?unionSpatialPolygons and, for instance, the related post on Joining polygons in R.


Update:

This would be a possible (yet very basic) ggplot2 version of the above code (with the legend disabled).

gal_ff <- fortify(gal)
gal_union_ff <- fortify(gal_union)

ggplot(aes(x = long, y = lat, group = id, fill = id), data = gal_ff) + 
  geom_polygon() + 
  geom_path(aes(x = long, y = lat, group = group), data = gal_union_ff, 
            colour = "black") +
  scale_fill_discrete(guide = FALSE) + 
  coord_equal()

plot2

  • If I want to stay with ggplot2 is there a way to join the polygons? – M. Beausoleil Mar 15 '16 at 12:43
  • Based on which feature property would you like to join the polygons? Or is it just about not displaying the inland polygon borders? – fdetsch Mar 15 '16 at 12:48
  • I just want to remove the lines inside the islands. Later, I'll try to apply a different colour to each island. – M. Beausoleil Mar 15 '16 at 23:52
  • @M.Beausoleil then the above update should fit your needs. Have a particular look at group = group inside geom_path that is responsible for the grouping of multiple polygons located on the same island. – fdetsch Mar 16 '16 at 7:03
  • Sorry, but I still see some lines. I don't understand what group = group or group = id is making. Plus, I'm using ggmap. I don't know if it's the issue – M. Beausoleil Mar 16 '16 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.