1

I have these vectors of the lakes in New Guinea and the coastline and i want to plot it using ggplot2 exactly as it follows but with a more cool appearance.

co<-coastline.shp
nglakes<-lakes.shp
plot(co,col="yellow")
plot(nglakes,col="blue",add=T)
legend("topleft",legend=c("LAND","LAKE"),title="LineTypes",col=c("yellow","blue"),lty=1,cex=0.8)

closed as primarily opinion-based by PolyGeo Sep 17 '15 at 9:26

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Could you provide enough code for a reproducible example? What do you mean by 'more cool'? What specifically are you looking to do? – scw Sep 10 '15 at 20:40
  • More professional i mean.You can use similar shapefiles to make it if you want. The idea is to make a map using the corresponding code in ggplot2. – gsa Sep 10 '15 at 21:00
  • How about posting your shape files? And perhaps a snapshot of what you have in mind for your final map? "More professional" might mean different things do different people. – cengel Sep 11 '15 at 22:11
  • Seeking the same as what you have but with a "more cool appearance" makes this open to opinion as to what is cool. In addition I think a question that seems like "I've done the easy bit, now give me ideas for how to embellish" makes it too broad also, – PolyGeo Sep 17 '15 at 9:26
  • Actually not my favourite question, but I really like the answer. – Iris Sep 17 '15 at 9:29
5

I used 10-m lake shapefile data from Natural Earth along with country borders from rworldmap to reproduce what I guess you were trying to achieve. Hadley Wickham wrote a nice tutorial on how to visualize spatial data using ggplot2 and I strongly encourage you to have a look at it. Still, a short workaround is required in order to get the visualization to work with fragmented polygons as it is the case for Papua New Guinea. Check out the following code.

## required packages
library(rgdal)
library(rworldmap)
library(raster)
library(plyr)
library(ggplot2)

## import lake shapefile
nglakes <- readOGR("data", "ne_10m_lakes")

## select papua new guinean lakes only
data(countriesCoarse)
ng <- subset(countriesCoarse, ADMIN == "Papua New Guinea")

nglakes <- crop(nglakes, ng)

## transform shapefile into data.frame
nglakes@data$id = rownames(nglakes@data)
nglakes.points = fortify(nglakes, region = "id")
nglakes.df = join(nglakes.points, nglakes@data, by = "id")
nglakes.df$Type <- "Lake"

## base ggplot
p <- ggplot() + 
  geom_polygon(aes(x = long, y = lat), data = ng, fill = NA, colour = NA) + 
  theme_bw() + 
  labs(x = "Longitude", y = "Latitude")

## gradually add polygons to existing plot
n <- 1
for (i in ng@polygons[[1]]@Polygons) {

  # convert each sub-polygon to 'spatialpolygonsdataframe' 
  pys <- Polygons(list(i), ID = n)
  spys <- SpatialPolygons(list(pys), proj4string = CRS(proj4string(ng)))
  spysdf <- SpatialPolygonsDataFrame(spys, data = data.frame(ID = n), match.ID = FALSE)

  # transform sub-polygon shapefile into data.frame
  spysdf@data$id <- rownames(spysdf@data)
  spysdf.points <- fortify(spysdf, region = "id")
  spysdf.df <- join(spysdf.points, spysdf@data, by = "id")
  spysdf.df$Type <- "Land"

  # add sub-polygon to existing plot
  p <- p + 
    geom_polygon(aes(x = long, y = lat, fill = Type), data = spysdf.df, 
                 colour = "grey65")
  n <- n + 1
}

## add lake polygon
p + 
  geom_polygon(aes(x = long, y = lat, fill = Type), data = nglakes.df) + 
  scale_fill_manual(values = c("Land" = "yellow", "Lake" = "blue")) + 
  theme(legend.position = c(1, 0), legend.justification = c(1, 0))

map Does this come close at what you were trying to figure out?

  • +1 In contrast to the question this looks like a great answer. – PolyGeo Sep 17 '15 at 9:28

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