| |
---|
| | |
---|
| | # create initial plot |
---|
| | |
---|
| | if(is.null(ylab)) { |
---|
| | ylab <- "Number of Students" |
---|
| | ylab <- "Count" |
---|
| | } |
---|
| | |
---|
| | plot(h,ylim=c(0, ylimit), xlab=xlab, ylab=ylab, sub=subText, axes=FALSE, ...) |
---|
| | |
---|
| |
---|
| | } |
---|
| | |
---|
| | # function for producing a scatter plot |
---|
| | |
---|
| | #pointlabels are labels to be rendered beside each point on the plot |
---|
| | scatter <- function(pointlabels=NULL, x, y, xlab, ylab, maxx=100, maxy=100, fit=FALSE, ...) { |
---|
| | |
---|
| | # plot x vs y as solid points (thats what the pch=19 means) points |
---|
| | plot(x, y, xlim=c(0,maxx), ylim=c(0,maxy), col="light blue", pch=19, xlab=xlab, ylab=ylab, xaxt='n',yaxt='n', ...) |
---|
| | |
---|
| | # plot some grid lines |
---|
| | abline(h=seq(0, maxy, 10), col="light grey", lty="solid", lwd=0.5) |
---|
| | abline(v=seq(0, maxx, 10), col="light grey", lty="solid", lwd=0.5) |
---|
| | # abline(a=0, b=1, col="light grey", lwd=0.5) |
---|
| | |
---|
| | # plot the graph |
---|
| | points(x, y, xlim=c(0,maxx), ylim=c(0,maxy), col="light blue", pch=19, xlab=xlab, ylab=ylab, ...) |
---|
| | |
---|
| | # cache the original graphics params and shrink the text a bit |
---|
| | op <- par(cex=0.7) |
---|
| | |
---|
| | # y-axis |
---|
| | LEFT <- 2 |
---|
| | axis(LEFT, at=seq(0, maxy, by=10), las=1) |
---|
| | axis(1, at=seq(0, maxx, by=10)) |
---|
| | |
---|
| | par(op) |
---|
| | |
---|
| | mtext("Scatter Plot", side=3, line=0.25) |
---|
| | |
---|
| | # replot the points with darker outline to create a border around the points |
---|
| | points(x,y, col="dark blue") |
---|
| | |
---|
| | # display point labels next to points if they were provided |
---|
| | if(!is.null(pointlabels)) { |
---|
| | # calculate a normalised offset for the x position of the labels |
---|
| | xlabelpos <- x-3.8*(maxx/100) |
---|
| | |
---|
| | # render the labels |
---|
| | text(xlabelpos,y,pointlabels, cex=0.5) |
---|
| | } |
---|
| | |
---|
| | # change plot color |
---|
| | op <- par(col="red") |
---|
| | |
---|
| | co <- list() |
---|
| | co$x <- x |
---|
| | co$y <- y |
---|
| | |
---|
| | valid <- is.na(co$x) | is.na(co$y) |
---|
| | valid <- !valid |
---|
| | |
---|
| | co$x <- co$x[valid] |
---|
| | co$y <- co$y[valid] |
---|
| | |
---|
| | if(fit) { |
---|
| | # render a lowess regression |
---|
| | lines(lowess(co)) |
---|
| | #abline(reggie) |
---|
| | } |
---|
| | |
---|
| | # restore original plot params |
---|
| | par(op) |
---|
| | } |
---|
| | |
---|
| | |
---|
| | # Sums two vectors in a coalescing fashion. |
---|
| | # If either value in x1 or x2 is NA that the result is the non-NA value. |
---|
| | # If both values in x1 or x2 are NA then the result is NA. |
---|
| | # If neither x1 or x2 is NA then the result is x1 + x2. |
---|
| | # coalescing_sum <- function (x1, x2) { |
---|
| | # |
---|
| | # # Iterative version. Keeping around as an example. |
---|
| | # |
---|
| | # results <- 1:length(x1) |
---|
| | # |
---|
| | # for(i in 1:length(x1)) { |
---|
| | # |
---|
| | # if(xor(is.na(x1[i]),is.na(x2[i]))) { |
---|
| | # results[i] <- ifelse(is.na(x1[i]), 0, x1[i]) + ifelse(is.na(x2[i]), 0, x2[i]) |
---|
| | # } else { |
---|
| | # results[i] <- x1[i] + x2[i] |
---|
| | # } |
---|
| | # |
---|
| | # } |
---|
| | # |
---|
| | # return(results) |
---|
| | # } |
---|
| | |
---|
| | # Sums two vectors in a coalescing fashion. |
---|
| | # If either value in x1 or x2 is NA that the result is the non-NA value. |
---|
| | # If both values in x1 or x2 are NA then the result is NA. |
---|
| | # If neither x1 or x2 is NA then the result is x1 + x2. |
---|
| | coalescing_sum <- function (x, y) { |
---|
| | |
---|
| | # keep track of which positions have both values as na |
---|
| | both.na <- is.na(x) & is.na(y) |
---|
| | |
---|
| | # zero nas |
---|
| | x[is.na(x)] <- 0 |
---|
| | y[is.na(y)] <- 0 |
---|
| | |
---|
| | # do the sum |
---|
| | z <- x + y; |
---|
| | |
---|
| | # replace positions with both na as na |
---|
| | z[both.na] <- NA |
---|
| | |
---|
| | return(z) |
---|
| | } |
---|
| | |
---|
| | summaryboxplot <- function(data, formula, drawOutlierLabels=F, extras=NULL, labs=NULL, ...) { |
---|
| | |
---|
| | # add padding if adding extras |
---|
| |
---|
|