Let's say we just want to compare two variables, x & y. We can use the following code [I'll use blue font for R code]:
x=c(2, 3, 5, 6, 7, 4, 5, 7, 8)
y=c(4, 5, 11, 13, 16, 16, 17, 18, 24, 19)
You could also use the following to create random normal variables:
x=rnorm(10, mean=4, sd=2)
y=rnorm(10, mean=12, sd=2)
From here you can make a very simple boxplot using:
boxplot(x,y)
Let's add a few options: color (col="lightblue"), width of boxes (boxwex=.4), and cleaning up line type (lty=1). Putting this together:
boxplot(x,y, col="lightblue", boxwex=.4, lty=1)
This is looking a little better.
Let's add some x & y labels and main title:
boxplot(x,y, col="lightblue", boxwex=.4, lty=1, ylab="Whatever y is", xlab="Whatever x is", main="Boxplot sample for stats class")
Now something to label the x values and also a line at some y value of interest - say you know that y values over 10 are unhealthy:
boxplot(x,y, col="lightblue", boxwex=.4, lty=1, ylab="Whatever y is", xlab="Whatever x is", main="Boxplot sample for stats class") axis(1,at=c(1, 2),labels=c("x", "y")) abline(h=10, lty=2, lwd=2, col="blue")
Finally, let's export this thing as a pdf (you can also export as jpeg, etc, but pdf looks best).
Final code looks like:
pdf("whatev.pdf") par(family="serif", bg="beige") boxplot(x,y, col="lightblue", boxwex=.4, lty=1, ylab="Whatever y is", xlab="Whatever x is", main="Boxplot sample for stats class") axis(1,at=c(1, 2),labels=c("x", "y")) abline(h=10, lty=2, lwd=2, col="blue") dev.off()
Final plot looks like (click on image for better view):
Note: I actually had to use Jpeg because of blogger constraints.

Play around with the options and have fun!
No comments:
Post a Comment