R Create a spatial bubble region that overlays the US basemap and other spatial layers as needed
I'm trying to create a nice bubble patch overlaid on top of the US basemap (I could import a shapefile if preferred, but I used base R files.
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, I adapted from another overflow column ( Create Bubble Plot in R with Satellite Map ), I can overlay the bubble plot on the US map. However, it appears very slowly, the degree is too dense, it is limited in the box, I cannot add other layers to the plot from what I can tell, and the basemap 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 am adding points to the map, but they are not sorted 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, how can I use the 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 on a better way to build 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 so that I can: a) control the colors (fill and lines) and b) add additional layers (for example, another layer in the forms).
Thanks in advance for any advice.
source to share
This might be helpful. I approached your question using my own path. It may be a simpler operation to achieve what you are probably trying to do. GADM
Excellent for maps . But some packages have already received cards. Here you can easily get a map of the states as follows. Then you can draw the map using ggplot2
. geom_path
draws USA and geom_point
adds data points to myData
. If you want to control the size of the 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")
source to share
Below is a similar approach to using jjazzurro ggplot and your original approach, but
- uses a different basemap
- further reduces the number of polygons (you don't need hi-res borders for the bubble area)
- uses
geom_map
vsgeom_path
orgeom_polygon
- uses the Albers projection in
coord_map
- gets rid of unwanted chart map
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 easy to add other layers.
source to share