2016-05-16 18 views
0

我最近開始用R編碼,到目前爲止,它已經是一段頗具道路的旅程。他們中的大部分我都能通過搜索互聯網來解決。但是,現在我遇到了一個我無法修復的問題。我在stackoverflow上找到了一種類似的線程(How to fill in the contour fully using stat_contour),但它在我的情況下不起作用,ggmap + ggplot似乎無法一起工作。關閉ggmap上的多邊形(stat_contour)

所以,問題如下。我正在研究阿姆斯特丹的紅燈區是否對當地企業有影響。我通過創建顯示阿姆斯特丹市中心業務密度的情節來實現這一點。我已經使用這個代碼,使其:

# Background map 
geo <- geocode("Amsterdam", source="google") 
lon <- geo$lon - .000 
lat <- geo$lat + .000 
map <- get_map(c(lon = lon, lat = lat), zoom=14,maptype = "satellite", color="color") 

p <- ggmap(map) + theme(legend.position = c(1, 1), legend.justification = c(1,1)) + 
    xlab("Longitude") + ylab("Latitude") 

# Map boundaries 
#xmin = 4.86774509503174 
#xmax = 4.92267673565674 
#ymin = 52.3534171572766 
#ymax = 52.386956071714 

# RLD Map (so it can be read by ggplot2) 
rldmap <- fortify(rld) 
rld2map <- fortify(rld2) 
natmap <- fortify(nat) 

# Ticks 
mid <- 250 
mi <- 0 
ma <- 500 
r <- (ma-mi)/5 
breaks = seq(mi,ma,r) 

# Density 
ncells <- 150 

dens2000 <- kde2d(bedrijven2000$LONG, bedrijven2000$LAT, n = ncells) 
densdf2000 <- data.frame(expand.grid(x = dens2000$x, y = dens2000$y), z = as.vector(dens2000$z)) 

densmap2000 <- p + geom_point(aes(x = x, y = y), data = coords2000, alpha = .5, size = 1) + 
    stat_contour(aes(x,y,z=z,fill = ..level..), data=densdf2000,geom="polygon", alpha=.3, inherit.aes = F) + 
    scale_fill_gradientn(name="density", breaks = breaks, colours=rev(brewer.pal(7,"Spectral"))) + 
    geom_polygon(aes(x=long, y=lat, group=group), fill='grey', size=.2,color='green', data=rldmap, alpha=0) + 
    geom_polygon(aes(x=long, y=lat, group=group), fill='grey', size=.2,color='green', data=rld2map, alpha=0) + 
    geom_polygon(aes(x=long, y=lat, group=group), fill='grey', size=.2,color='green', data=natmap, alpha=0) + 
    geom_point(aes(x = x, y = y), data = coords,color = 'yellow',alpha = 1, size = 1, shape=10) + 
    annotate('text', x=4.892, y=52.374, label = "Amsterdam", col="white") + 
    ggtitle("Business Density in Amsterdam year=2000") 

plot(densmap2000) 

使用此代碼提供了以下的輸出: output density plot Amsterdam

這裏的問題是,顯示密度停止多邊形的線條,當他們接近情節的邊界,這給了一個令人不安的形象。

因此,我的問題是,我怎樣才能使多邊形忘記圖的邊界,並使它們連續?

回答

0

管理來回答這個問題我自己到底,感謝這個線程:Truncated Density Polygons with ggmap

,答案就在兩個部分:

1,編輯在背景圖

ggmap擴展和maprange(地圖,程度= 「正常」,maprange = FALSE)

2,編輯在情節代碼,添加:

coord_cartesian(XLIM = C(米(4.86774509503174),max(4.92267673565674)), ylim = c(min(52.3534171572766),max(52.386956071714)))+