[R] Projecting data on a world map using long/lat
Patrick Hausmann
patrick.hausmann at uni-bremen.de
Sat Dec 11 12:46:37 CET 2010
Hi Mathijs,
this should work:
library(maptools)
library(ggplot2)
gpclibPermit()
theme_set(theme_bw())
#setwd("C:\\foo") point to your local dir
# Data: http://thematicmapping.org/downloads/world_borders.php
world.shp <- readShapeSpatial("TM_WORLD_BORDERS-0.3.shp")
# check for region-id - Use "FIPS"
head(world.shp at data)
## see licence, not GPL
world.shp.p <- fortify.SpatialPolygonsDataFrame(world.shp, region="FIPS")
world <- merge(world.shp.p, world.shp, by.x="id", by.y="FIPS")
head(world)
dim(world)
# only the worldmap
p <- ggplot(data=world, aes(x=long, y=lat, group=group)) +
geom_polygon(fill="#63D1F4")
p <- p + geom_path(color="white") + coord_equal()
ggsave(p, width=11.69, height=8.27, file="world_map.jpg")
## Add some locations
cities <- read.table(textConnection("
long lat city pop
-58.381944 -34.599722 'Buenos Aires' 11548541
14.25 40.833333 Neapel 962940"), header = TRUE)
p1 <- p + geom_point(data = cities, aes(group = NULL), shape=5,
color='black')
ggsave(p1, width=11.69, height=8.27, file="world_map_2.jpg")
Regards,
Patrick
Am 10.12.2010 17:53, schrieb mathijsdevaan:
>
> Thanks for the suggestions, but I am not there yet (I'm a real novice). In
> the code provided by Patrick (see below), I changed the shape input (from
> sids to world) which I downloaded here:
> http://thematicmapping.org/downloads/world_borders.php. As a result I also
> need to change the "CNTY_ID" and "id" in the code, but I have no idea what
> to put there. Could you please help me? Thanks!
>
> Mathijs
>
> library(maptools)
> library(ggplot2)
> gpclibPermit()
>
> myshp<- readShapeSpatial(system.file("shapes/sids.shp",
> package="maptools"))
>
> ## see licence, not GPL
> myshp.points<- fortify.SpatialPolygonsDataFrame(myshp,
> region="CNTY_ID")
>
> shpm<- merge(myshp.points, myshp, by.x="id", by.y="CNTY_ID")
>
> head(shpm)
>
> p<- ggplot(shpm, aes(long, lat, group=group, fill=NWBIR74))
> p<- p + geom_polygon() + geom_path(color="white") + coord_equal()
>
> ## Add some locations
> cities<- read.table(textConnection("
> long lat city val
> -78.644722 35.818889 Raleigh 323
> -80.843333 35.226944 Charlotte 510
> -82.555833 35.58 Asheville 400"), header = TRUE)
>
> p<- p + geom_point(aes( fill=NULL, group = NULL, size=val),
> data = cities, color= 'black')
> p
More information about the R-help
mailing list