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")
The following take a similar approach to @jazzurro's use of ggplot and your initial approach, but
- uses a different base map
- further reduces the polygons (you don't need hi-res borders for a bubble plot)
- uses
geom_map
vsgeom_path
orgeom_polygon
- uses the Albers projection in
coord_map
- gets rid of map chart junk
library(maptools)
library(mapproj)
library(rgeos)
library(rgdal)
library(ggplot2)
# for theme_map
devtools::source_gist("33baa3a79c5cfef0f6df")
# nice US map GeoJSON
us <- readOGR(dsn="http://eric.clst.org/wupl/Stuff/gz_2010_us_040_00_500k.json", layer="OGRGeoJSON")
# even smaller polygons
us <- SpatialPolygonsDataFrame(gSimplify(us, tol=0.1, topologyPreserve=TRUE),
data=us@data)
# don't need these for the continental base map
us <- us[!us$NAME %in% c("Alaska", "Hawaii", "Puerto Rico", "District of Columbia"),]
# for ggplot
map <- fortify(us, region="NAME")
# your data
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))
# the map
gg <- ggplot()
# the base map
gg <- gg + geom_map(data=map, map=map,
aes(x=long, y=lat, map_id=id, group=group),
fill="#ffffff", color="#0e0e0e", size=0.15)
# your bubbles
gg <- gg + geom_point(data=myData,
aes(x=long, y=lat, size=pop), color="#AD655F")
gg <- gg + labs(title="Bubbles")
# much better projection for US maps
gg <- gg + coord_map(projection="albers", lat=39, lat1=45)
gg <- gg + theme_map()
gg <- gg + theme(legend.position="bottom")
gg <- gg + theme(plot.title=element_text(size=16))
gg
This should make it pretty straightforward to add other layers as well.
来源:https://stackoverflow.com/questions/27328372/r-create-a-spatial-bubble-plot-that-overlays-a-basemap-of-the-us-and-other-spati