[R-sig-Geo] Mapping Singapore data

boB Rudis bob at rudis.net
Fri Jan 15 06:18:56 CET 2016


This can get you started (if you're the ggplot2 sort of person).
raster::getData will pull the data without a manual download. I'm not
sure what you need admin-level-wise, so I went with Admin1.
ggplot2::fortify takes the spatial data and makes it something ggplot2
can work with. It would normally use the polygon id, but you can
substitute other identifiers from the shapefile. i went with the
region name.

i made a dummy data frame for a random choropleth and then proceed to
plot the base map and fill layers.

viridis is a nice scale, but you'd ideally want to cut or cut2 some
levels since it's unlikely you have data that should be on a
continuous scale (just guessing tho).

the projection i chose is a pretty gd one for that region with those
lat/lon boundaries.

library(raster)
library(rgeos)
library(maptools)
library(ggplot2)
library(ggalt)
library(ggthemes)
library(viridis)

sg <- getData(country="SGP", level=1)

sg_map <- fortify(sg, region="NAME_1")

choro_dat <- data.frame(region=sg at data$NAME_1,
                        value=sample(100, nrow(sg at data)),
                        stringsAsFactors=FALSE)

gg <- ggplot()
gg <- gg + geom_map(data=sg_map, map=sg_map,
                    aes(x=long, y=lat, map_id=id),
                    color="#b2b2b200", fill="#ffffff00", size=0.15)
gg <- gg + geom_map(data=choro_dat, map=sg_map,
                    aes(fill=value, map_id=region),
                    color="#b2b2b2", size=0.15)
gg <- gg + coord_proj("+proj=aea +lon_0=103.8474")
gg <- gg + scale_fill_viridis(name="Measure")
gg <- gg + theme_map()
gg <- gg + theme(legend.position="bottom")
gg

On Thu, Jan 14, 2016 at 8:03 PM, Richa Agarwal <agaricha at gmail.com> wrote:
> Hi Guys,
>
> I have some experience with R though relatively new to mapping geo data.
>
> I am trying to plot a heat map for Singapore and was trying to figure out
> what would be the best way to start. Reading up online I downloaded a .rds
> file from http://www.gadm.org/country. Though sure how to proceed now.
>
> Thanks
> Richa
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list