background preloader

Making maps

Facebook Twitter

Mapping Public Opinion: A Tutorial. At the upcoming 2012 summer meeting of the Society of Political Methodology, I will be presenting a poster on Isarithmic Maps of Public Opinion. Since last posting on the topic, I have made major improvements to the code and robustness of the modeling approach, and written a tutorial that illustrates the production of such maps. This tutorial is in a very rough draft form, but I will post it here when it is finalized. (An earlier draft had some errors, and so I have taken it down.) To leave a comment for the author, please follow the link and comment on his blog: David B. Sparks » r. R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

R to GeoJSON. GitHub recently introduced the ability to render GeoJSON files on their site as maps here, and recently introduced here support for TopoJSON, an extension of GeoJSON can be up to 80% smaller than GeoJSON, support for other file extensions (.topojson and .json), and you can embed the maps on other sites (so awesome). The underlying maps used on GitHub are Openstreet Maps. A recent blog post showed how to convert .shp or .kml files to GeoJSON to then upload to GitHub here. The approach used Ruby on the command line locally to convert the geospatial files to GeoJSON. Can we do this in R? Perhaps others have already done this, but there's more than one way to do anything, no? I'm not aware of a converter to GeoJSON within R, but there is a web service that can do this, called Ogre. The service lets you POST a file, which then converts to GeoJSON and gives it back to you. So here's the protocol. 1.

. # install.packages('httr') library(httr) 2. 3. Shapefiles 4. Acer spicatum distribution (points) R GIS: Terrain Analysis for Polygons as Simple as it Gets! Use R to Bulk-Download Digital Elevation Data with 1" Resolution. Maps in R: Plotting data points on a map. In the introductory post of this series I showed how to plot empty maps in R. Today I'll begin to show how to add data to R maps. The topic of this post is the visualization of data points on a map. We will use a couple of datasets from the OpenFlight website for our examples.

After loading the airports.dat file let's visualize the first few lines. Latitude and longitude are reported for every airport in the dataset. Then we can easily lay the airports over the map: Adding dimensions In the introductory post I mentioned that ggmap actually builds on the ggplot graphics engine, thus all the strengths of ggplot are available when mapping data with ggmap.

Let's load another dataset from OpenFlights in R. Starting from the routes dataset, let's count the both number of routes departing from and arriving to a particular airport. Then, let's add the info on departing and arriving flights to the airports dataset (which contains the coordinates data.) What's next View (and download) the full code:

Displaying Your Data in Google Earth Using R2G2. Have you ever wanted to easily visualize your ecology data in Google Earth? R2G2 is a new package for R, available via R CRAN and formally described in this Molecular Ecology Resources article, which provides a user-friendly bridge between R and the Google Earth interface. Here, we will provide a brief introduction to the package, including a short tutorial, and then encourage you to try it out with your own data! Nils Arrigo, with some help from Loren Albert, Mike Barker, and Pascal Mickelson (one of the contributors to Recology), has created a set of R tools to generate KML files to view data with geographic components. Instead of just telling you what the tools can do, though, we will show you a couple of examples using publically available data.

