Friday, 28 October 2016

Pretty patterns in R

The past two weeks has been all about programming in R, a language that's supposed to be the next great thing for statistics and computational biology. I'd already covered it in my last course and it was a source of great pain. This has been less pressured, but still challenging.

The highlight so far has been one section on building graphs. As far as I can tell, the beauty of R is that you can plot an awful lot of things in an awful lot of ways, but making even a simple line graph is waaaay more complicated than you'd imagine. This exercise was all about how to make a fairly complex graph from a matrix of data and then strip it all down to pretty quilt patterns. You should be able to paste this all into R (or RStudio) and run it, though I'd do it line-by-line to avoid formatting errors:


install.packages("ggplot2")
require(ggplot2)
require(reshape2) #reshape2 comes included with the basic R download

#We're going to plot a matrix of random values taken from a normal distribution #[0,1]. Because ggplot2 only accepts dataframes, we'll use reshape2 to 'melt' the matrix into a dataframe.

GenerateMatrix <- function(N){
  M <- matrix(runif(N*N),N,N)
  return(M)
}
GenerateMatrix(3) #The function fills a matrix with N*N random numbers from the uniform distribution. The matrix is N rows by N columns. So, the fill and size match.

M <- GenerateMatrix(10)
M #It's a 10 by 10 matrix
M[1:3,1:3] #Square brackets specify what you want to look at: this views just the first three rows and columns
Melt <- melt(M)
Melt[1:4,] #This object has many many rows (we see the first 4) and 3 columns (Var1, Var2 and value)

ggplot(Melt,aes(Var1,Var2,fill=value))+geom_tile() #The fill=value is what colours each tile according to its value. What a pretty diagram. But, we can make it prettier.

#Saving the plot as an object:
Plot <- ggplot(Melt,aes(Var1,Var2,fill=value))
Plot #It's just the blank plot; we're about to add another layer
Plot <- Plot+geom_tile()
Plot
#Removing the legend:
Plot2 <- Plot+theme(legend.position="none")
Plot2
#Removing all the rest:
Plot2 <- Plot+theme(legend.position="none",
            panel.background=element_blank(),
            axis.ticks=element_blank(),
            panel.grid.major=element_blank(),
            panel.grid.minor=element_blank(),
            axis.text.x=element_blank(),
            axis.title.x=element_blank(),
            axis.text.y=element_blank(),
            axis.title.y=element_blank())
Plot2 #It's now just a lovely blue grid
Bathroom tiling


#Exploring the colours:
Plot3 <- Plot2
Plot3+scale_fill_gradient2()
Flowers for grandma

Plot3+scale_fill_continuous(low="yellow",high="darkgreen")
Aftermath of limeade explosion

Plot3+scale_fill_gradientn(colours=c("red","white","blue"))
Picnic blanket with a touch of chaos

Plot3+scale_fill_gradientn(colours=grey.colors(10)
Dodgy aerial connection (for anyone who remembers what an aerial is)

Plot3+scale_fill_gradientn(colours=rainbow(10))

My eyes are watering

No comments:

Post a Comment