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.color="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 theme="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 fill
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 fill=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 low.fill
and hi.fill
options, or individually specify the color of each piece with the fill
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 lessR
functions generate their own graphics calls, 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 width
and 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