r for macroecology functions and plotting. a few words on for for( i in 1:10 )
TRANSCRIPT
![Page 1: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/1.jpg)
R for Macroecology
Functions and plotting
![Page 2: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/2.jpg)
A few words on for for( i in 1:10 )
![Page 3: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/3.jpg)
A few words on for for( i in 1:10 )
1 2 3 4 5 6 7 8 910
![Page 4: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/4.jpg)
A few words on for for( i in 1:10 )
1 2 3 4 5 6 7 8 910
i = 1Do any number of functions with iprint(i)x = sqrt(i)
![Page 5: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/5.jpg)
A few words on for for( i in 1:10 )
1 2 3 4 5 6 7 8 910
i = 2Do any number of functions with iprint(i)x = sqrt(i)
![Page 6: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/6.jpg)
A few words on for for( i in 1:10 )
1 2 3 4 5 6 7 8 910
i = 10Do any number of functions with iprint(i)x = sqrt(i)
![Page 7: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/7.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X =
![Page 8: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/8.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y =
![Page 9: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/9.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y =
1 2 3 4 5i = 1(so X[i] = 17)
![Page 10: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/10.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y =
1 2 3 4 5i = 1(so X[i] = 17)
F
![Page 11: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/11.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y =
1 2 3 4 5i = 2(so X[i] = 3)
![Page 12: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/12.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y =
1 2 3 4 5i = 2(so X[i] = 3)
T
![Page 13: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/13.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y = NA
1 2 3 4 5i = 2(so X[i] = 3)
8
![Page 14: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/14.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y = NA
1 2 3 4 5
8 415
14
![Page 15: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/15.jpg)
i as an IndexX = c(17,3,-1,10,9)Y = NULLfor(i in 1:length(X))
{if(X[i] < 12)
{Y[i] = X[i] + 5}
}
17
3 -110
9X = Y = NA
1 2 3 4 5
8 415
14
This vector (created by the for) indexes vectors X and Y
![Page 16: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/16.jpg)
2-dimension equivalentX = 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 4X = 2 5
3 6
NA NA
Y = NA NA
NA NA
![Page 17: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/17.jpg)
2-dimension equivalentX = 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 4X = 2 5
3 6
NA NA
Y = NA NA
NA NA
i j
![Page 18: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/18.jpg)
2-dimension equivalentX = 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 4X = 2 5
3 6
1 NA
Y = NA NA
NA NA
i j
1 1
![Page 19: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/19.jpg)
2-dimension equivalentX = 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 4X = 2 5
3 6
1 16
Y = 4 NA
NA NA
i j
112
121
![Page 20: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/20.jpg)
2-dimension equivalentX = 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 4X = 2 5
3 6
1 16
Y = 4 25
9 36
i j
112233
121212
![Page 21: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/21.jpg)
Onward to today’s topics! Looking more at functions Plotting your data
![Page 22: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/22.jpg)
Packages Sets of functions for a particular purpose
We will explore some of these in detail
install.packages()
require(package.name)
CRAN!
![Page 23: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/23.jpg)
Function help
SyntaxArguments
Return
![Page 24: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/24.jpg)
Function help
![Page 25: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/25.jpg)
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 producemyFunction = function(argument, argument . . .)
{stuffmore stuffreturn(anObject)}
![Page 26: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/26.jpg)
Defining a functionSayHi = function(input)
{print(paste(“Hello”,input))}
SayHi(“Brody”)
![Page 27: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/27.jpg)
Defining a functionSayHi = function(input)
{print(paste(“Hello”,input))}
SayHi(“Brody”)[1] “Hello Brody”
![Page 28: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/28.jpg)
Defining a functionSayHi = function(input)
{print(paste(“Hello”,input))}
SayHi(“Brody”)[1] “Hello Brody”
SayHi()Error in paste("Hello", input) : argument "input" is missing, with no default
![Page 29: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/29.jpg)
Defining a functionSayHi = 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
![Page 30: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/30.jpg)
Defining a function with defaultsSayHi2 = function(input = “Sven”)
{print(paste(“Hello”,input))}
SayHi2(“Brody”)[1] “Hello Brody”
SayHi2()[1] “Hello Sven”
![Page 31: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/31.jpg)
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
![Page 32: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/32.jpg)
A break to try things out
VectorFunctio
nNumbe
r
Value
![Page 33: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/33.jpg)
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()
![Page 34: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/34.jpg)
plot()x = 1:10y = 10:1plot(x,y)
![Page 35: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/35.jpg)
plot()x = 1:10y = 10:1plot(x,y,main = “A plot”,xlab = “Temperature”,
ylab = “Pirates”)
![Page 36: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/36.jpg)
type =
“l” “b” ”h”
“o” “s”
![Page 37: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/37.jpg)
type =
“l” “b” ”h”
“o” “s”
![Page 38: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/38.jpg)
Plotting size and characters
cex = 2 or cex = 3
![Page 39: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/39.jpg)
Plotting size and characters
pch = 10, cex = 3 pch = A, cex = 3 pch = A, cex = x
![Page 40: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/40.jpg)
Color By name
“blue” or “dark grey” . . .
By function grey() rainbow() rgb()
![Page 41: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/41.jpg)
Colorx = rep(1:10,10)y = rep(1:10,each=10)plot(x,y)
![Page 42: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/42.jpg)
Colorx = rep(1:10,10)y = rep(1:10,each=10)plot(x,y,pch = 15,cex = 2)
![Page 43: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/43.jpg)
Colorx = rep(1:10,10)y = rep(1:10,each=10)plot(x,y,pch = 15,cex = 2,col = “dark green”)
![Page 44: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/44.jpg)
Colorx = 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))
![Page 45: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/45.jpg)
Colorx = 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))
![Page 46: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/46.jpg)
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()
![Page 47: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/47.jpg)
The wonderful world of par() 70 different options to control your plots!
![Page 48: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/48.jpg)
Plotting to a file pdf(), bmp() dev.off()
![Page 49: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/49.jpg)
Some examples
All created entirely within R!
![Page 50: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/50.jpg)
One last fun thing Scatterplots of massive data can be hard to
read
170265 data points
![Page 51: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/51.jpg)
2-d histogram with hexagonal bins
Now the structure in the data is clearer
![Page 52: R for Macroecology Functions and plotting. A few words on for for( i in 1:10 )](https://reader036.vdocuments.net/reader036/viewer/2022062620/551aea795503465e7d8b4c0b/html5/thumbnails/52.jpg)
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