如何将 SpatialPolygons 添加到 R 中的气泡图
How to add a SpatialPolygons to a bubble plot in R
我想使用包 sp
中的函数 bubble
来绘制数据,我想添加一个 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 have done this through transforming the SpatialPolygons
into SpatialLines
and it looks like I could do it through ggplot2 (see post),但我很困惑为什么气泡图不适用于 SpatialPolygons
。有什么技巧可以让它发挥作用吗?
我不确定为什么你的 spplot
不起作用,但这正是 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
(我可能混淆了气泡的 x 和 y)
通过这种方式,您 (IMO) 可以更好地控制美学。
解决方法是使用自定义面板功能:
bubble(pp, "att",
panel=function(...) {
sp.polygons(SpP, fill="blue")
sp:::panel.bubble(...)
})
我想使用包 sp
中的函数 bubble
来绘制数据,我想添加一个 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 have done this through transforming the SpatialPolygons
into SpatialLines
and it looks like I could do it through ggplot2 (see post),但我很困惑为什么气泡图不适用于 SpatialPolygons
。有什么技巧可以让它发挥作用吗?
我不确定为什么你的 spplot
不起作用,但这正是 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
(我可能混淆了气泡的 x 和 y)
通过这种方式,您 (IMO) 可以更好地控制美学。
解决方法是使用自定义面板功能:
bubble(pp, "att",
panel=function(...) {
sp.polygons(SpP, fill="blue")
sp:::panel.bubble(...)
})