############ #CTD plots # ############ CTDplot <- function(station) { ####################################################### #Example for how to use the function: #CTDplot(sta0386) #where sta0386 is the first name in the cnv-file for which you want to make a CDT-plot. # #In the import function all the measured variables are included but just four of them #are plotted against depth, i.e. temperature, oxygen, salinity (PSU) and light. #If the latter is not recorded, it will not show up in the plot. #If you want to plot other variables you must change the programming below. #The only thing you need to understand is the latter part called Plotting. #To understand the variable names, you must go into your CTD-file and #look at the section where you find lines starting with "name 0 =", "name 1 =", etc. #This is where variables are explained. The variable names in the syntax below #are the same as in the CTD-file. For example: Depth is called prDM. An exception is #that the names with forward slash (/) and dash (-) have been replaced with #two dots (..) or one dot (.), respectively. It is done like this to get names #that will work under R. #You must set the correct working directory for the function to work. #Example: #setwd('bla/bla/rwork') ########################################################## #Importing a cnv-file to R # ############################ filename <- paste('data/', deparse(substitute(station)),'.cnv',sep='') options(warn=-1) nlines <- grep('*END*', readLines(filename)) #To skip all lines until *END* and only extract # the data from the .cnv file. options(warn=0) ctd.df <-read.table(filename, skip=nlines, header=F, sep="") #Extract the correct variable names from the CTD-file: options(warn=-1) nlines.START <- grep('name 0', readLines(filename))-1 nlines.END <- grep('= flag:', readLines(filename)) nlines.describing.variables <- nlines.END-nlines.START options(warn=0) v1 <- scan(filename, what='raw', skip=nlines.START, nlines=nlines.describing.variables, sep='\n') v2 <- sub(':.*', "", v1) varnames <- as.factor(sub('.*= ', '', v2)) varnames <- gsub('/', '..', varnames) varnames <- gsub('-', '.', varnames) #varnames <- ifelse(varnames=='c0S/m','c0S.m', ifelse(varnames=='c1S/m','c1S.m', ifelse(varnames=='','' names(ctd.df) <- varnames #Removing PAR (photosynthetically active radiation) values lower or equal to 1e-12: #(Do this if the photon-unit repeats the same low number at a given depth and onwards, #indicating that the unit has reached the lower end of its range) ctd.df$par <- ifelse(ctd.df$par<=1e-12, NA, ctd.df$par) #Clean up rm(nlines, nlines.START, nlines.END, nlines.describing.variables, v1, v2, varnames) # Plotting with ggplot ###################### library(ggplot2) # Create an object for an expression with special characters (e.g. degrees Celsius symbol, # to later use as axes labels) xtekst1 <- expression(paste('Temperature ','(', degree,'C)')) # Create an object for a plot of temperature (t068C) against depth (prDM). scale_y_reverse() # reverses the y axis, geom_line() adds the data as a line, xlab() and ylab() create axis labels. # ylab() has been left blank to later add a common y-label to all graphs. p1 <- ggplot(data=ctd.df, aes(x=t068C, y=prDM)) p1 <- p1 + scale_y_reverse() p1 <- p1 + geom_path() p1 <- p1 + xlab(xtekst1) + ylab('') p1 <- p1 + theme_bw(base_size=20) # Plot the other variables. xtekst2 <- expression(paste('Oxygen (ml ', L^-1, ')')) p2 <- ggplot(data=ctd.df, aes(x=sbeox0ML..L, y=prDM)) p2 <- p2 + scale_y_reverse() p2 <- p2 + geom_path() p2 <- p2 + xlab(xtekst2) + ylab('') p2 <- p2 + theme_bw(base_size=20) p3 <- ggplot(data=ctd.df, aes(x=sal00, y=prDM)) p3 <- p3 + scale_y_reverse() p3 <- p3 + geom_path() p3 <- p3 + xlab("Salinity (PSU)") + ylab('') p3 <- p3 + theme_bw(base_size=20) xtekst3 <- expression(paste('PAR (',mu,'Mole ', m^2, sec^-1, ')')) p4 <- ggplot(data=ctd.df, aes(x=par, y=prDM)) + ylim(60,0) p4 <- p4 + geom_path() p4 <- p4 + xlab(xtekst3) + ylab('') p4 <- p4 + theme_bw(base_size=20) # Place multiple figures on one page, using the gridExtra package. ncol= assigns the number of columns, # top= creates a figure heading, left= creates a common y-axis label. library(gridExtra) library(grid) p5 <- grid.arrange(p1,p2,p3,p4, ncol=2, top=textGrob(paste('Station: ', deparse(substitute(station)),sep=''), gp=gpar(fontsize=20)), left=textGrob('Pressure (dbar)', rot=90, vjust=2, gp=gpar(fontsize=20))) grid.draw(p5) #Saving the plot savename <- paste('figures/', deparse(substitute(station)),'.png',sep='') ggsave(file=savename, p5) }