1 / 52

R for Macroecology

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 ). 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. A few words on for. for( i in 1:10 ). 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. i = 1 Do any number of functions with i

wang-jensen
Télécharger la présentation

R for Macroecology

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. R for Macroecology Functions and plotting

  2. A few words on for • for( i in 1:10 )

  3. A few words on for • for( i in 1:10 ) 1 2 3 4 5 6 7 8 9 10

  4. A few words on for • for( i in 1:10 ) 1 2 3 4 5 6 7 8 9 10 i = 1 Do any number of functions with i print(i) x = sqrt(i)

  5. A few words on for • for( i in 1:10 ) 1 2 3 4 5 6 7 8 9 10 i = 2 Do any number of functions with i print(i) x = sqrt(i)

  6. A few words on for • for( i in 1:10 ) 1 2 3 4 5 6 7 8 9 10 i = 10 Do any number of functions with i print(i) x = sqrt(i)

  7. 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 } } 17 3 -1 10 9 X =

  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 } } 17 3 -1 10 9 X = Y =

  9. 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 } } 17 3 -1 10 9 X = Y = 1 2 3 4 5 i = 1 (so X[i] = 17)

  10. 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 } } F 17 3 -1 10 9 X = Y = 1 2 3 4 5 i = 1 (so X[i] = 17)

  11. 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 } } 17 3 -1 10 9 X = Y = 1 2 3 4 5 i = 2 (so X[i] = 3)

  12. 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 } } T 17 3 -1 10 9 X = Y = 1 2 3 4 5 i = 2 (so X[i] = 3)

  13. 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 } } 17 3 -1 10 9 NA 8 X = Y = 1 2 3 4 5 i = 2 (so X[i] = 3)

  14. 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 } } 17 3 -1 10 9 NA 8 4 15 14 X = Y = 1 2 3 4 5

  15. 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 } } 17 3 -1 10 9 NA 8 4 15 14 X = Y = 1 2 3 4 5 This vector (created by the for) indexes vectors X and Y

  16. 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 } } 1 4 NA NA 2 5 NA NA X = Y = 3 6 NA NA

  17. 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 } } i j 1 4 NA NA 2 5 NA NA X = Y = 3 6 NA NA

  18. 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 } } i j 1 1 1 4 1 NA 2 5 NA NA X = Y = 3 6 NA NA

  19. 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 } } i j 1 2 1 1 1 2 1 4 1 16 2 5 4 NA X = Y = 3 6 NA NA

  20. 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 } } i j 1 2 1 2 1 2 1 1 2 2 3 3 1 4 1 16 2 5 4 25 X = Y = 3 6 9 36

  21. Onward to today’s topics! • Looking more at functions • Plotting your data

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

  23. Function help Syntax Arguments Return

  24. Function help

  25. 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) }

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

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

  28. 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

  29. 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

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

  31. 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

  32. A break to try things out Function Vector Number Value

  33. 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()

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

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

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

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

  38. Plotting size and characters cex = 2 or cex = 3

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

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

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

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

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

  44. 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))

  45. 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))

  46. Drawing on plots • points(x,y) adds points to existing plots (withverysimilar options to plot()) • segments(x0,y0,x1,y1) draws lines from points to other points • polygons()

  47. The wonderful world of par() • 70 different options to control your plots!

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

  49. Some examples All created entirely within R!

  50. One last fun thing • Scatterplots of massive data can be hard to read 170265 data points

More Related