# ---------------------------------------------------------
# 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)
# 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)
# specify a unique bar color for each of the two bars
BarChart(Gender, col.bars=c("pink","lightblue"))
# automatically provide horizontal value labels
# and adjust left margin as needed
BarChart(Pain, horiz=TRUE)
# ----------------------------------------
# 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 frequencies of the Gender levels
# display more gray colors
BarChart(Pain, by=Gender, colors="gray")
# or set for all subsequent analyses for all lessR graphic functions
set(colors="gray")
# at each level of Gender, show the frequencies of the Pain levels
# Pain levels are ordered, so the corresponding colors are also ordered
BarChart(Gender, by=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
BarChart(Gender, by=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
BarChart(Gender, by=Pain, col.bars=c("thistle1","thistle2","thistle3","thistle4"))
# 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.axis, col.lab, cex.lab
# for more info on these graphic options, enter: help(par)
BarChart(Pain, by=Gender, col.bars=c("coral3","seagreen3"),
legend.loc="topleft", legend.labels=c("Girls", "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)
# ---------------------------------------------
# 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()
# bar chart of all relevant variables with a color randomly chosen
# from the default color palette of eight colors
# no frequency summary, just the graphs
BarChart(random.col=TRUE, text.out=FALSE)
# Use the subset function to specify a variable list
mysub <- subset(mydata, select=c(Pain))
BarChart(dframe=mysub)
# ----------------------------
# 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,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 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")
BarChart(City, main="Employees in Each City", addtop=10)
# 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")
# counts are in the data file to be read directly
mydata <- read.csv(text="Dept, Count
ACCT,5
ADMN,6
FINC,4
MKTG,6
SALE,15", header=TRUE)
# use count.names to indicate the label for each corresponding count
bc(Count, count.names=Dept)
Run the code above in your browser using DataLab