3

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:

enter image description here

PART2 script gives me this output:

enter image description here

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.

Community
  • 1
  • 1
lokheart
  • 23,743
  • 39
  • 98
  • 169
  • I don't know how to fix, but what about changing the alpha value of the blue parts? – Luciano Selzer Aug 21 '12 at 03:07
  • not possible, i need to have a hole in the red part, something will be shown behind the red part – lokheart Aug 21 '12 at 03:11
  • So the blue bits are actually over the red? – Luciano Selzer Aug 21 '12 at 03:13
  • yes, currently I plot it on top of the red one so that one can see which part should be land, which part should be hole – lokheart Aug 21 '12 at 03:23
  • 1
    why are you using this dumb text file and not giving detail on how to load it? Did the data come from shapefile originally? That would be helpful, there's obviously some winding problems in Part 1. – mdsumner Aug 21 '12 at 04:58
  • IN part 1 you do group=id, but the id value is 15 all the way down, so that's doing nothing. What did you expect it to do? Why group=id? – Spacedman Aug 21 '12 at 07:34
  • @Spacedman there is a group column indicating different polygon, some of them are actually hole and can be shown in the hole column, so using group=id can show the hole also, to my understanding. – lokheart Aug 21 '12 at 07:38
  • @lokheart That's incorrect. `group` tells ggplot to treat the data as a group - this has nothing to do with holes, as @spacedman points out. – Andrie Aug 21 '12 at 09:01
  • @Andre do you mean I don't have to add group while using geom_polygon? – lokheart Aug 21 '12 at 09:19
  • No, I don't mean that. I just mean that `group` has nothing to do with holes. – Andrie Aug 21 '12 at 09:22
  • @Andrie you refer to vote, but not accepted answer, right? I'm a bit confused – lokheart Aug 21 '12 at 10:11

1 Answers1

10

With a nod to @spacedman, who said:

The solution I came up with years ago for drawing holes is to make sure that after each hole your x,y coordinates return to the same place. This stops the line buzzing all around and crossing other polygons and leaving open areas that the winding number algorithm doesn't fill (or does fill when it shouldn't).

(In https://stackoverflow.com/a/12051278/602276)

So, let's follow his advice:

library(plyr2)
map.shp2 <- ddply(map.shp, .(piece), function(x)rbind(x, map.shp[1, ]))
ggplot(data=map.shp2) + geom_polygon(aes(x=long,y=lat))

enter image description here

Community
  • 1
  • 1
Andrie
  • 176,377
  • 47
  • 447
  • 496