Description Usage Arguments Value See Also Examples
Helper functions to draw geographical map based on shapefiles
1 2 3 | getPathShp(geo = "CH", year = 2016)
formatShp(shp)
|
geo |
a character, either 'CH' or 'world' |
year |
a numeric, year for data to use (default 2015) |
shp |
shapefile contents loaded ojbect, as returned by |
a character, the path to the shapefiles folder
a dataframe object
https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | require(rgdal)
require(rgeos)
require(ggplot2)
require(maptools)
path <- getPathShp('world')
layers <- ogrListLayers(path)
# read shapefiles for given layer
world <- readOGR(path, layer = layers[1])
world.df <- formatShp(world)
# plot world map
wm <- ggplot(world.df, aes(x = long, y = lat, group = group)) +
geom_polygon(size = 0.01, aes(fill = FIPS)) +
theme_minimal() +
theme(
legend.position = "none",
panel.grid = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank(),
axis.text = element_blank()
)
wm
# With nice orthographic projection
wm + coord_map("ortho")
# Same with swiss commune map (slow!)
## Not run:
path.ch <- getPathShp('CH', year = 2015)
ch <- readOGR(path.ch, layer = 'municipalities-without-lakes')
ch.df <- formatShp(ch)
# plot swiss commune map
ch.communes <- ggplot(ch.df, aes(x = long, y = lat, group = group)) + geom_polygon(size = 0, aes(fill = GEMNAME)) +
theme_minimal() + theme(legend.position = "none", panel.grid = element_blank(), axis.ticks = element_blank(), axis.title = element_blank(), axis.text = element_blank())
ch.communes
# add the lake
lake.df <- formatShp(readOGR(path.ch, layer = "lakes"))
ch.communes + geom_polygon(data = lake.df, fill = "blue", colour = "blue")
# get all the id and name of communes from the map
bfsnName <- ch.df[!duplicated(ch.df$BFSNR),c('BFSNR', 'GEMNAME')]
bfsnName[order(bfsnName[,1]),]
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.