Note: a number of individual files are linked to throughout the tutorial below, but just in case you would rather download all the tutorial files in one go, have at it (tutorial zip file). Provincial Map using GADM. Add a frame to a map. Here is a function that adds a frame of alternating colors to a map (un-projected). One defines the extension of each bar (in degrees) and an optional width of the bars (in inches). It uses the "joinPolys" function of the package to trim the bars near the map corners where the axes meet. the map.frame function:Read more » To leave a comment for the author, please follow the link and comment on his blog: me nugget. R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Package maptools. Coke vs Soda vs Pop : Linguistic trends analyzed with Twitter and R. Growing up in Australia, for me a carbonated drink like Pepsi or Fanta or lemonade was always just a "soft drink". (Also, 'lemonade' in Australia was something different to 'lemonade' in the US; it's something close to 7-Up.) So when I moved to Seattle, it was surprising to me that all such things were called "pop". And then I travelling across the US, and realised it was also "soda" (which, to an Australian, is exclusively club soda), and even sometimes "coke". Not capital-C Coke, but "coke", meant any generic soft drink.

Mindoro Digital Elevation Map. A Napa Valley wine tasting map, made with R and ggmap. R has had a maps package available since the very early days. It's great for simple geographic maps, but the political boundaries can be out of date. For more detailed maps, you can also download shape files and use the sp package to draw borders directly. But for accurate and attractive maps of countries, roads and satellite imagery, nothing beats online sources like OpenStreetMap and Google Maps. At the useR! I wanted to try out the ggmap package, so I thought it would be fun to try and solve a problem that's been bugging me for a while: it's hard to find a good map of wineries you can visit in Napa Valley.

First, I imported the Wine Project database as a data frame in R, and filtered out any wineries with an missing address (or where only a P.O. Addresses <- with(visit.wine, paste(Address, City, "CA", sep=", ")) locs <- geocode(addresses) And here's the final map: install.packages(c("ggmap","mapproj")) Happy mapping! Related: Napa Valley wine tasting map: interactive version. Use IUCN API with R & XPath. A gWidgets GUI for climate data. If you haven’t worked with the gWidgets package it’s worth some time exploring it which is what I’ve been doing for a little paleo project I’ve been working on. After struggling with the few demos and tutorials I could find I went ahead and bought the book: Programming Graphical User Interfaces in R.

Luckily the book was a little better than the tutorials which just scratch the surface. GUI programming, in my mind, is unlike other programming and I had many false starts, primarily struggling with lexical scoping issues and getting widgets to update based on changes made to other widgets and the underlying data. The only reliable method I could find was <<- Yikes. I’m sure over time I’d figure away around making these type of “globals” it ended up looking pretty but being ugly under the hood. Lets Start with the Top Window Pretty basic but it took a while to get this to work. The glabel(0 widgets on the right show you how many stations are loaded and the min and max time. A Napa Valley wine tasting map, made with R and ggmap. R has had a maps package available since the very early days.

It's great for simple geographic maps, but the political boundaries can be out of date. For more detailed maps, you can also download shape files and use the sp package to draw borders directly. But for accurate and attractive maps of countries, roads and satellite imagery, nothing beats online sources like OpenStreetMap and Google Maps. At the useR! 2012 conference last month, David Kahle introduced his ggmap package, which makes it trivial to import such maps into R. You can then use maps as a background layer in a data visualization with ggplot2. I wanted to try out the ggmap package, so I thought it would be fun to try and solve a problem that's been bugging me for a while: it's hard to find a good map of wineries you can visit in Napa Valley. First, I imported the Wine Project database as a data frame in R, and filtered out any wineries with an missing address (or where only a P.O.

And here's the final map: Happy mapping! An exercise in R using local open data. Last week I went to the “Government Open Data Hack Day” ([@,#]godhd on twitter) in Birmingham (UK), organised by Gavin Broughton and James Catell. The idea was to get hold of local open data and try and make use of them in just one day. You can see some of the work done on that day presented here.

It was good fun and I’ve learned a few tricks and resources in the process, which I’m going to go through in this post. I’ll refrain from any data analysis because I know next to nothing about this type of data. (all files here) The data I was given come from nomis and consist of job vacancies in West Midlands for the years 2011 and 2012, broken down by job types. From the spread sheet to an R data frame The first thing to do is to turn this into an R data frame for easier manipulation and reshaping. In a terminal: Select All Code: In R: Now that we have the data in one single data frame, it’s much easier to do something with it. Maps! Which is rather nice for a first go. 3D Maps in R. Talking about elevation, one can also plot a wire frame 3D view of an area using the persp function. Using the same data source from my previous post, 3D view of Marinduque, Philippines was produced using the following code below: #############################################################################################library(maptools)library(raster)alt <- getData('alt', country='PHL')adm <- getData('GADM', country='PHL', leve=1)mar<-(adm[adm$NAME_1=="Marinduque",])maralt<-crop(alt,mar)persp(maralt, exp=0.2,phi=35, xlab="Longitude", ylab="Latitude", zlab="Elevation") ############################################################################################# persp(maralt, exp=0.2,phi=35, xlab="Longitude", ylab="Latitude", zlab="Elevation", shade=0.45, col="green4")############################################################################################# To leave a comment for the author, please follow the link and comment on his blog: R-NOLD.

Visualize your Facebook friends network with R. Great Maps with ggplot2. The above map (and this one) was produced using R and ggplot2 and serve to demonstrate just how sophisticated R visualisations can be. We are used to seeing similar maps produced with conventional GIS platforms or software such as Processing but I hadn’t yet seen one from the R community (feel free to suggest some in the comments). The map contains three layers: buildings, water and the journey segments. The most challenging aspect was to change the standard line ends in geom_segment from “butt” to “round” in order that the lines appeared continuous and not with “cracks” in, see below. I am grateful to Hadley and the rest of the ggplot2 Google Group for the solution. You can see it here. #if your map data is a shapefile use maptools library(maptools) gpclibPermit() geom_segment2 “identity”, position = “identity”, arrow = NULL, …) { GeomSegment2$new(mapping = mapping, data = data, stat = stat, position = position, arrow = arrow, …) } #load spatial data.

#create base plot plon1 plon2. Maps with R, and polygon boundaries. With R, it is extremely easy to draw maps. Let us start with something simple, like French regions. Baptiste mentioned on his blog that shapefiles can be downloaded from website. Hence, if you extract the zip file, it is possible to get claims frequency per region (as done in the course ACT2040), > library(maptools)> library(maps)> departements<-readShapeSpatial("DEPARTEMENT.SHP")> region<-tapply(baseFREQ[,"nbre"],+ as.factor(baseFREQ[,"region"]),sum)/+ tapply(baseFREQ[,"exposition"],+ as.factor(baseFREQ[,"region"]),sum)> depFREQ=rep(NA,nrow(departements))> names(depFREQ)=as.character(+ departements$CODE_REG)> for(nom in names(region)){+ depFREQ[names(depFREQ)==nom] = + region[nom]}> plot(departements,col=gray((depFREQ-.05)*20))> legend(166963,6561753,legend=seq(1,0,by=-.1)/20+.05,+ fill=gray(seq(1,0,by=-.1)),cex=1.25, bty="n") Another application is on earthquakes.

It is possible to use shapefiles of tectonic plates contour, and to relate earthquakes to plates. Create maps with maptools R package. Amateur Mapmaking: Getting Started With Shapefiles. One of the great things about (software) code is that people build on it and out from it… Which means that as well as producing ever more complex bits of software, tools also get produced over time that make it easier to do things that were once hard to do, or required expensive commercial software tools. Producing maps is a fine example of this. Not so very long ago, producing your own annotated maps was a hard thing to do. Then in June, 2005, or so, the Google Maps API came along and suddenly you could create your own maps (or at least, put markers on to a map if you had latitude and longitude co-ordinates available). Since then, things have just got easier. If you want to put markers on a map just given their addresses, it’s easy (see for example Mapping the New Year Honours List – Where Did the Honours Go?).

You can make use of Ordnance Survey maps if you want to, or recolour and style maps so they look just the way you want. Here’s what it looks like: And here’s the result: The undiscovered country – a tutorial on plotting maps in R. Creating beautiful maps with R. Spanish R user and solar energy lecturer Oscar Perpiñán Lamigueiro has written a detailed three-part guide to creating beautiful maps and choropleths (maps color-coded with regional data) using the R language.

Motivated by the desire to recreate this graphic from the New York Times, Oscar describes how he creates similar high-quality maps using R. In Part 1, Oscar grabbed some voting data from Spanish elections and shapefiles (detailed map coordinates) from Spain's Official Statistics department (INE) and set to work in R. He then used the maptools package and lattice graphics to create this choropleth of voting patterns in Spain: In Part 2, he uses the raster package to import pixel-based environmental data from NASA, to create a map that combines population density, land use and land cover in a single map. (Personally, I find it very difficult to interpret such multivariate maps encoded in multiple dimensions of color, but the process is interesting.) Omnia sunt Communia! Maps with R (III) Maps with R (II) Maps with R (I)