08/02/2015
The following data objects are usually the inputs in R plots:
# vector a<-c(1,2,3,4,5) a
## [1] 1 2 3 4 5
is.vector(a)
## [1] TRUE
let<-letters[1:5] let
## [1] "a" "b" "c" "d" "e"
either numbers or characters
#matrix b<-cbind(a,a,a,a) b
## a a a a ## [1,] 1 1 1 1 ## [2,] 2 2 2 2 ## [3,] 3 3 3 3 ## [4,] 4 4 4 4 ## [5,] 5 5 5 5
is.matrix(b)
## [1] TRUE
numbers and characters
#matrix c<-data.frame(a,a,a,a,let) c
## a a.1 a.2 a.3 let ## 1 1 1 1 1 a ## 2 2 2 2 2 b ## 3 3 3 3 3 c ## 4 4 4 4 4 d ## 5 5 5 5 5 e
is.data.frame(c)
## [1] TRUE
Graph the number of males per lek
males <- c(5, 4, 6, 4, 9) # males per lek plot(males)
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) # title red, bold/italic font
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() # Graph females with red dashed line and square points lines(females, type="o", pch=22, lty=2, col="red") # Create a title with a red, bold/italic font 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=rainbow(5)) # 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=rainbow(5))
Now let's graph the total number of birds per day using some color and show a legend:
For showing data distribution and normality
# Get a random log-normal distribution r <- rnorm(1000,5) hist(r,col="skyblue")
For showing proportions
pie(males)
Add legend
pie(males, main="Males", col=rainbow(length(males)), 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)
Different symbols
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=1:5)# Plot digits 0-4 with increasing size/color points(1:5, rep(5,5), cex=1:5, col=1:5, 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 random 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 = T)
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)
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
# dev.off() 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()