2016-05-16 19 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)))+