data(airquality) names(airquality) airquality$date<-with(airquality, ISOdate(1973,Month,Day)) plot(Ozone~date, data=airquality) plot(Ozone~date, data=airquality, type="l") # lines plot(Ozone~date, data=airquality, type="h") # heights plot(Ozone~date, data=airquality, type="n") # not as useless as you'd think! # add points to an existing plot plot(Ozone~Solar.R, data=airquality,type="n") points(airquality$Solar.R,airquality$Ozone,col="blue",pch=7) # adding a horizontal line bad <- ifelse(airquality$Ozone>=90, "orange","forestgreen") plot(Ozone~date,data=airquality,type="h",col=bad) abline(h=90,lty=2,col="red") # adding text to an existing plot plot(Ozone~Solar.R, data=airquality, col=bad) abline(h=90, lty=2, col="red") text(85,100,"High Ozone Level",cex=.8,col="blue") # multi-colors and symbols, and a legend lowwinds <- ifelse(airquality$Wind<=8, "red", "blue") symbols <- ifelse(airquality$Wind<=8, 5,1) plot(Ozone~Solar.R,data=airquality,col=lowwinds,pch=symbols) legend("topleft",c("low wind","high wind"),col=c("red","blue"), pch=c(5,1)) # adding smoothers data(cars) plot(dist~speed,data=cars) with(cars, lines(lowess(speed, dist), col="tomato", lwd=2)) plot(dist~speed,data=cars, log="xy") with(cars, lines(lowess(speed, dist), col="tomato", lwd=2)) with(cars, lines(supsmu(speed, dist), col="purple", lwd=2)) legend("bottomright", legend=c("lowess","supersmoother"),bty="n", lwd=2, col=c("tomato","purple")) # multiple plots data(ToothGrowth) # load data into current R session par(mfrow=c(2,2)) # Set up a 2x2 layout #1st Plot - scatterplot of length vs dose; plot(len~dose, data=ToothGrowth, xlab="Vitamin C dose (mg)", ylab="Tooth Length", col="blue" ,cex.main=.8) #2nd plot - boxplot of length vs dose; boxplot(len~dose, data=ToothGrowth, horizontal=TRUE, ylab="Vitamin C dose (mg)", xlab="Tooth Length", cex.main=.8) #3rd plot - boxplot of length vs type of supplement; boxplot(len~supp, data=ToothGrowth, horizontal=TRUE, ylab="Supplement Type", xlab="Tooth Length", cex.main=.8) #4th plot - length vs *interaction* (i.e. all combinations) of supp and dose; boxplot(len~supp*dose,data=ToothGrowth,horizontal=TRUE,col=c("orange","yellow"), ylab="Supplement and Dose", xlab="Tooth Length") #... and give this one a legend legend("topleft", c("Ascorbic acid", "Orange juice"), fill=c("yellow","orange")) #set up a 2x2 layout, but merge first 2 cells, i.e. the top row matrix(c(1,1,2,3), 2, 2, byrow = TRUE) layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE)) #1st plot - the interactions again, with a legend added boxplot(len~supp*dose, data=ToothGrowth, col=c("orange","yellow"), xlab="Supplement and Dose",ylab="Tooth Length") legend("bottomright",c("Ascorbic acid", "Orange juice"), fill = c("yellow", "orange")) #2nd plot (in bottom left position) - scatterplot length vs dose plot(len~dose, data=ToothGrowth, xlab="Vitamin C dose (mg)", ylab="Tooth Length", col="blue", cex.main=.8) #3rd plot (in bottom right position) - histogram of tooth length hist(ToothGrowth$len, xlab="Tooth Length", main="", cex.main=.8)