data("airquality") Ozone = na.omit(airquality$Ozone) ###### Stripchart ###### stripchart(Ozone) stripchart(Ozone,method="overplot",pch=1,col=1,cex=1.5) # vertical=FALSE stripchart(Ozone,method="overplot",pch=1,col=1,cex=1.5,log="x") stripchart(Ozone,method="jitter",jitter=0.1, pch=1,col=1,cex=1.5) stripchart(Ozone,method="jitter",jitter=0.1, pch=1,col=1,cex=1.5,log="x") stripchart(Ozone,method="stack",pch=1,col=1,cex=1.5) stripchart(Ozone,method="stack",pch=1,col=1,cex=1.5,log="x") stripchart(Petal.Length~Species, data=iris,main="Iris", xlab="Species",ylab="Petal length",col=c(2,3,4), vertical=TRUE,pch=16) stripchart(Petal.Length~Species, data=iris,main="Iris", xlab="Species",ylab="Petal length",col=c(2,3,4), vertical=FALSE,pch=16) library(beeswarm) beeswarm(Ozone, log = FALSE,pch = 1, col = 1, cex = 1.2, vertical=FALSE) beeswarm(Ozone, log = TRUE,pch = 1, col = 1, cex = 1.2, vertical=FALSE) library(ggplot2) ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_jitter() p<-ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_jitter(position=position_jitter(0.2)) p p + coord_flip() ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_dotplot(binaxis='y', stackdir='center') library(lattice) stripplot(Petal.Length ~ Species, iris) stripplot(Species ~ Petal.Length, iris) ###### Histogram ###### hist(Ozone) hist(Ozone, freq=TRUE) hist(Ozone, freq=FALSE) hist(Ozone,main="Air quality",xlab="Ozone",col="darkmagenta",freq=FALSE) h <- hist(Ozone,ylim=c(0,50)); h text(h$mids,h$counts,labels=h$counts, adj=c(0.5, -0.5)) hist(Ozone, breaks=4, main="With breaks=4") hist(Ozone, breaks=20, main="With breaks=20") nclass.Sturges(Ozone) # Sturges = default nclass.scott(Ozone) # Scott nclass.FD(Ozone) # Friedman - Diaconis hist(Ozone, breaks=nclass.FD(Ozone), main="") range(Ozone) hist(Ozone,main="Ozone",xlab="Ozone",col="chocolate", border="black",breaks=c(1,10,25, 50,100,200)) hist(Ozone, freq=TRUE, breaks = 20, plot = TRUE,main = NULL) rug(Ozone, col=2) hist(Ozone, freq=FALSE, breaks = 20, plot = TRUE,main = NULL) library(BHH2) dots(Ozone, y = 0, xlim = range(Ozone,na.rm=TRUE), stacked = FALSE, hmax= 0.5,base = TRUE, axes = FALSE, pch = 21, pch.size = "x", labels = NULL,hcex = 1, cex = par("cex"), cex.axis = par("cex.axis")) library(ggplot2) p <- ggplot(as.data.frame(Ozone), aes(x=Ozone)) + geom_histogram() p p + geom_histogram(binwidth=5) p + geom_histogram(color="black", fill="white") ggplot(as.data.frame(Ozone), aes(x=Ozone)) + geom_histogram(aes(y=..density..), colour="black", fill="white")+ geom_density(alpha=.2, fill="#FF6666") ggplot(iris, aes(x=Petal.Length, color=Species)) + geom_histogram(fill="white") library(lattice) histogram(~Petal.Length | Species, data = iris, xlab = "Iris", type = "density", nint = 10, layout = c(2, 2)) ###### Kernel density estimation (KDE) plot ###### density(Ozone) plot(density(Ozone)) rug(Ozone) plot(density(Ozone)) polygon(density(Ozone), col = "#1b98e0") hist(Ozone, prob = TRUE) lines(density(Ozone), col = "red") hist(Ozone, prob = TRUE) curve(dnorm(x, mean=mean(Ozone), sd=sd(Ozone)), add=TRUE, col="red") plot(density(Ozone), lwd = 2, col=2) plot(density(Ozone, bw = 25), lwd = 2, col = "red", main = "big bandwidth") plot(density(Ozone, bw = 4), lwd = 2, col = "red", main = "small bandwidth") bw.nrd0(Ozone) # Silverman bw.nrd(Ozone) # Scott bw.SJ(Ozone, method = c("ste", "dpi")) # Sheather & Jones bw.ucv(Ozone) # unbiased cross-validation bw.bcv(Ozone) # biased cross-validation plot(density(Ozone, n = 1000)) rug(precip) lines(density(Ozone, bw = "nrd"), col = 2) lines(density(Ozone, bw = "ucv"), col = 3) lines(density(Ozone, bw = "bcv"), col = 4) lines(density(Ozone, bw = "SJ-ste"), col = 5) lines(density(Ozone, bw = "SJ-dpi"), col = 6) legend("topright",legend = c("nrd0", "nrd", "ucv", "bcv", "SJ-ste", "SJ-dpi"), col = 1:6, lty = 1) den1 = density(Ozone) den1$bw den1$x den1$y plot(den1$x, den1$y, type="l", ylab = "density", xlab = "x") rug(Ozone, lwd=2) library(BHH2) dots(Ozone, y = 0, xlim = range(Ozone,na.rm=TRUE), stacked = FALSE, hmax= 0.5,base = TRUE, axes = FALSE, pch = 21, pch.size = "x", labels = NULL,hcex = 1, cex = par("cex"), cex.axis = par("cex.axis")) library(ggplot2) p <- ggplot(as.data.frame(Ozone), aes(x=Ozone)) + geom_density() p p + geom_density(color="darkblue", fill="lightblue") ggplot(iris, aes(x=Petal.Length, color=Species)) + geom_density() ggplot(iris, aes(x=Petal.Length, color=Species)) + geom_density(adjust=0.5) ggplot(iris, aes(x=Petal.Length, color=Species)) + geom_density(adjust=5) ggplot(iris, aes(x=Petal.Length, color=Species)) + geom_density(adjust=bw.nrd0(iris$Petal.Length)) ggplot(iris, aes(x=Petal.Length, color=Species)) + geom_density(adjust=bw.ucv(iris$Petal.Length)) library(lattice) densityplot(~ Petal.Length, data = iris) densityplot(~ Petal.Length, data = iris, kernel = "gauss", bw = 0.5, plot.points = "rug", n = 200) densityplot(~Petal.Length | Species, data = iris, plot.points = FALSE, ref = TRUE, layout = c(2, 2)) ###### Box and whisker plot (boxplot) ###### boxplot(Ozone) boxplot(Ozone,main = "Mean ozone in parts per billion", xlab = "Parts Per Billion",ylab = "Ozone", col = "orange", border = "black",horizontal = TRUE, notch = TRUE) b <- boxplot(Ozone) b boxplot.stats(Ozone) boxplot.stats(Ozone)$out library(beeswarm) boxplot(Ozone, horizontal=TRUE, notch = FALSE, xlab = "Ozone", log = "x", cex = 1.2) beeswarm(Ozone, log = TRUE,pch = 1, col = 1, cex = 1.2, vertical=FALSE, add=TRUE) boxplot(Ozone,xlab="Ozone",las=2, horizontal=TRUE, cex=1.2,log="x") stripchart(Ozone,pch=1, col=1, vert=FALSE, add=TRUE, cex=1.2,log="x") boxplot(Ozone,xlab="Ozone",las=2, horizontal=TRUE, cex=1.2,log="") rug(Ozone, col=2) boxplot(Petal.Length~Species, data=iris,main="Iris", xlab="Species",ylab="Petal length") library(ggplot2) p <- ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_boxplot() p p + coord_flip() ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_boxplot(notch=TRUE) ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_boxplot(outlier.colour="red", outlier.shape=19,outlier.size=2) p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1) p + geom_jitter(shape=16, position=position_jitter(0.2)) library(lattice) bwplot(Petal.Length ~ Species, data = iris, xlab = "Species", ylab = "Petal length") bwplot(Species ~ Petal.Length, data = iris, xlab = "Petal length", ylab = "Species") library(vioplot) vioplot(Ozone, range=1.5, horizontal=TRUE, col=0) stripchart(Ozone, pch=1, col=2, vert=FALSE, add=TRUE, cex=1.2) vioplot(Ozone, range=1.5, horizontal=TRUE, col=0) stripchart(x, method="jitter", pch=1, col=2, vert=FALSE, add=TRUE, cex=1.2) vioplot(Ozone, range=1.5, horizontal=TRUE, col=0) beeswarm(Ozone, log = FALSE,pch = 16, col = 2, cex = 1.2, vertical=FALSE, add=TRUE) library(ggplot2) p <- ggplot(iris, aes(x=Species, y=Petal.Length)) + geom_violin() p p + coord_flip() p + stat_summary(fun.y=mean, geom="point", shape=23, size=2) p + stat_summary(fun.y=median, geom="point", size=2, color="red") p + geom_boxplot(width=0.1) data_summary <- function(x){ m <- mean(x) ymin <- m-sd(x) ymax <- m+sd(x) return(c(y=m,ymin=ymin,ymax=ymax)) } p + stat_summary(fun.data=data_summary) p + geom_dotplot(binaxis='y', stackdir='center', dotsize=1) p + geom_jitter(shape=16, position=position_jitter(0.2))