# ---------------------------------------------------------
# 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=50, replace=TRUE)
Pain <- factor(Pain, levels=c("None", "Some", "Much", "Massive"), ordered=TRUE)
Gender <- sample(c("Male", "Female"), size=50, replace=TRUE)
Gender <- factor(Gender)
mydata <- data.frame(Pain, Gender)
rm(Pain); rm(Gender)
# --------------------------------------------
# pie chart from the data for a single variable
# --------------------------------------------
# for each level of Pain, display the frequencies
# Pain is an ordered factor, so the slice colors are ordered
PieChart(Pain)
# short name
pc(Pain)
# compare to standard R pie chart, which requires mydata$ reference
pie(table(mydata$Pain))
# Gender is unordered, so a different color for each slice
PieChart(Gender)
# specify a unique slice color for each of the two slices
PieChart(Gender, fill=c("pink","lightblue"))
# specify the colors from the R palette rainbow.colors
PieChart(Gender, colors="rainbow")
# ------------------------------
# pie chart directly from counts
# ------------------------------
# pie chart 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")
pc(City, main="Employees in Each City")
Run the code above in your browser using DataLab