## ------------------------------------------------------------------------ BirdData <- data.frame( Tarsus = c(22.3, 19.7, 20.8, 20.3, 20.8, 21.5, 20.6, 21.5), Head = c(31.2, 30.4, 30.6, 30.3, 30.3, 30.8, 32.5, 31.6), Weight = c(9.5, 13.8, 14.8, 15.2, 15.5, 15.6, 15.6, 15.7), Wingcrd = c(59, 55, 53.5, 55, 52.5, 57.5, 53, 55), Species = c('A', 'A', 'A', 'A', 'A', 'B', 'B', 'B') ) ## ---- histo, fig.cap = "**Fig.** Some histograms"------------------------ # set layout and margins par(mfrow = c(2, 2), mar = c(3, 3, 2, 1), lwd = 2) hist(BirdData$Head, main = "default histogram") hist(BirdData$Head, breaks = 10, main = "breaks = 10") hist(BirdData$Head, breaks = seq(from = 30, to = 33, by = 0.25), main = "breaks = seq()" ) # plot probability density distribution hist(BirdData$Head, breaks = seq(from = 30, to = 33, by = 0.25), freq = FALSE, main = "probability density" ) ## ---- scatter, fig.cap = "**Fig.** Some scatterplots"-------------------- par(mfrow = c(2, 2), mar = c(4, 4, 2, 1), lwd = 2) plot(Head ~ Tarsus, data = BirdData, xlab = 'Tarsus (mm)', ylab = 'Head Size (mm)', main = 'Head vs Tarsus', pch = 20, col = Species, cex = 3) plot(Head ~ Wingcrd, data = BirdData, xlab = 'Wingcrd (m)', ylab = 'Head Size (mm)', main = 'Head vs Wing', pch = 20, col = Species, cex = 3) plot(Head ~ Weight, data = BirdData, xlab = 'Weight (kg)', ylab = 'Head Size (mm)', main = 'Head vs Weight', pch = 20, col = Species, cex = 3) # Here, we add a legend to this plot legend('topleft', pch = c(20, 20), col = c(1, 2), legend = c('Species A', 'Species B') ) plot(Head ~ Weight, data = BirdData, xlab = 'Species', ylab = 'Head Size (mm)', main = 'Head vs Species', pch = c(20, 23), col = 1:2, cex = Tarsus/5) # Another legend legend('topleft', pch = c(20, 23), col = c(1, 2), legend = c('Species A', 'Species B') ) ## ------------------------------------------------------------------------ BFdata <- read.table(file = 'http://www.simonqueenborough.info/R/basic/data/birdflu_corrected.txt', header = TRUE, sep = '\t') # select cases columns only BFcases <- BFdata[, c('cases03', 'cases04', 'cases05', 'cases06', 'cases07', 'cases08')] names(BFdata) str(BFdata) ## ------------------------------------------------------------------------ Cases <- colSums(BFcases) names(Cases) <- 2003:2008 Cases ## ---- pies, fig.cap = "**Fig**. Some pie charts"------------------------- par(mfrow = c(2, 2), mar = c(3, 3, 2, 1)) pie(Cases, main = "Ordinary pie chart") pie(Cases, col = gray(seq(0.4, 1.0, length = 6)), clockwise = TRUE, main = "Grey colours") pie(Cases, col = rainbow(6), clockwise = TRUE, main = "Rainbow colours") #install.packages('plotrix') library(plotrix) pie3D(Cases, labels = names(Cases), explode = 0.1, main = "3D pie chart", labelcex = 0.6) ## ---- stacked-bar, fig.cap = 'An improvement to the pie chart, the stacked bar plot'---- # To get the 'stacked' bars, we need to convert our vector to a matrix (barplot() will only stack a matrix). barplot(as.matrix(Cases), horiz = TRUE, xlim = c(0, 400)) ## ------------------------------------------------------------------------ BFdata <- read.table(file = 'http://www.simonqueenborough.info/R/basic/data/birdflu_corrected.txt', header = TRUE, sep = '\t') # select cases columns only BFdeaths <- BFdata[, c('deaths03', 'deaths04', 'deaths05', 'deaths06', 'deaths07', 'deaths08')] names(BFdeaths) str(BFdeaths) # Make a vector as before Deaths <- colSums(BFdeaths) names(Deaths) <- 2003:2008 Deaths ## ---- barp, fig.cap = "**Fig.** Some bar plots"-------------------------- par(mfrow = c(2, 2), mar = c(3, 3, 2, 1), lwd = 2) barplot(Cases, main = "default barplot of Cases") # Create a new variable Counts <- cbind(Cases, Deaths) # makes a matrix barplot(Counts, main = "default with 2-column matrix" ) barplot(t(Counts), col = gray(c(0.5, 1)), main = "stacked") barplot(t(Counts), beside = TRUE, main = "beside") ## ---- barp-birds, fig.cap = 'Bird head without error bars'--------------- head.mean <- tapply(BirdData$Head, INDEX = BirdData$Species, FUN = mean) head.sd <- tapply(BirdData$Head, INDEX = BirdData$Species, FUN = sd) barplot(head.mean, xlab = "Species", ylab = "Head Length (mm)", ylim = c(0, 35), col = 1:2) ## ------------------------------------------------------------------------ xvals <- barplot(head.mean, plot = FALSE) xvals ## ---- barp-birds2, fig.cap = 'Bird head with error bars'----------------- barplot(head.mean, xlab = "Species", ylab = "Head Length (mm)", ylim = c(0, 35), col = 1:2) arrows(x0 = xvals, y0 = head.mean, # arrow from ... x1 = xvals, y1 = head.mean + head.sd, # ... to lwd = 2, angle = 90, length = 0.1) ## ---- boxpl, fig.cap = "**Fig.** Some box plots", message = FALSE-------- par(mfrow = c(2, 2), lwd = 2) plot(Head ~ Species, data = BirdData, main = "default boxplot with plot()") boxplot(Head ~ Species, data = BirdData, main = "default boxplot with boxplot()") boxplot(Head ~ Species, data = BirdData, notch = TRUE, main = "A notched boxplot") # install.packages('vioplot') library(vioplot) bird.A <- BirdData$Head[BirdData$Species == 'A'] bird.B <- BirdData$Head[BirdData$Species == 'B'] #vioplot(bird.A, bird.B) #mtext(side = 3, text = "default violin plot", line = 1.8, font = 2) ## ---- stripch, fig.cap = "**Fig.** Some strip charts"-------------------- par(mfrow = c(2, 2), mar = c(3, 3, 2, 1), lwd = 2, las = 1, cex = 1.25) stripchart(BirdData$Head ~ BirdData$Species, xlim = c(29, 34), ylim = c(0.5, 2.5), main = "default strip chart", col = 'grey80') stripchart(BirdData$Head ~ BirdData$Species, xlim = c(29, 34), ylim = c(0.5, 2.5), main = "add jitter to y", col = 'grey80', method = 'jitter') stripchart(BirdData$Head ~ BirdData$Species, xlim = c(29, 34), ylim = c(0.5, 2.5), main = "add summary data", col = 'grey80', method = 'jitter') points(x = head.mean, y = 1:2, pch = 20, cex = 2) arrows(x0 = head.mean, y0 = 1:2, x1 = head.mean + head.sd, y1 = 1:2, angle = 90, length = 0.1, lwd = 2) arrows(x0 = head.mean, y0 = 1:2, x1 = head.mean - head.sd, y1 = 1:2, angle = 90, length = 0.1, lwd = 2)