plotting land with hole using ggplot2

对着背影说爱祢 提交于 2019-12-10 10:00:56

问题


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.


回答1:


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))



来源:https://stackoverflow.com/questions/12047919/plotting-land-with-hole-using-ggplot2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!