如何将SpatialPolygons添加到R中的气泡图

时间:2015-06-08 20:07:50

标签: r geospatial sp

我想使用packge bubble中的函数sp绘制数据,我想添加SpatialPolygons图层。我可以使用spplot轻松绘制数据,但出于某种原因,它不适用于bubble。例如:

library(sp)
# Create SpatialPolygons
Sr1 <- Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2)))
Srs1 <- Polygons(list(Sr1), "s1")
SpP <- SpatialPolygons(list(Srs1))
# Create SpatialPointsDataFrame
pp <- data.frame(x1=2:4,x2=2:4,att=2:4)
coordinates(pp) <- ~x1+x2
# Plot using spplot
spplot(pp, sp.layout=list("sp.polygons", SpP, fill="blue"))
# Plot using bubble
bubble(pp, sp.layout=list("sp.polygons", SpP, fill="blue")) 

Some通过将SpatialPolygons转换为SpatialLines来完成此操作,看起来我可以通过ggplot2(请参阅post)来完成此操作,但我很困惑为什么气泡图不适用于SpatialPolygons。任何让它运作的技巧?

2 个答案:

答案 0 :(得分:2)

解决方法是使用自定义面板功能:

bubble(pp, "att",
       panel=function(...) {
         sp.polygons(SpP, fill="blue")
         sp:::panel.bubble(...)
       }) 

bubble

答案 1 :(得分:0)

我不确定为什么你的ggplot无效,但这是library(sp) library(ggplot2) Sr1 <- Polygon(cbind(c(2,4,4,1,2),c(2,3,5,4,2))) Srs1 <- Polygons(list(Sr1), "s1") SpP <- SpatialPolygons(list(Srs1)) pp <- data.frame(x1=2:4,x2=2:4,att=2:4) spdf <- SpatialPolygonsDataFrame(SpP, data=data.frame(row.names="s1", val=1)) sp_map <- fortify(spdf) gg <- ggplot() gg <- gg + geom_map(data=sp_map, map=sp_map, aes(x=long, y=lat, map_id=id), color="black", fill="blue") gg <- gg + geom_point(data=pp, aes(x=x1, y=x2, size=att), color="orange") gg <- gg + scale_size_continuous(range=c(5,10)) gg <- gg + coord_equal(ylim=c(1.5,5), xlim=c(1,4.5)) gg <- gg + labs(x=NULL, y=NULL) gg <- gg + theme_bw() gg <- gg + theme(panel.grid=element_blank()) gg <- gg + theme(panel.border=element_blank()) gg 的小巷:

    div *{
        display:inline-block;
        /*
        * set your child elements to a set width. If you want them to change
        * size you can use % for the width and adjust this using @media
        */
        margin-left:20px;//use % if you want it to scale.
    }

    div:first-child { 
        margin-left:0px;
    }

<div>
   <div>1</div>
   <div>2</div>
   <div>3</div>
   <div>4</div>
<div>

enter image description here

(我可能已经混淆了气泡的x&amp; y)

你有(IMO)以这种方式更好地控制美学。