# box- vs histogram-plot
par(mfrow=c(2,1))
mu<-2
si<-0.6
bimodal<-c(rnorm(1000,-mu,si),rnorm(1000,mu,si))
uniform<-runif(2000,-4,4)
normal<-rnorm(2000,0,3)
histoplot(bimodal,uniform,normal)
boxplot(bimodal,uniform,normal)
# add to an existing plot
x <- rnorm(100)
y <- rnorm(100)
plot(x, y, xlim=c(-5,5), ylim=c(-5,5))
histoplot(x, col="tomato", horizontal=TRUE, at=-4, add=TRUE,lty=2, rectCol="gray")
histoplot(y, col="cyan", horizontal=FALSE, at=-4, add=TRUE,lty=2)
# formula input
data("iris")
histoplot(Sepal.Length~Species, data = iris, main = "Sepal Length",
col=c("lightgreen", "lightblue", "palevioletred"))
legend("topleft", legend=c("setosa", "versicolor", "virginica"),
fill=c("lightgreen", "lightblue", "palevioletred"), cex = 0.5)
data("diamonds", package = "ggplot2")
palette <- RColorBrewer::brewer.pal(9, "Pastel1")
par(mfrow=c(3, 1))
histoplot(price ~ cut, data = diamonds, las = 1, col = palette)
histoplot(price ~ clarity, data = diamonds, las = 2, col = palette)
histoplot(price ~ color, data = diamonds, las = 2, col = palette)
par(mfrow=c(3, 1))
#generate example data
data_one <- rnorm(100)
data_two <- rnorm(50, 1, 2)
#generate histogram plot with similar functionality to histoplot
histoplot(data_one, data_two, col="magenta")
#note vioplox defaults to a greyscale plot
histoplot(data_one, data_two)
#colours can be customised separately, with axis labels, legends, and titles
histoplot(data_one, data_two, col=c("red","blue"), names=c("data one", "data two"),
main="data histogram", xlab="data class", ylab="data read")
legend("topleft", fill=c("red","blue"), legend=c("data one", "data two"))
#colours can be customised for the histogram fill and border separately
histoplot(data_one, data_two, col="grey85", border="purple", names=c("data one", "data two"),
main="data histogram", xlab="data class", ylab="data read")
#colours can also be customised for the boxplot rectange and lines (border and whiskers)
histoplot(data_one, data_two, col="grey85", rectCol="lightblue", lineCol="blue",
border="purple", names=c("data one", "data two"),
main="data histogram", xlab="data class", ylab="data read")
#these colours can also be customised separately for each histogram
histoplot(data_one, data_two, col=c("skyblue", "plum"), rectCol=c("lightblue", "palevioletred"),
lineCol="blue", border=c("royalblue", "purple"), names=c("data one", "data two"),
main="data histogram", xlab="data class", ylab="data read")
#this applies to any number of histograms, given that colours are provided for each
histoplot(data_one, data_two, rnorm(200, 3, 0.5), rpois(200, 2.5), rbinom(100, 10, 0.4),
col=c("red", "orange", "green", "blue", "violet"),
rectCol=c("palevioletred", "peachpuff", "lightgreen", "lightblue", "plum"),
lineCol=c("red4", "orangered", "forestgreen", "royalblue", "mediumorchid"),
border=c("red4", "orangered", "forestgreen", "royalblue", "mediumorchid"),
names=c("data one", "data two", "data three", "data four", "data five"),
main="data histogram", xlab="data class", ylab="data read")
#The areaEqual parameter scales with width of histograms
#histograms will have equal density area (including missing tails) rather than equal maximum width
histoplot(data_one, data_two, areaEqual=TRUE)
histoplot(data_one, data_two, areaEqual=TRUE,
col=c("skyblue", "plum"), rectCol=c("lightblue", "palevioletred"),
lineCol="blue", border=c("royalblue", "purple"), names=c("data one", "data two"),
main="data histogram", xlab="data class", ylab="data read")
histoplot(data_one, data_two, rnorm(200, 3, 0.5), rpois(200, 2.5), rbinom(100, 10, 0.4),
areaEqual=TRUE, col=c("red", "orange", "green", "blue", "violet"),
rectCol=c("palevioletred", "peachpuff", "lightgreen", "lightblue", "plum"),
lineCol=c("red4", "orangered", "forestgreen", "royalblue", "mediumorchid"),
border=c("red4", "orangered", "forestgreen", "royalblue", "mediumorchid"),
names=c("data one", "data two", "data three", "data four", "data five"),
main="data histogram", xlab="data class", ylab="data read")
#To compare multiple groups of histogram densities, it helps to adjust the wex.
dlist1 <- lapply(c(10,20,30,40), function(n) runif(n))
dlist2 <- lapply(c(100,200,300,400), function(n) runif(n))
hscale1 <- sapply(dlist1, function(r){
max(hist(r, plot=FALSE, breaks=seq(0,1,by=.05))$density)})
histoplot(dlist1, side='left', col=grey(.3),
breaks=seq(0,1,by=.05), add=FALSE, pchMed=NA, drawRect=FALSE, border=NA,
wex=hscale1/length(hscale1))
hscale2 <- sapply(dlist2, function(r){
max(hist(r, plot=FALSE, breaks=seq(0,1,by=.05))$density)})
histoplot(dlist2, side='right', col=grey(.7),
breaks=seq(0,1,by=.05), add=TRUE, pchMed=NA, drawRect=FALSE, border=NA,
wex=hscale2/length(hscale2))
#Sometimes, it is helpful to see the raw counts instead.
dvec <- length(unlist(c(dlist1, dlist2)))/4
histoplot(dlist1, side='left', col=grey(.3),
breaks=seq(0,1,by=.05), add=FALSE, pchMed=NA, drawRect=FALSE, border=NA,
wex=sapply(dlist1, length)/dvec*hscale1/length(hscale1))
histoplot(dlist2, side='right', col=grey(.7),
breaks=seq(0,1,by=.05), add=TRUE, pchMed=NA, drawRect=FALSE, border=NA,
wex=sapply(dlist2, length)/dvec*hscale2/length(hscale2))
#It may also benefit some users to pass density and angle arguments to the
# histograms (ultimately rect) and create outer legends
hist(runif(100), density=c(10,20), angle=c(22,90+22) ,col=1)
outer_legend <- function(...) {
opar <- par(fig=c(0, 1, 0, 1), oma=c(0, 0, 0, 0), mar=c(0, 0, 0, 0), new=TRUE)
on.exit(par(opar))
plot(0, 0, type='n', bty='n', xaxt='n', yaxt='n')
legend(...)
}
outer_legend('topright', pch=15, density=c(10,20), angle=c(22,90+22), col=0, legend=c('Y','N'))
Run the code above in your browser using DataLab