# R for Macroecology Functions and plotting. A few words on for  for( i in 1:10 )

## Presentation on theme: "R for Macroecology Functions and plotting. A few words on for  for( i in 1:10 )"— Presentation transcript:

R for Macroecology Functions and plotting

A few words on for  for( i in 1:10 )

A few words on for  for( i in 1:10 ) 12345678910

A few words on for  for( i in 1:10 ) 12345678910 i = 1 Do any number of functions with i print(i) x = sqrt(i)

A few words on for  for( i in 1:10 ) 12345678910 i = 2 Do any number of functions with i print(i) x = sqrt(i)

A few words on for  for( i in 1:10 ) 12345678910 i = 10 Do any number of functions with i print(i) x = sqrt(i)

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y =

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = 12345 i = 1 (so X[i] = 17)

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = 12345 i = 1 (so X[i] = 17) F

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = 12345 i = 2 (so X[i] = 3)

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = 12345 i = 2 (so X[i] = 3) T

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = NA 12345 i = 2 (so X[i] = 3) 8

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = NA 12345 841514

i as an Index X = c(17,3,-1,10,9) Y = NULL for(i in 1:length(X)) { if(X[i] < 12) { Y[i] = X[i] + 5 } 173109 X =Y = NA 12345 841514 This vector (created by the for) indexes vectors X and Y

2-dimension equivalent X = matrix(1:6,ncol = 2,nrow = 3) Y = matrix(NA,ncol = 2,nrow = 3) for(i in 1:nrow(X)) { for(j in 1:ncol(X)) { Y[i,j] = X[i,j]^2 } 14 X = 25 36 NA Y = NA

2-dimension equivalent X = matrix(1:6,ncol = 2,nrow = 3) Y = matrix(NA,ncol = 2,nrow = 3) for(i in 1:nrow(X)) { for(j in 1:ncol(X)) { Y[i,j] = X[i,j]^2 } 14 X = 25 36 NA Y = NA ijij

2-dimension equivalent X = matrix(1:6,ncol = 2,nrow = 3) Y = matrix(NA,ncol = 2,nrow = 3) for(i in 1:nrow(X)) { for(j in 1:ncol(X)) { Y[i,j] = X[i,j]^2 } 14 X = 25 36 1NA Y = NA ijij 1 1

2-dimension equivalent X = matrix(1:6,ncol = 2,nrow = 3) Y = matrix(NA,ncol = 2,nrow = 3) for(i in 1:nrow(X)) { for(j in 1:ncol(X)) { Y[i,j] = X[i,j]^2 } 14 X = 25 36 116 Y = 4NA ijij 112112 121121

2-dimension equivalent X = matrix(1:6,ncol = 2,nrow = 3) Y = matrix(NA,ncol = 2,nrow = 3) for(i in 1:nrow(X)) { for(j in 1:ncol(X)) { Y[i,j] = X[i,j]^2 } 14 X = 25 36 116 Y = 425 936 ijij 112233112233 121212121212

Onward to today’s topics!  Looking more at functions  Plotting your data

Packages  Sets of functions for a particular purpose  We will explore some of these in detail install.packages() require(package.name) CRAN!

Function help Syntax Arguments Return

Function help

Writing your own functions  Why bother?  We often have blocks of code that we want to execute many times, with small changes  Repetitive code is hard to read and likely to contain errors  Think about what variables the function should work on, and what the function should produce myFunction = function(argument, argument...) { stuff more stuff return(anObject) }

Defining a function SayHi = function(input) { print(paste(“Hello”,input)) } SayHi(“Brody”)

Defining a function SayHi = function(input) { print(paste(“Hello”,input)) } SayHi(“Brody”) [1] “Hello Brody”

Defining a function SayHi = function(input) { print(paste(“Hello”,input)) } SayHi(“Brody”) [1] “Hello Brody” SayHi() Error in paste("Hello", input) : argument "input" is missing, with no default

Defining a function SayHi = function(input) { print(paste(“Hello”,input)) } SayHi(“Brody”) [1] “Hello Brody” SayHi() Error in paste("Hello", input) : argument "input" is missing, with no default Functions (usually) only have access to the variables given as arguments! input = “Bob” SayHi() Error in paste("Hello", input) : argument "input" is missing, with no default

Defining a function with defaults SayHi2 = function(input = “Sven”) { print(paste(“Hello”,input)) } SayHi2(“Brody”) [1] “Hello Brody” SayHi2() [1] “Hello Sven”

Things to remember about functions  Use them whenever you have chunks of repeated code  Remember to use return() to have the function return the desired object  Not always necessary, sometimes you might just want a function to plot something, or print something  Local/Global  Functions only have access to variables passed as arguments  Changes to variables to and new variables defined within the function are not available outside the function

A break to try things out Vector Function Number Value

Plotting  For creating a plot  plot()  hist()  For drawing on a plot  points()  segments()  polygons()  For controlling how plots look  par()  Make a new plotting window  x11()

plot() x = 1:10 y = 10:1 plot(x,y)

plot() x = 1:10 y = 10:1 plot(x,y,main = “A plot”,xlab = “Temperature”, ylab = “Pirates”)

type = “l”“b””h” “o”“s”

type = “l”“b””h” “o”“s”

Plotting size and characters cex = 2 or cex = 3

Plotting size and characters pch = 10, cex = 3 pch = A, cex = 3pch = A, cex = x

Color  By name  “blue” or “dark grey”...  By function  grey()  rainbow()  rgb()

Color x = rep(1:10,10) y = rep(1:10,each=10) plot(x,y)

Color x = rep(1:10,10) y = rep(1:10,each=10) plot(x,y,pch = 15,cex = 2)

Color x = rep(1:10,10) y = rep(1:10,each=10) plot(x,y,pch = 15,cex = 2,col = “dark green”)

Color x = rep(1:10,10) y = rep(1:10,each=10) plot(x,y,pch = 15,cex = 2,col = rgb(0.8,0.1,0.2))

Color x = rep(1:10,10) y = rep(1:10,each=10) plot(x,y,pch = 15,cex = 2,col = rgb(seq(0,1,by = 0.01),0.1,0.2))

Drawing on plots  points(x,y) adds points to existing plots (with very similar options to plot() )  segments(x0,y0,x1,y1) draws lines from points to other points  polygons()

The wonderful world of par()  70 different options to control your plots!

Plotting to a file  pdf(), bmp()  dev.off()

Some examples All created entirely within R!

One last fun thing  Scatterplots of massive data can be hard to read 170265 data points

2-d histogram with hexagonal bins Now the structure in the data is clearer

Hexagonal 2-d histograms  hexbin() function in the package hexbin  Additional powerful plotting tools are found in the grid package, which provides a whole different approach to plotting

Similar presentations