# generate 100 random normal data values with three decimal digits
y <- round(rnorm(100),3)
# --------------------
# different histograms
# --------------------
# histogram with all defaults
hst(y)
# compare to standard R function hist
hist(y)
# histogram with specified bin width
# can also use bin.start
hst(y, bin.width=.25)
# histogram with specified bins and grid lines displayed over the histogram
hst(y, breaks=seq(-5,5,.25), xlab="My Variable", over.grid=TRUE)
# histogram with bins calculated with the Scott method and values displayed
hst(y, breaks="Scott", labels=TRUE)
# histogram with the number of suggested bins, with proportions
hst(y, breaks=25, prop=TRUE)
# histogram with specified colors, overriding defaults
# col.bg and col.grid are defined in histogram
# all other parameters are defined in hist, par and plot functions
hst(y, col.bars="darkblue", col.border="lightsteelblue4", col.bg="ivory",
col.grid="darkgray", density=25, angle=-45, cex.lab=.8, cex.axis=.8,
col.lab="sienna3", main="My Title", col.main="gray40", xlim=c(-5,5), lwd=2,
xlab="My Favorite Variable")
# ---------------------
# cumulative histograms
# ---------------------
# cumulative histogram with superimposed regular histogram, all defaults
hst(y, cumul="both")
# cumulative histogram plus regular histogram
# present with proportions on vertical axis, override other defaults
hst(y, cumul="both", breaks=seq(-4,4,.25), prop=TRUE,
col.reg="mistyrose")
# -------------------------------------------------
# histograms for data frames and multiple variables
# -------------------------------------------------
# create data frame, mydata, to mimic reading data with rad function
# mydata contains both numeric and non-numeric data
mydata <- data.frame(rnorm(100), rnorm(100), rnorm(100), rep(c("A","B"),50))
names(mydata) <- c("X","Y","Z","C")
rm(X); rm(Y); rm(Z); rm(C);
# although data not attached, access the variable directly by its name
hst(X)
# histograms for all numeric variables in data frame called mydata
# except for numeric variables with unique values < ncut
# mydata is the default name, so does not need to be specified with dframe
hst()
# variable of interest is in a data frame which is not the default mydata
# access the breaks variable in the R provided warpbreaks data set
# although data not attached, access the variable directly by its name
data(warpbreaks)
hst(breaks, dframe=warpbreaks)
hst()
# histograms for all numeric variables in data frame called mydata
# with specified options
hst(col.bars="palegreen1", col.bg="ivory", labels=TRUE)
# Use the subset function to specify a variable list
# histograms for all specified numeric variables
mysub <- subset(mydata, select=c(X,Y))
hst(dframe=mysub)
Run the code above in your browser using DataLab