A site to help Biochemists learn R.

Starting points

Friday, 3 July 2015

Map of our Training Day Delegates...

So I have decided to use R to make a map of the delegates that attended our R for Biochemists Training Day. This is partly Friday Fun. It also illustrates the feasibility of using R for drawing a map. Finally, it illustrates some useful things with ggplot:

  • a little bit about colours and overplotting
  • using data from different data frames with different layers of a plot
Here the final map:



The darker circles indicate that more people came from there. This is the "alpha" argument of the geom_point() function. 
As a scientist, I like the labels on the longitude and latitude labels on the axis. 


Here is the script including some of the pictures along the way:


# draw a map of the UK with R. 

library(maps)       # Provides functions that let us plot the maps
library(mapdata)    # Contains the hi-resolution points that mark out the countries.

# use the map() function which comes from the maps package
map('worldHires',
    c('UK', 'Ireland', 'Isle of Man','Isle of Wight', 'Wales:Anglesey'))
# very small map

# make it larger by limiting the x and y axis which corresponds to
# longitude and latitude
map('worldHires',
    c('UK'),
    xlim=c(-10,2), ylim=c(50,60))

# import delagate info
data <- read.csv("http://science2therapy.com/data/locations.csv")
points(data$longitude,data$latitude,col=2,pch=18)


# we can also do this in ggplot probably better...
library(ggplot2)

# use map_data()function to create a data.frame containing the map of the UK
uk<-map_data("worldHires", region = "UK")

# create the object m with the map in it. 
# we can make the map different colours
# see here for more about colours in R
# http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf
m <- ggplot() +
  geom_polygon(data=uk, 
               aes(x=long, y=lat, group=group),
               fill="gray88") +   # see link for a list of R colours
  scale_x_continuous(limits = c(-10,3)) +
  scale_y_continuous(limits =c(49,60.9))

# add the points on the plot telling us where the longitude and latitude of various places
# note the data is coming from a different data.frame to the map
m <- m +  geom_point(data=data, 
                aes(x=longitude, y=latitude), 
                alpha=0.5, colour="red", size = 5) 

# change the theme to make the map nicer
m <- m + theme_bw()

# add a title
m + ggtitle("Where the R for Biochemists delagates came from...")



END OF SCRIPT

Sources of advice and help:


No comments:

Post a Comment

Comments and suggestions are welcome.