2014-01-22 3 views
3

Для целей воспроизводства, рассмотрим следующие данные:ggplot2/ГИС Заговор внутри полигона области

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

Если все вещи вы хотите построить уже внутри многоугольника, вы можете просто установить * цвет * линий многоугольника на белый и сюжет как есть. Если это не так, см. Функцию 'gDifference' в пакете [rgeos] (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

Смежные вопросы