# ----------------------
# barchart from the data
# ----------------------
# 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)
# 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 space is .2
color.barchart(Gender, col.bars=c("rosybrown","tan"), space=.6)
# for each level of Pain, display the frequencies at each level of Gender
color.barchart(Pain, Gender)
# for each level of Pain, display the frequencies at each level of Gender
# display more vivid colors, randomize from the palette the bar colors chosen
color.barchart(Pain, Gender, vivid=TRUE, random.col=TRUE)
# for each level of Gender, display the frequencies at each level of Pain
# 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
color.barchart(Gender, Pain, col.low="lightblue", col.hi=rgb(100,110,200,max=255))
# define custom colors bar by bar within each group of bars
# works for ordered or 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(4, size=100, replace=TRUE)
X2 <- sample(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")
# -----------------------------
# barchart 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, three Supplier levels
# enter counts as a matrix, defined by binding the rows of counts with rbind
# must supply the level names and variable names
Pass <- c(19, 16, 23)
Defective <- c(6, 6, 8)
mymat <- rbind(Pass, Defective)
rownames(mymat) <- c("Pass","Defective")
colnames(mymat) <- c("Acme, Inc","Nuts, Inc","Bolts, Inc")
color.barchart(mymat, xlab="Supplier", legend.title="Quality")
Run the code above in your browser using DataLab