Creating a zipcode map in R? - r

I want to create a zipcode map for Dallas.
I have this shapefile which should include all postal codes within it (Street files)
I've been using this as a resource
below is an example of what I would like to create, and color the zipcode map in as well for certain regions I'm discussing at the time

The data isn't ideal for making something exactly like what you've linked to, but you can still get close.
After unzipping the downloaded data:
library(tidyverse)
library(sf)
dallas_streets <- sf_read('unzipped_folder/')
ggplot(sample_frac(dallas_streets, .05)) + #large file, 5% used for example
geom_sf(aes(color = POSTAL_R)) +
theme(legend.position = 'none')
Should get you here:
Color palette needs to be adjusted, labels could be added, and geometries joined (or unioned) to get closer.
If you're really looking for a zip code map of Dallas, you should try to find a shapefile meant for that purpose.
A little closer:
dallas_streets %>%
sample_frac(.3) %>%
group_by(POSTAL_L) %>%
summarize(geometry = st_convex_hull(st_union(geometry))) %>%
ggplot() +
geom_sf(aes(fill = as.numeric(POSTAL_L))) +
geom_sf_text(aes(label = POSTAL_L)) +
scale_fill_viridis_c(option = "C")
group_by, then summarize for a new geometry based on unioned convex hulls gets close to the actual zip code boundaries with only 30% of the data.

Related

Map FAO fishing areas in R

I would like to make a map in R that colours in the FAO Fishing Areas according to a data set (in my case, length data of shark species).
I would prefer to do a choropleth map in ggplot but other types of maps are also fine. Worst case scenario a base map of FAO areas that I can add bubbles to. Even just an existing base map of FAO areas would be great. Any suggestions welcome!
I went to this page and clicked through to find this link to retrieve a GeoJSON file:
download.file("http://www.fao.org/fishery/geoserver/fifao/ows?service=WFS&request=GetFeature&version=1.0.0&typeName=fifao:FAO_AREAS_CWP&outputFormat=json", dest="FAO.json")
From here on, I was following this example from the R graph gallery, with a little help from this SO question and these notes:
library(geojsonio)
library(sp)
library(broom)
library(ggplot2)
library(dplyr) ## for joining values to map
spdf <- geojson_read("FAO.json", what = "sp")
At this point, plot(spdf) will bring up a plain (base-R) plot of the regions.
spdf_fortified <- tidy(spdf)
## make up some data to go with ...
fake_fish <- data.frame(id = as.character(1:324), value = rnorm(324))
spdf2 <- spdf_fortified %>% left_join(fake_fish, by = "id")
ggplot() +
geom_polygon(data = spdf2, aes( x = long, y = lat, group = group,
fill = value), color="grey") +
scale_fill_viridis_c() +
theme_void() +
theme(plot.background = element_rect(fill = 'lightgray', colour = NA)) +
coord_map() +
coord_sf(crs = "+proj=cea +lon_0=0 +lat_ts=45") ## Gall projection
ggsave("FAO.png")
notes
some of the steps are slow, it might be worth looking up how to coarsen/lower resolution of a spatial polygons object (if you just want to show the picture, the level of resolution might be overkill)
to be honest the default sequential colour scheme might be better but all the cool kids seem to like "viridis" these days so ...
There are probably better ways to do a lot of these pieces (e.g. set map projection, fill in background colour for land masses, ... ?)

Shading a map with underlying data in Julia

