# --------------------------------------------
# barchart from the data for a single variable
# --------------------------------------------
# generate some random data values for two variables
# Pain is an ordered factor, Gender is an unordered factor
Pain <- sample(c("None", "Some", "Much", "Massive"), size=25, replace=TRUE)
Pain <- factor(Pain, levels=c("None", "Some", "Much", "Massive"), ordered=TRUE)
Gender <- sample(c("Male", "Female"), size=25, replace=TRUE)
Gender <- factor(Gender)
# for each level of Pain, display the frequencies
# Pain is an ordered factor, so the bar colors are ordered
color.barchart(Pain)
# compare to standard R bar plot
barplot(table(Pain))
# Gender is unordered, so one color used for all the bars
color.barchart(Gender)
# specify a unique bar color for each of the two bars
# also display narrower bars, the default gap between bars is .2
color.barchart(Gender, col.bars=c("rosybrown","tan"), gap=.75)
# ----------------------------------------
# barchart from the data for two variables
# ----------------------------------------
# at each level of Pain, show the frequencies of the Gender levels
color.barchart(Pain, Gender, chisq=TRUE)
# compare to standard R bar plot for two variables
barplot(table(Gender,Pain), beside=TRUE)
# at each level of Pain, show the frequencies of the Gender levels
# display more vivid colors, randomize from the palette the bar colors
color.barchart(Pain, Gender, vivid=TRUE, random.col=TRUE)
# at each level of Gender, show the frequencies of the Pain levels
# Pain levels are ordered, so the corresponding colors are also ordered
color.barchart(Gender, Pain)
# specify an ordered blue palette of colors for the ordered levels of Pain
# only works when the variable is an ordered factor
# colors can be named or customized with rgb function
color.barchart(Gender, Pain, col.low="azure", col.hi=rgb(100,110,200,max=255))
# define custom color gradient within each group of bars
# applies to both ordered and unordered factors
color.barchart(Gender, Pain, col.bars=c("thistle1","thistle2","thistle3","thistle4"))
# display only two stacked bars, Female and Male
# the levels of Pain are included within each respective bar
color.barchart(Gender, Pain, beside=FALSE, legend.horiz=TRUE, addtop=5)
# horizontal bar chart of two variables
color.barchart(Gender, Pain, horiz=TRUE, legend.loc="bottomright")
# many options, including those from par: col.main, col.axis, col.lab, cex.lab
# for more info on these graphic options, enter: help(par)
color.barchart(Pain, Gender, col.bars=c("coral3","seagreen3"),
legend.loc="topleft", legend.labels=c("The Girls", "The Boys"),
xlab="Pain Level", main="Gender for Different Pain Levels",
col.bg="wheat1", col.grid="wheat3", col.main="wheat4",
col.axis="wheat4", col.lab="wheat4", cex.lab=1.2)
# ----------------------------
# can enter many types of data
# ----------------------------
# generate and enter integer data
X1 <- sample(1:4, size=100, replace=TRUE)
X2 <- sample(1:4, size=100, replace=TRUE)
color.barchart(X1)
color.barchart(X1,X2)
# generate and enter type double data
X1 <- sample(c(1,2,3,4), size=100, replace=TRUE)
X2 <- sample(c(1,2,3,4), size=100, replace=TRUE)
color.barchart(X1)
color.barchart(X1,X2)
# generate and enter character string data
# that is, without first converting to a factor
Travel <- sample(c("Bike", "Bus", "Drive", "Walk"), size=25, replace=TRUE)
color.barchart(Travel)
# can enter the table counts directly, as in standard R barplot function
# provide a label for the different levels
color.barchart(table(Pain), xlab="Pain")
# ---------------------------------------------
# multiple bar charts across multiple variables
# ---------------------------------------------
# read data into data frame called mydata
#rad("http://web.pdx.edu/~gerbing/data/employees2.csv")
# bar charts for all non-numeric variables in the data frame called mydata
#color.barchart()
# Use the subset function to specify a variable list
#color.barchart(subset(mydata, select=c(Gender,Dept)))
# ------------------------------
# bar chart directly from counts
# ------------------------------
# barchart of one variable with three levels
# enter counts as a vector with the combine function, c
# must supply the level names and variable name
City <- c(206, 94, 382)
names(City) <- c("LA","Chicago","NY")
color.barchart(City, main="Employees in Each City")
# barchart of two variables
# two Quality levels, the rows
# three Supplier levels, the columns
# enter counts row by row, then form the table with rbind function
# must supply the level (value) names and the variable names
# chart presented as Row Variable, analyzed at each level of Column Variable
row1 <- c(19, 16, 23)
row2 <- c(6, 6, 8)
mytable <- rbind(row1, row2)
rownames(mytable) <- c("Pass", "Defective")
colnames(mytable) <- c("Acme, Inc", "Nuts, Inc", "Bolts, Inc")
color.barchart(mytable, xlab="Supplier", legend.title="Quality")
Run the code above in your browser using DataLab