Learn R Programming

lessR (version 2.6)

PieChart: Pie Chart

Description

Abbreviation: pc

Plots a pie chart with default colors from a variety of different types of data. Also displays the frequency table for the variable and provides the corresponding chi-square inferential analysis.

Usage

PieChart(x, dframe=mydata, 
         random.col=FALSE,
         col.slices=NULL, col.low=NULL, col.hi=NULL,
         colors=c("rainbow", "terrain", "heat"),
         text.out=TRUE, main=NULL, 
         pdf.file=NULL, pdf.width=5, pdf.height=5, ...)

pc(...)

Arguments

x
For each level of this variable, x, display the frequencies.
dframe
Optional data frame that contains the variable(s) of interest, default is mydata.
random.col
Randomizes the order of the colors within the chosen color palette, when the second variable is not ordered, otherwise is ignored. When TRUE, each run of the same function call generally yields different colors of the slices
col.slices
Specified color of each slice.
col.low
Only when the variable is an ordered factor, sets the color for the lowest level of the factor in the resulting ordered progression of colors.
col.hi
Only when the variable is an ordered factor, sets the color for the highest level of the factor in the resulting ordered progression of colors.
colors
Optional palettes that provide more options.
text.out
If TRUE, then display frequency table, chi-square analysis and sample size information.
main
Title of graph.
pdf.file
Name of the pdf file to which graphics are redirected.
pdf.width
Width of the pdf file in inches.
pdf.height
Height of the pdf file in inches.
...
Other parameter values for graphics as defined by pie and par including col.main, etc.

Details

OVERVIEW Plot a pie chart with default colors for one or two variables, presumably with a relatively small number of values for each variable. By default, colors are selected for the slices, background and grid lines, all of which can be customized. The basic computations of the chart are provided with the standard R functions pie and chisq.test and the lessR function chisq.test.

COLORS There are three ways to override the default colors. 1. There are two pre-defined color palettes, each with 7 colors. The default palette provides lighter, more pastel colors. The vivid palette, activated by color="vivid", provides brighter colors with a brighter background (cornsilk1). A third color palette, set by color="gray", provides an ordered gray scale. Three more built-in R color palettes are also available by setting color to one of "rainbow", "heat" and "terrain". The most vivid of all the palettes is "rainbow". 2. The order of the colors within the chosen palette can be randomized with the random.col="TRUE" option. For example, when this option is activated each of the seven colors in a palette has a 1/7 chance of appearing as the first color, the only color used in the plot of a single variable. When invoked for a colors="gray", the order from light to dark will generally be lost, which may be desirable if the categories do not represent an ordered factor. 3. The desired colors can be explicitly specified with the col.pieces option, which overrides any other color options. When plotting one variable, include one color in this color list, the color used for all of the slices As always with R, if the list includes more than once color, the c function must be used to generate the list, as in col.pieces=c("coral3","seagreen3").

The colors provided by the gray color palette are shades of gray. The default colors in the other two provided color palettes can be viewed, in the order in which they are displayed, by running the corresponding two lines of R code, first for the default colors and second for the vivid colors: clr <- c("slategray3", "bisque3", "darksalmon", "darkolivegreen3", "thistle", "azure3", "moccasin") barplot(rep(1,7), names=clr, col=clr, border=NA, space=.1) clr <- c("coral3", "seagreen3", "maroon3", "dodgerblue3", "purple3", "turquoise3", "yellow3") barplot(rep(1,7), names=clr, col=clr, border=NA, space=.1)

When plotting ordered factor then neither of the two standard color palettes are used. Instead, the resulting slice colors for each level of the ordered factor are also ordered in a progression of colors. The default progression is based on the first color of either the regular, vivid or gray color palettes, but this can be changed with the col.low and col.hi options, or individually specify the color of each piece with the col.piece option. A specified palette can, for example, be from light to dark of the same hue, or from a light color of one hue to a dark color of another hue. Each color value can be specified with a color name, or with a specification with the rgb function. See the examples below.

Use the showColors function in this package to get, for each color: name, sample color swatch, and corresponding rgb specification. For a very small number of levels, such as two, it is may be desirable to specify the low and high values to not be closer to each other than the default values.

STATISTICS In addition to the pie chart, descriptive and optional inferential statistics are also presented. First, the frequency table with proportions is displayed. Second, the corresponding chi-square test is also displayed.

PDF OUTPUT Because of the customized graphic windowing system that maintains a unique graphic window for the Help function, the standard graphic output functions such as pdf do not work with the lessR graphics functions. Instead, to obtain pdf output, use the pdf.file option, perhaps with the optional pdf.width and pdf.height options. These files are written to the default working directory, which can be explicitly specified with the R setwd function.

ONLY VARIABLES ARE REFERENCED The referenced variable in a lessR function can only be a variable name. This referenced variable must exist in either the referenced data frame, mydata by default, or in the user's workspace, more formally called the global environment. That is, expressions cannot be directly evaluated. For example:

> PieChart(rnorm(50)) # does NOT work}

Instead, do the following: > Y <- rnorm(50) # create vector Y in user workspace > PieChart(Y) # directly reference Y

[object Object],[object Object]

pie, chisq.test.

# --------------------------------------------------------- # 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)

# -------------------------------------------- # 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, col.slices=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") PieChart(City, main="Employees in Each City")pie chart color