I want to create a map of Germany where each state is shaded according to its gross domestic product. I know how to do this in R (and put the code below). Is there a possibility to do this in Julia in an equally simple way?
library(tidyverse)
library(ggplot2)
library(sf)
shpData = st_read("./geofile.shp")
GDPData <- read.delim("./stateGDP.csv", header=FALSE)
GDPData <- rename(GDPData,StateName=V1,GDP=V2)
GDPData %>%
left_join(shpData) ->mergedData
ggplot(mergedData) + geom_sf(data = mergedData, aes(fill = BIP,geometry=geometry)) + coord_sf(crs = st_crs(mergedData))-> pBIP1
You'd load the Shapefile and use Plots to plot it.
The ideomatic code is something like
using Plots, Shapefile, CSV
shp = Shapefile.shapes(Shapefile.Table("geofile.shp"))
GDPData = CSV.read("stateGDP.csv")
plot(shp, fill_z = GDPData.V2')
Note the ' which transposes the values to a column vector - this will tell Plots to apply the colors to individual polygons.

ggplot, ggsave & coord_map/quickmap: how to save large spatial objects and get the projection right?

I have a largish polyline shapefile (Bavarian rivers, which can be accessed here) which I would like to plot and save via ggplot. This can easily be done via e.g. this code:
library(ggplot2)
library(rgdal)
library(sp)
library(rgeos)
riv <- readOGR(paste0(getwd(),"\\rivers_bavaria","rivers_bavaria"))
riv1 <- subset(riv,WDM=="1310"|WDM=="1320")
riv2 <- subset(riv,WDM=="1330")
p <- ggplot() +
geom_line(data=riv1, aes(x=long, y=lat, group=group), color="dodgerblue", size=1) +
geom_line(data=riv2, aes(x=long, y=lat, group=group), color="dodgerblue")
ggsave(paste0(getwd(),"\\riv.tiff",p,device="tiff",units="cm",dpi=300)
This is not exactly efficient, due to the large file size, but it works. However, without further specifying aspect ratio or projection, the dimensions of the output file are defined by the plot window - not desirable for maps. This can be remedied by using coord_quickmap().
p1 <- ggplot() +
geom_line(data=riv1, aes(x=long, y=lat, group=group), color="dodgerblue", size=1) +
geom_line(data=riv2, aes(x=long, y=lat, group=group), color="dodgerblue") +
coord_quickmap()
p1
Unfortunately, the projection is completely off. I have tried coord_map() for a better result, but due to the large file size, it takes forever and is therefore not a realistic option. Simplifying the polyline via gLinemerge() produces a much smaller object, but cannot be handled by ggplot, as it is a SpatialLines object. Using fortify() or data.frame() to coerce it into a ggplot-friendly data frame format also produces Error: ggplot2 doesn't know how to deal with data of class SpatialLines.
I'm therefore desperately looking for a workflow that will allow me to plot and save this kind of spatial data in good quality with ggplot. Any suggestions will be much appreciated!
Here's a quick walkthrough with sf. I recommend the sf vignettes and docs to see more details of any of the functions. I'm first reading the shapefile in as an sf object using sf::st_read, then filtering, mutating, and selecting the same as you would in dplyr to get a smaller version of the shape.
library(tidyverse)
library(sf)
rivers_sf <- st_read("rivers_bavaria/rivers_bavaria.shp") %>%
filter(WDM %in% c("1310", "1320", "1330")) %>%
mutate(name2 = ifelse(WDM == "1330", "river 2", "river 1")) %>%
select(name2, NAM, geometry)
The object is pretty big, and will be very slow to plot, so I simplified it by uniting the geometries by name, then using st_simplify. There's also rmapshaper::ms_simplify, which uses Mapshaper and which I prefer for better control over how much information you keep. Then to show a CRS transformation, I picked a projection from Spatial Reference for Germany.
riv_simple <- rivers_sf %>%
group_by(name2, NAM) %>%
summarise(geometry = st_union(geometry)) %>%
ungroup() %>%
st_simplify(preserveTopology = T, dTolerance = 1e6) %>%
st_transform(31493)
The dev version of ggplot2 on GitHub has a function geom_sf for plotting different types of sf objects. To get this version, run devtools::install_github("tidyverse/ggplot2").
geom_sf has some quirks, and works a little differently from other geoms, but it's pretty versatile. I believe it's being included in the next CRAN release. geom_sf has corresponding stat_sf and coord_sf. By default, it plots graticule lines; to turn those off, add coord_sf(ndiscr = F).
ggplot(riv_simple) +
geom_sf(aes(size = name2), color = "dodgerblue", show.legend = "line") +
scale_size_manual(values = c("river 1" = 1, "river 2" = 0.5)) +
theme_minimal() +
coord_sf(ndiscr = F)
Hope that helps you get started!

Bizarre polygons from a shapefile

I'm trying to apply a shapefile to a ggmaps map, but it's giving me really weird results. The shapefile in question is the "Statistical Local Area" (groups similar to postcode) shapefile from the Australian Bureau of Statistics available here.
Normally I might think that it's a problem of cut off edge points, but I'm hitting it even at zoom level 1 (in fact it looks even worse):
Here's some code I used to produce the charts above:
library(tidyverse)
library(ggmap)
library(rgdal)
slas <- readOGR(dsn="SLA",layer="SLA11aAust")
aus4 <- get_map("Australia",zoom=4)
ggmap(aus4)
ggmap(aus4)+
geom_polygon(data=slas, aes(x=long,y=lat))
aus1 <- get_map("Australia",zoom=1)
ggmap(aus1)
ggmap(aus1)+
geom_polygon(data=slas, aes(x=long,y=lat))
Am I doing something wrong, or is the shapefile incorrectly configured somehow?
I think you just need to (optionally) fortify the variable slas, don't forget to group and make the boundaries visible with a color:
slas <- fortify(slas, region = "SLA_CODE11")
ggmap(aus4) +
geom_polygon(data = slas2, color = "white", aes(x = long, y = lat, group = group))

Chloropleth map in R looks bizarre

I have a data set of ~25,000 people that have complete postal codes. I'm trying to create a map of Canada at the FSA level but always seem to get bizarre results. I would appreciate if someone could point out where my mistakes are happening or what I'm missing.
library(rgeos)
library(maptools)
library(ggplot2)
fsas = readShapeSpatial('./Resources/FSA/gfsa000a11a_e.shp')
data = fortify(fsas, region = 'CFSAUID')
data$fsa = factor(data$id)
data$id = NULL
df$fsa = substr(df$Postal, 1, 3)
prvdr_cts = data.frame(table(df$fsa)) ; names(prvdr_cts) = c('fsa', 'ct')
plot.data = merge(data, prvdr_cts, by = 'fsa')
ggplot(plot.data, aes(x = long, y = lat, group = group, fill = ct)) +
geom_polygon() +
coord_equal()
This is my resulting plot
I got my map file from http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/bound-limit-2011-eng.cfm under 'Forward sortation areas'. df has two columns Person ID and FSA.
I've seen similar problems before when I forgot group = group (as #r.bot points out), but as you have that I wonder if it's because the shapefile you're using is highly detailed.
I suggest trying the sf package to load shapefiles which has superseded using readShapePoly. This has the advantages of being faster and you don't need to fortify(). I've also simplified the shapefile somewhat to make plotting faster. Finally, you need to development version of ggplot2 to use the new geom_sf() (ATOW):
install.packages(c("rmapshaper", "sf", "devtools"))
devtools::install_github("tidyverse/ggplot2")
library("ggplot2")
fsas = sf::read_sf("gfsa000a11a_e.shp")
fsas = rmapshaper::ms_simplify(fsas, keep = 0.05)
ggplot(fsas) + geom_sf()

Resources