11-04-2018
The following data objects are usually the inputs in R plots:
Graph the number of males per lek
males <- c(5, 4, 6, 4, 9) # males per lek plot(males)
cols <- topo.colors(5, alpha = 0.7)
Let's add a title, a line to connect the points, and some color:
plot(males, type = "o", col = "blue") #blue points overlayed by a line title(main = "Males per lek", col.main = "red", font.main = 4)
Now let's add a red line for females and specify the y-axis range directly so it will be large enough to fit the female data:
# Define 2nd vector females <- c(2, 5, 4, 5, 12) # Graph y axis that ranges from 0 to 12 plot(males, type = "o", col = "blue", ylim = c(0,12)) # females red dashed line and square points lines(females, type = "o", pch = 22, lty = 2, col = "red") # title red, bold/italic font title(main = "Males & females per lek", col.main = "red", font.main = 4)
Now let's add a red line for females and specify the y-axis range directly so it will be large enough to fit the female data:
Next let's change the axes labels to match our data and add a legend
# Calculate range from 0 to max value of males and females g_range <- range(0, females, males) #Turn off axes and annotations (axis labels) so we can specify them ourself plot(males, type = "o", col = "blue", ylim = g_range, axes = FALSE, ann = FALSE)
Next let's change the axes labels to match our data and add a legend
Add the axis and box
plot(males, type = "o", col = "blue", ylim = g_range, axes = FALSE, ann = FALSE) # Make x axis using lek labels axis(1, at = 1:5, lab = c("CCL","STR","SAT","SJA","SUR")) # Make y axis with horizontal labels that display ticks at # every 4 marks. 4*0:g_range[2] is equivalent to c(0,4,8,12). axis(2, las = 1, at = 4*0:g_range[2]) # Create box around plot box()
Add the axis and box
Add the data, title and legend
plot(males, type = "o", col = "blue", ylim = g_range, axes = FALSE, ann = FALSE) axis(1, at = 1:5, lab = c("CCL","STR","SAT","SJA","SUR")) axis(2, las = 1, at = 4*0:g_range[2]) box() lines(females, type = "o", pch = 22, lty = 2, col = "red") title(main = "Males and females in leks", col.main = "red", font.main = 4) title(xlab = "Leks", col.lab = rgb(0,0.5,0)) title(ylab = "Total", col.lab = rgb(0,0.5,0)) legend(1, g_range[2], c("males","females"), cex = 0.8, col = c("blue","red"), pch = 21:22, lty = 1:2);
Add the data, title and legend
Bar graph of male data
barplot(males)
Graph males with specified labels for axes.
barplot(males, main = "Males", xlab = "Leks", ylab = "Total", names.arg=c("CCL", "STR", "SAT", "SJA", "SUR"), border="blue", density = c(10, 20, 30, 40, 50))
Now let's graph the total number of birds per lek using some color and show a legend:
hummers <- cbind(males, females, juve = c(4, 4, 6, 6, 16)) barplot(hummers, main = "Hummingbirds", ylab = "Total", beside = TRUE, col = cols) # Place the legend at the top-left corner with no frame legend("topleft", c("CCL", "STR", "SAT", "SJA", "SUR"), cex = 0.6, bty = "n", fill = cols)
Now let's graph the total number of birds per day using some color and show a legend:
Check out the "AirPassengers" data set (deaths per year):
data(AirPassengers) AirPassengers <- t(matrix(AirPassengers, ncol = 12)) colnames(AirPassengers) <- 1949:1960
1.1 Make a barplot for "casualty" counts for each month by year (similar to the one in the previous slides)
1.2 Use a single color for each bar (a total of 144 colors!)
For showing data distribution and normality
hist( rnorm(1000,5), col = topo.colors(1, alpha = 0.3))
For showing proportions
pie(males, col = cols)
Add legend
pie(males, main="Males", col = cols, labels=c("CCL","STR","SAT","SJA","SUR"))
Create a colored dotchart for age classes by lek
dotchart(t(hummers), color=c("red","blue","darkgreen"), main="Dotchart for hummingbirds", cex=0.8)
plot(1, 1, xlim = c(1,5.5), ylim = c(0,7), type = "n", ann = FALSE) # Make an empty chart text(1:5, rep(6, 5), labels = c(0:4), cex = 1:5, col= cols)# Plot digits 0-4 with increasing size/color points(1:5, rep(5, 5), cex = 1:5, col = cols, pch = 0:4)# Plot symbols 0-4 with increasing size/color text((1:5) + 0.4, rep(5, 5), cex=0.6, (0:4)) points(1:5, rep(4,5), cex = 2, pch = (5:9))# Plot symbols 5-9 with labels text((1:5) + 0.4, rep(4,5), cex = 0.6, (5:9)) points(1:5, rep(3,5), cex=2, pch=(10:14))# Plot symbols 10-14 with labels text((1:5)+0.4, rep(3,5), cex=0.6, (10:14)) points(1:5, rep(2,5), cex = 2, pch = 15:19)# Plot symbols 15-19 with labels text(1:5 + 0.4, rep(2,5), cex=0.6, 15:19) points(1:6 * 0.8 + 0.2, rep(1, 6), cex = 2, pch = 20:25) # Plot symbols 20-25 with labels text(1:6 * 0.8 + 0.5, rep(1, 6), cex = 0.6, 20:25)
On hummingbird data
class<-c(rep("male",5),rep("female",5),rep("juvenile",5)) humm.df<-data.frame(class,total=c(males, females, c(4,4,6,6,16)), Leks= rep(c("CCL","STR","SAT","SJA","SUR"),3)) boxplot(total~class,data = humm.df)
On hummingbird data
Changing color and orientation
humm.df2 <- data.frame(class, total = c(rnorm(5, 1, 1), rnorm(5, 3, 2), rnorm(5, 3, 1)),Leks = rep(c("CCL", "STR", "SAT", "SJA", "SUR"), 3)) boxplot(total ~ class, data = humm.df2, col = "yellow4", horizontal = TRUE)
Changing color and orientation
# height of president divided by height of most successful opponent: body.size <- c(3.745756, 1.725416, 3.905743, 6.216381, 4.516818, 5.049753, 4.922817, 4.961822, 6.387712, 6.360082, 3.673467, 6.403956, 3.360937, 4.721525, 5.963445, 5.648205, 4.250537, 4.581020, 6.081036, 5.994140, 3.883612, 4.457508, 1.892721, 3.409522, 5.053790, 3.896708, 6.422167, 4.993130, 2.226273, 6.255924, 4.098446, 2.568278, 3.914492, 6.052945, 3.985166, 2.329352, 2.622594, 2.034283, 5.583006, 3.434656, 4.476129, 4.466925, 4.490384, 3.497488, 4.974221, 5.177449, 4.735471, 5.836074, 5.886631, 6.510843)
#Bill length in long-billed hermits frequency <- c(6.455821, 5.139507, 6.108490, 8.623173, 6.710728, 8.029906, 7.469104, 6.906462, 8.603421, 7.284242, 5.979938, 7.143694, 6.096457, 5.999354, 7.850083, 7.785403, 5.907416, 7.901530, 7.937979, 7.114081, 6.179503, 7.143809, 6.380096, 6.769819, 7.385331, 6.539280, 8.525748, 7.068429, 6.181722, 7.508457, 7.860184, 5.886500, 5.968909, 8.239845, 6.530752, 5.949638, 4.630713, 5.908844, 7.291094, 6.046808, 6.816406, 6.908434, 6.724284, 4.804671, 8.942853, 6.950045, 7.461648, 7.487176, 7.943633, 7.275438)
plot(body.size, frequency, col = "black", pch = 21, bg = "grey", cex = 2, ylab = "", xlab = "", axes = F) axis(1) axis(2) reg1 <- lm(frequency~body.size) abline(reg1, lwd = 2) par(las = 0) mtext("Body size (g)", side = 1, line = 2.5, cex = 1.5) mtext("Frequency (kHz)", side = 2, line = 2.2, cex = 1.5) text(3, 8, "r = .58", cex = 1.5)
2.1 Using the 'iris' data set example make a scatter plot showing the association between "sepal length" and "petal length"
2.2 Use different symbols for different species
2.3 Use a color gradient to show the size of "petal width" in the previous scatter plot
2.4 Add the "sepal width" as the size of the symbols
def.par <- par(no.readonly = TRUE) xhist <- hist(body.size, plot = FALSE) yhist <- hist(frequency, plot = FALSE) top <- max(c(xhist$counts, yhist$counts)) nf <- layout(matrix(c(2,0,1,3),2,2,byrow = TRUE), c(3,1), c(1,3), TRUE) layout.show(nf)
layout(matrix(c(2, 0, 1, 3),2, 2, byrow = TRUE), c(3,1), c(1,3), TRUE) par(mar = c(3,3,1,1)) plot(body.size, frequency, pch = 20, cex = 2) par(mar = c(0,3,1,1)) barplot(xhist$counts, axes = FALSE, ylim = c(0, top), space = 0) par(mar = c(3,0,1,1)) barplot(yhist$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE) par(def.par)
Example with regression plot
#set working directory where you want to save plot setwd("~/Desktop") jpeg("reg plot.jpeg") plot(body.size, frequency, col = "black", pch = 21, bg = "grey", cex = 2, ylab = "", xlab = "", axes = F) axis(1) axis(2) reg1 <- lm(frequency~body.size) abline(reg1, lwd = 2) par(las = 0) mtext("Body size (g)", side = 1, line = 2.5, cex = 1.5) mtext("Frequency (kHz)", side = 2, line = 3.7, cex = 1.5) text(3, 8, "r = .58", cex = 1.5) dev.off()
Plots can be saved in other formats with these functions:
tiff()
png()
pdf()
Using the 'iris' data set example:
3.1 Make a multipanel plot with 4 plots (2 rows and 2 columns) showing the association between "sepal length" and "petal length" for each species (first 3 panels) and for the whole data set (4th panel) (hint: you could use par(mfrow = c(2, 2))
instead of layout)
3.2 Add the species names to the titles for each plot
3.3 Use symbol size and color gradient to include information about "petal width" and "sepal width" (respectively) as in exercise 2.3 and 2.4
3.4 Save the plot as a tiff file. Make sure the font size is OK (not too big, not too small) in the saved image.