# NOT RUN {
# ---------------------------------------------------------
# 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.x=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, stroke="off", 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
style("gray")
BarChart(Gender, by=Pain)
style("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.fill="azure", hi.fill=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: 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.fill="wheat1", values.stroke="wheat4", trans=.7,
# 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 data
# ----------------------------
# include a y-variable, here Salary, in the data table to read directly
mydata <- read.csv(text="
Dept, Salary
ACCT,51792.78
ADMN,71277.12
FINC,59010.68
MKTG,60257.13
SALE,68830.06", header=TRUE)
BarChart(Dept, Salary)
# specify two variables for a two variable bar chart
# also specify a y-variable to provide the counts directly
mydata <- read.csv(text="
Dept,Gender,Count
ACCT,F,3
ACCT,M,2
ADMIN,F,4
ADMIN,M,2
FINC,F,1
FINC,M,3
MKTG,F,5
MKTG,M,1
SALE,F,5
SALE,M,10", header=TRUE)
BarChart(Dept, Count, by=Gender)
# }
Run the code above in your browser using DataLab