使用ggplot2绘制带孔的土地 [英] plotting land with hole using ggplot2

查看:123
本文介绍了使用ggplot2绘制带孔的土地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这里询问了相同的问题,并且这里,但仍不能解决我的问题。我想我需要提出整个问题并寻求帮助,而不是将它分解成小部分。



我有一个数据框,我将它导出到csv并可以可在 http://pastebin.com/SNT9Ykt7 找到。

 图<  -  ggplot(data = map.shp,aes(x = long,y = lat))

### PART1 START# ##
图< - chart + geom_polygon(data = map.shp,aes(x = long,y = lat,group = id),color = rgb(162,159,140,maxColorValue = 255),fill = rgb 233,235,232,maxColorValue = 255),size = 0.1)
### PART1 END ###

### PART2 START ###
map.group< - 独特(map.shp [,group])
for(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 + geom_polygon(data = temp.shp,aes(x = long,y = lat,group = ID,订单=基团),C olour = rgb(162,159,140,maxColorValue = 255),fill = temp.colour,size = 0.1)
}
### PART2 END ###

图表<图+ opts(panel.background = theme_rect(color = 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 + coord_cartesian(xlim = range(map.shp [,long]),ylim = range(map.shp [ ,lat]))

PART1脚本给了我这个输出:





PART2脚本给了我这个输出:


实际上这是一块带有一些洞的土地,我会在这层之下展示其他东西,这样我就必须呈现孔作为孔,所以使用PART2脚本显示是不可能的。但PART2脚本正确绘制地图(红色为土地,蓝色为洞)。



PART1输出中有一些问题需要修正:




  • 孔的某些部分未显示为孔

  • 线在多边形之外被错误地绘制出来



我不知道我在PART1中做了什么错误。任何人都可以帮忙吗?



更新01



txt文件是使用以下代码创建的:

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

附加的txt文件可以保存为txt并导入为data.frame使用read.table命令。

解决方案

用@spacedman点头,说:


几年前我提出的绘制孔的解决方案是让
确保在每个孔之后x,y坐标返回到相同的
地方。这样可以避免线路嗡嗡作响,并穿过其他
多边形并留下开放区域,绕线数算法
不填充(或者在不应该填充时填充)。


(在 https://stackoverflow.com/a/12051278/所以,让我们按照他的意见:

 <$ c $ (map.shp,。(piece),function(x)rbind(x,map.shp [1,]))
ggplot( data = map.shp2)+ geom_polygon(aes(x = long,y = lat))

< img src =https://i.stack.imgur.com/PdFdP.pngalt =在这里输入图片描述>


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.

解决方案

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

这篇关于使用ggplot2绘制带孔的土地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