2014-01-22 200 views
3

內繪製的再生用,請考慮以下數據:GGPLOT2/GIS多邊形區域

library(rgdal) 
library(ggplot2) 
library(rgeos) 

download.file("http://spatialanalysis.co.uk/wp-content/uploads/2010/09/London_Sport.zip", 

destfile = "London_Sport.zip") 
unzip("London_Sport.zip") 

projection="+proj=merc" 

london_shape = readOGR("./", layer="london_sport") 

# Create random points 
set.seed(1) 
points = data.frame(long=rnorm(10000, mean=-0.1, sd=0.1), lat=rnorm(10000, mean=51.5, sd=0.1)) 
points = SpatialPoints(points, proj4string=CRS("+proj=latlon")) 

# Transform data to our projection 
london = spTransform(london_shape, CRS(projection)) 
points = spTransform(points, CRS(projection)) 

# Keeps only points inside London 
intersection = gIntersects(points, london, byid = T) 
outside = apply(intersection == FALSE, MARGIN = 2, all) 
points = points[which(!outside), ] 

# Blank theme 
new_theme_empty <- theme_bw() 
new_theme_empty$line <- element_blank() 
new_theme_empty$rect <- element_blank() 
new_theme_empty$strip.text <- element_blank() 
new_theme_empty$axis.text <- element_blank() 
new_theme_empty$plot.title <- element_blank() 
new_theme_empty$axis.title <- element_blank() 
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit") 

# Prepare data to ggplot 
london = fortify(london) 
points = as.data.frame(points) 

我想積點的密度圖。我可以通過使用stat_bin2d這樣做:

ggplot() + 
    geom_polygon(data=london, aes(x=long,y=lat,group=group), fill="black") + 
    stat_bin2d(data=points, aes(x=long,y=lat), bins=40) + 
    geom_path(data=london, aes(x=long,y=lat,group=id), colour='white') + 
    coord_equal() + 
    new_theme_empty 

但是,這導致密度廣場的某些部分可以被繪製倫敦之外:

Density

我怎麼能只繪製密度圖在倫敦?

+1

如果你要繪製的東西已經是多邊形內,你可以設置線的*色*爲多邊形爲白色,按原樣繪製。如果不是這種情況,請參閱[rgeos軟件包]中的'gDifference'函數(http://cran.r-project.org/web/packages/rgeos/index.html)。與往常一樣,如果您提供了一個可重複使用的小例子,那麼您可以幫助其他人。 –

+0

只是放了一個可重複的例子:) –

+1

這個鏈接可能會有所幫助:http://spatial.ly/2013/12/introduction-spatial-data-ggplot2/ – tonytonov

回答

3

我找到了答案,得到的是倫敦的邊界框和倫敦本身(使用gDifference)之間的區別的多邊形,並將其繪製成白色之上。我認爲這種方法的缺點是:1)如果某些方塊仍然出現在它後面,則必須手動增加多邊形的尺寸。 2)你不能使用複雜背景的繪圖主題。因此,如果任何人有更好的答案,我會留下一段時間的問題。

下面的代碼:

library(rgdal) 
library(ggplot2) 
library(rgeos) 

projection="+proj=merc" 

#London boroughs polygons 
download.file("http://spatialanalysis.co.uk/wp-content/uploads/2010/09/London_Sport.zip", destfile = "London_Sport.zip") 
unzip("London_Sport.zip") 
london = readOGR("./", layer="london_sport") 
london = spTransform(london, CRS(projection)) 

# Generate random points 
set.seed(1) 
points = data.frame(long=rnorm(10000, mean=-0.1, sd=0.1), lat=rnorm(10000, mean=51.5, sd=0.1)) 
points = SpatialPoints(points, proj4string=CRS("+proj=latlon")) 
points = spTransform(points, CRS(projection)) 

# Keep only points inside London 
intersection = gIntersects(points, london, byid = TRUE) 
inside = apply(intersection == TRUE, MARGIN = 2, any) 
points = points[which(inside), ] 

# Create a bounding box 10% bigger than the bounding box of London 
x_excess = ([email protected]['x','max'] - [email protected]['x','min'])*0.1 
y_excess = ([email protected]['y','max'] - [email protected]['y','min'])*0.1 
x_min = [email protected]['x','min'] - x_excess 
x_max = [email protected]['x','max'] + x_excess 
y_min = [email protected]['y','min'] - y_excess 
y_max = [email protected]['y','max'] + y_excess 
bbox = matrix(c(x_min,x_max,x_max,x_min,x_min, 
       y_min,y_min,y_max,y_max,y_min), 
       nrow = 5, ncol =2) 
bbox = Polygon(bbox, hole=FALSE) 
bbox = Polygons(list(bbox), "bbox") 
bbox = SpatialPolygons(Srl=list(bbox), pO=1:1, [email protected]) 

# Get the Polygon that is the difference between the bounding box and London 
outside = gDifference(bbox,london) 

# Blank theme 
new_theme_empty <- theme_bw() 
new_theme_empty$line <- element_blank() 
new_theme_empty$rect <- element_blank() 
new_theme_empty$strip.text <- element_blank() 
new_theme_empty$axis.text <- element_blank() 
new_theme_empty$plot.title <- element_blank() 
new_theme_empty$axis.title <- element_blank() 
new_theme_empty$plot.margin <- structure(c(0, 0, -1, -1), unit = "lines", valid.unit = 3L, class = "unit") 

# Prepare data for ggplot 
london = fortify(london) 
points = as.data.frame(points) 
outside = fortify(outside) 

# Plot! 
ggplot() + 
    geom_polygon(data=london, aes(x=long,y=lat,group=group), fill="black") + 
    stat_bin2d(data=points, aes(x=long,y=lat), bins=40) + 
    geom_path(data=london, aes(x=long,y=lat,group=id), colour='white') + 
    geom_polygon(data=outside, aes(x=long,y=lat), fill='white') + 
    coord_equal() + 
    new_theme_empty 

Plot