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 pc, 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.
VARIABLE LABELS
A labels data frame named mylabels
, obtained from the Read
function, can provide the label for some or all of the variables in the data frame that contains the data for the analysis. If this labels data frame exists, then the corresponding variable label is listed as the label for the horizontal axis unless xlab is specified in the function call. If there are two variables to plot, the title of the resulting plot is based on the two variable labels, unless a specific title is listed with the main
option. The variable label is also listed in the text output, next to the variable name. If the analysis is for two variables, then labels for both variables are included.