# ---------------------------------------------------------
# generate some data in data frame mydata for two variables
# ---------------------------------------------------------
# Pain is an ordered factor, Gender is an unordered factor
# Place in data frame mydata to simulate reading with rad
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)
mydata <- data.frame(Pain, Gender)
rm(Pain); rm(Gender)
# --------------------------------------------
# barchart from the data for a single variable
# --------------------------------------------
# for each level of Pain, display the frequencies
# Pain is an ordered factor, so the bar colors are ordered
BarChart(Pain)
# short name
bc(Pain)
# save and then display the frequencies for later analysis
myCount <- BarChart(Pain)
myCount
# rotate and offset the axis labels
BarChart(Pain, rotate.values=45, offset=1)
# compare to standard R bar plot, which requires mydata$ reference
barplot(table(mydata$Pain))
# Gender is unordered, so one color used for all the bars
BarChart(Gender)
# column proportions instead of frequencies
BarChart(Gender, proportion=TRUE)
# specify a unique bar color for each of the two bars
BarChart(Gender, fill=c("palegreen3","tan"))
# automatically provide horizontal value labels
# and adjust left margin as needed
BarChart(Pain, horiz=TRUE)
# manage size of horizontal value labels
myd <- Read("Cars93", format="lessR")
bc(Make, horiz=TRUE, label.max=4, las=1, data=myd)
# ----------------------------------------
# barchart from the data for two variables
# ----------------------------------------
# at each level of Pain, show the frequencies of the Gender levels
BarChart(Pain, by=Gender)
# at each level of Pain, show the row proportions of the Gender levels
# i.e., proportional stacked bar graph
BarChart(Pain, by=Gender, proportion=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 theme set to gray
theme(colors="gray")
BarChart(Gender, by=Pain)
theme(colors="sienna")
# 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
BarChart(Gender, by=Pain, low.color="azure", hi.color=rgb(100,110,200,max=255))
# display bars beside each other instead of stacked, Female and Male
# the levels of Pain are included within each respective bar
BarChart(Gender, by=Pain, beside=TRUE, legend.horiz=TRUE)
# horizontal bar chart of two variables, put legend on the top
BarChart(Gender, by=Pain, horiz=TRUE, legend.loc="top")
# many options, including those from par: axes, col.main, col.lab, cex.lab
# for more info on these graphic options, enter: help(par)
BarChart(Pain, by=Gender, fill=c("coral3","seagreen3"),
legend.loc="topleft", legend.labels=c("Girls", "Boys"),
xlab="Pain Level", main="Gender for Different Pain Levels",
bg="wheat1", grid="wheat3", axes="wheat4",
col.main="wheat4", col.lab="wheat4", cex.lab=1.2)
# ---------------------------------------------
# multiple bar charts across multiple variables
# ---------------------------------------------
# bar charts for all non-numeric variables in the data frame called mydata
# and all numeric variables with a small number of values, < n.cat
BarChart()
# ----------------------------
# 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)
BarChart(X1)
BarChart(X1, by=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)
BarChart(X1)
BarChart(X1, by=X2)
# generate and enter character string data
# that is, without first converting to a factor
Travel <- sample(c("Bike", "Bus", "Car", "Motorcycle"), size=25, replace=TRUE)
BarChart(Travel, horiz=TRUE)
# ------------------------------
# bar chart directly from counts
# ------------------------------
# barchart of one variable with five levels
# enter counts as a vector with the combine function, c
# must supply the level names and variable name
Dept <- c(5,6,4,6,15)
names(Dept) <- c("ACCT", "ADMIN", "FINC", "MKTG", "SALE")
BarChart(Dept)
# counts are in the data table to be read directly
mydata <- read.csv(text="
Dept, Count
ACCT,5
ADMN,6
FINC,4
MKTG,6
SALE,15", header=TRUE)
# use count.labels to indicate the label for each corresponding count
BarChart(Count, count.labels=Dept)
# 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")
BarChart(mytable, xlab="Supplier", legend.title="Quality")
Run the code above in your browser using DataLab