Created
December 30, 2016 05:38
-
-
Save valentinitnelav/881e4ff47481b109cc0cfc7be9d71f43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# define the function: | |
Interaction.Plots_fun <- function(MyMatrix.lst, data.lst) { | |
# storing default par() for reverting par to original/default values | |
par.default <- par() | |
# on.exit(par(par.default)) # when exiting function par(), restore to default (? - need to test!) | |
# par(par.default) # or simply call each time this line | |
# open a pdf device with desired parameters | |
pdf(file=paste0("Mueller ", format(Sys.time(), "%Y-%m-%d %H-%M-%p"), ".pdf"), onefile=TRUE, width=24, height=8.27) | |
# remember that an A4 is 8.27 × 11.69 in. Here you want a landscape format width > height! | |
# the width and height of the graphics region are in inches. The default values are 7. | |
# to fit the grphs one must adjust the width and height; width=24, height=8.27 fits all graphs at all locations | |
# format(Sys.time(), "%Y-%m-%d %H-%M-%p") # adds the time like: "2016-06-16 08-42-AM" # !! avoid special characters in the name of the file (e.g ":") | |
my.cex <- 1 # store a universal value for the character expansion factor (default=1) [for adjusting text size in mtext()] | |
# run for each matrix in MyMatrix.lst (that is, for each location) | |
for (i in seq_along(MyMatrix.lst)) { | |
# try to cath possible errors | |
possibleError <- tryCatch({ | |
# __ plot interaction web __ # | |
# par(mar=par.default$mar-2.5) # bipartite plot functions don't react to margins mar() options !!! | |
bipartite::plotweb(MyMatrix.lst[[i]], arrow="down", | |
labsize=1, text.rot=90, y.width.low=0.01, y.width.high=0.01, high.y=1.2, low.y=0.9) | |
# display text to inform about the location | |
mtext(paste0("Interaction network at location: ", i, " - ", names(MyMatrix.lst[i]), "; Altitude (range of means): ", | |
paste0(range(as.numeric(data.lst[[i]]$altitude_mean)), "m", collapse=" - ")), | |
side=3, outer=T, line=-2, cex=my.cex) | |
# __ plot interaction matrix __ # | |
# this function has a horrible adjustment of labels! | |
bipartite::visweb(MyMatrix.lst[[i]], type="nested", labsize=0.5) | |
# display text to inform about the location | |
mtext(paste0("Interaction matrix at location: ", i, " - ", names(MyMatrix.lst[i]), "; Altitude (range of means): ", | |
paste(range(as.numeric(data.lst[[i]]$altitude_mean)), "m", collapse=" - ")), | |
side=3, outer=T, line=-1, cex=my.cex) | |
# __ plot altitude frequnecy table and isect order details __ # | |
par(par.default) # this is needed because bipartite::visweb is distorting par parameters somehow without warnings! took me a while to figure this out :) | |
par(mfrow = c(2,2)) # split the screen in 4 | |
# compute altitude frequnecy table | |
alt.freq <- table(data.lst[[i]]$altitude_range, dnn = "Altitude classes and their frequencies:") | |
# dnn - the names to be given to the dimensions in the result (just a trick to get some text above the table) | |
# plot histogram of altitudes on first (up-left) screen with plot() - it works because alt.freq si a frequency table; can as well skip type = "h" | |
plot(alt.freq, type = "h", lwd=5, frame.plot=FALSE, main="Altitude classess - histogram") | |
# plot histogram of insect order on the up-right screen | |
plot(table(data.lst[[i]]$InsectOrderNew_WD), lwd=5, frame.plot=FALSE, | |
xlab="InsectOrderNew_WD", ylab="Freq", main="Insect order - histogram") | |
# plot the content of the altitude frequnecy table on the down-left | |
textplot(capture.output(alt.freq), cex=1.5, valign="top") | |
par(par.default) # return par to default values (par.default was saved previosly) | |
# display text to inform about the location (on top of the page) | |
mtext(paste0("Details at location: ", i, " - ", names(MyMatrix.lst[i])), side=3, outer=T, line=-1, cex=my.cex) | |
}, error=function(e) e) | |
# in case of inheriting an error event: | |
if(inherits(possibleError, "error")){ | |
frame() # plot empty graph | |
# display text to inform about the error | |
mtext(paste0("Plotting error for location: ", i, " - ", names(MyMatrix.lst[i]), ", due to small matrix dimensions"), side=3, outer=T, line=-2, cex=my.cex) | |
mtext(paste0("number of rows in matrix: ", dim(MyMatrix.lst[[i]])[1]), side=3, outer=T, line=-5, cex=my.cex) | |
mtext(paste0("number of columns in matrix: ", dim(MyMatrix.lst[[i]])[2]), side=3, outer=T, line=-7, cex=my.cex) | |
next | |
} | |
} | |
dev.off() # close the pdf device | |
warnings() # show any wornings | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment