Skip to Content

plotting land with hole using ggplot2

I have asked about the same issue from here and here, but still can't get my problem solved. I think I need to bring the whole problem and ask for help, rather than breaking it down into small parts.

I have a dataframe which I exported it to csv and can be found at http://pastebin.com/SNT9Ykt7.

chart <- ggplot(data=map.shp,aes(x=long,y=lat))
 
### PART1 START ###
chart <- chart + geom_polygon(data=map.shp,aes(x=long,y=lat,group=id),colour=rgb(162,159,140,maxColorValue=255),fill=rgb(233,235,232,maxColorValue=255),size=0.1)
### PART1 END ###
 
### PART2 START ###    
map.group <- unique(map.shp[,"group"])
for (loop in (1:length(map.group))) {
  temp.shp <- map.shp[map.shp[,"group"]==map.group[loop],]
  temp.colour <- "red"
  if (unique(temp.shp[,"hole"])=="TRUE") {
    temp.colour <- "blue"
  }
  chart <- chart + geom_polygon(data=temp.shp,aes(x=long,y=lat,group=id,order=group),colour=rgb(162,159,140,maxColorValue=255),fill=temp.colour,size=0.1)
}
### PART2 END ###
 
chart <- chart + opts(panel.background=theme_rect(colour=rgb(190,225,247,maxColorValue=255),fill=rgb(190,225,247,maxColorValue=255)),                      
                      panel.grid.major=theme_blank(),
                      panel.grid.minor=theme_blank(),
                      panel.border=theme_blank(),
                      plot.background = theme_blank(),
                      axis.line=theme_blank(),
                      axis.text.x=theme_blank(),
                      axis.title.x=theme_blank(),
                      axis.text.y=theme_blank(),
                      axis.title.y=theme_blank(),
                      axis.ticks=theme_blank())
chart <- chart + coord_cartesian(xlim = range(map.shp[,"long"]), ylim = range(map.shp[,"lat"]))

PART1 script gives me this output:

PART2 script gives me this output:

Actually this is a piece land with some hole on it, I will have something else shown under this layer so that I must present the hole as "hole", so display using PART2 script is not possible. But PART2 script is plotting the map correctly (red as land, blue as hole).

A few problems from PART1 output that I need to fix:

  • some part of the hole not presented as hole
  • line outside the polygon is plotted wrongly

I don't know what have I done wrong in PART1. Can anyone help?

update 01

The txt file is created using the following code:

map.shp.raw <- readShapeSpatial("shp_files/map.shp")
map.shp <- fortify(map.shp.raw)

The txt file attached can be saved as txt and import as data.frame using read.table command.