R根据需要创建覆盖美国和其他空间图层的底图的空间气泡图 [英] R Create a Spatial Bubble Plot That Overlays A basemap of the US and other spatial layers as needed

查看:194
本文介绍了R根据需要创建覆盖美国和其他空间图层的底图的空间气泡图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在美国底图上叠加一个漂亮的泡泡图(如果这是首选的,我可以导入一个shapefile,但我一直在使用R底图。

  library(ggplot2,sp,raster,maps,mapdata,maptools,ggmap,rgeos)
myData = data.frame(name = c(Florida ,科罗拉多,加州,哈佛,黄石),
lat = c(28.1,39,37,42,44.6),
long = c(-81.6, - 105.5,-120,-71,-110),
pop = c(280,156,128,118,202))

使用下面的代码,我改编自另一个堆栈溢出帖子(在R中使用卫星地图创建泡泡图),我可以在美国的地图上覆盖泡沫图,但是这种渲染非常缓慢,程度太紧,它被限制在一个盒子里,我无法将其他图层添加到绘图中,并且底图很厚,而且n不是视觉干净。

  xy < -  myData [,c(long,lat)] 
nl< - getData('GADM',country =USA,level = 1)#raster data,format SpatialPolygonsDataFrame
nl< - gSimplify(nl,tol = 0.01,topologyPreserve = TRUE)
#强制多边形轮廓到SpatialLines对象
spl< - list(sp.lines,as(nl,SpatialLines))
SPDF< - SpatialPointsDataFrame(coords = xy,data = myData)
coordinate(myData)< -c(lat,long)
projection(SPDF)< - + proj = longlat + ellps = WGS84 + datum = WGS84 + no_defs + towgs84 = 0,0,0
坐标(SPDF)[1:5,]#从空间坐标中获取数据帧
bubble(SPDF,pop,sp.layout = spl,main =这是它!)

我可以使用此代码绘制一个不错的底图。我给地图添加了点数,但它们的大小并未通过数据中的弹出列来确定。我可以为这张地图添加额外的图层。但是我可以控制点的大小和符号本身,就像我可以使用泡沫图吗?

  map(database =世界,ylim = c(45,90),
xlim = c(-160,-50),col =grey80,
fill = TRUE,projection =gilbert,
orientation = c(90,0,225))

coord< - mapproject(myData $ lon,myData $ lat,proj =gilbert,orientation = c(90,0,225))
点(coord,pch = 20,cex = 1.2,col =red)

任何人都可以请指导我在R中绘制泡泡图的最佳方式,我可以调整泡泡图中符号的填充和轮廓,并且我可以添加干净的底图,我可以a)控制(填充和线条)和b)添加额外的图层(例如另一个shapefile图层)。

预先感谢您的任何建议。

解决方案

这可能会有帮助。我用我自己的方式接近你的问题。这可能是一个简单的操作来实现你可能要做的事情。对于地图, GADM 非常好。但是,一些软件包已经获得了地图。在这里,您可以通过以下方式轻松获取州地图。然后,您可以使用 ggplot2 来绘制地图。 geom_path 绘制美国图形,并且 geom_point 将数据点添加到 myData ggplot2 中气泡的大小,你可以在 size > AES 。

 图书馆(地图)
图书馆(ggplot2)

#获取美国地图
usa< - map_data(state)

#绘制地图并将数据点添加到myData

ggplot()+
geom_path(data = usa,aes(x = long,y = lat,group = group))+
geom_point(data = myData,aes(x = long,y = lat,size = pop),color =red)


I'm trying to produce a nice bubble plot overlaid on top of a basemap of the US (i could import a shapefile if that is preferred but i've been using the R basemaps.

library(ggplot2,sp,raster,maps,mapdata,maptools,ggmap,rgeos)
myData = data.frame(name=c("Florida","Colorado","california","Harvard","Yellowstone"),
                lat=c(28.1,39,37,42,44.6), 
                long=c(-81.6,-105.5,-120,-71,-110),
                pop=c(280,156,128,118,202))

Using this code below, that i adapted from another stack overflow post (Create bubble plot in R using satellite map), i am able to overlay a bubble plot on a map of the US. However this renders very slowly, the extent is too tight, it is bounded in a box, i'm not able to add other layers to the plot from what i can tell, and the base map is thick and not visually clean.

xy <- myData[,c("long", "lat")]
nl <- getData('GADM', country="USA", level=1) #raster data, format SpatialPolygonsDataFrame
nl <- gSimplify(nl, tol=0.01, topologyPreserve=TRUE)
# coercing the polygon outlines to a SpatialLines object
spl <- list("sp.lines", as(nl, "SpatialLines"))
SPDF <- SpatialPointsDataFrame(coords=xy, data=myData)
coordinates(myData) <- c("lat", "long")
projection(SPDF)<- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"
coordinates(SPDF)[1:5,] #retrieves spatial coordinates form the dataframe
bubble(SPDF, "pop", sp.layout=spl, main="This is It!")

I can draw a nice basemap using this code. I add points to the map but they are not sized by the pop column in my data. And i can add additional layers to this map. But can i control the size of the points and the symbol itself like i can using a bubble plot?

map(database= "world", ylim=c(45,90), 
    xlim=c(-160,-50), col="grey80", 
    fill=TRUE, projection="gilbert", 
    orientation= c(90,0,225))

coord <- mapproject(myData$lon, myData$lat, proj="gilbert",orientation=c(90, 0, 225))
points(coord, pch=20, cex=1.2, col="red") 

Can anyone please guide me towards the best way to plot a bubble map in R where i can adjust the fill and outline of the symbols in the bubble map, And i can add a clean basemap, that i can a) control the colors of (fill and lines) and b) add additional layers to (for instance another shapefile layer).

Thank you in advance for any advice.

解决方案

This may be helpful. I approached your question using my own way. This may be a simpler operation to achieve what you are probably trying to do. For maps, GADM is great. But, some packages already got maps. Here, you can easily get the States map in the following way. Then, you can draw the map using ggplot2. geom_path draws the US, and geom_point adds the data points in myData.If you want to control size of bubbles in ggplot2,you can use size in aes.

library(map)
library(ggplot2)

# Get US map
usa <- map_data("state")

# Draw the map and add the data points in myData

ggplot() +
geom_path(data = usa, aes(x = long, y = lat, group = group)) +
geom_point(data = myData, aes(x = long, y = lat, size = pop), color = "red") 

这篇关于R根据需要创建覆盖美国和其他空间图层的底图的空间气泡图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