Learn R Programming

lessR (version 1.5.2)

color.plot: Scatter-Plot, Function Plot, or Run Chart with Optional Dates in Color

Description

Plots individual points, such as for a scatterplot, or plots connected line segments for the plot of a function or a run chart, including an option for adding dates to the horizontal axis for a time series chart. One enhancement over the usual plot function is the automatic inclusion of color. The color of the line segments and/or the points, background, area under the plotted line segments, gridlines, and border can each be explicitly specified, with default colors provided. Also provided, when applicable, is a center line, for the generation of a run chart, plotting the values of a variable in order of occurrence over time. Also, the defaults change for each of the types of plots. The intent is to rely on the default values for a relatively sophisticated plot, particularly when compared to the default values of the standard R plot function.

Usage

color.plot(x, y=NULL, type=NULL, col.line="darkblue", col.area=NULL,  
           col.point="darkblue", col.fill=NULL, col.grid="grey90", 
           col.bg="snow1", col.box="black", xy.ticks=TRUE, 
           xlab=NULL, ylab=NULL, pch=NULL, cex=NULL, center.line=NULL,
           x.start=NULL, x.by=NULL, x.reverse=FALSE, ...)

Arguments

x
If both x and y are specified, then the x values are plotted on the horizontal axis. If x is not sorted, a scatter plot is produced. If x is sorted, then a function is plotted with a smooth line. If only x is specified with no y, then these x val
y
Coordinates of points in the plot on the vertical axis.
type
Character string that indicates the type of plot, either "p" for points, "l" for line, or "b" for both. If x and y are provided and x is sorted so that a function is plotted, the default is "l"
col.line
Color of any plotted line segments, with a default of "darkblue".
col.area
Color of area under the plotted line segments. To have a border at the bottom and right of a run chart but retain the property of no area color, specify a color of "transparent".
col.point
Color of the border of the plotted points.
col.fill
For plotted points, the interior color of the point. For a scatterplot the default value is transparent. For a run chart the default value is the color of the point's border, col.point.
col.grid
Color of the grid lines, with a default of "grey90".
col.bg
Color of the plot background, with a default of "snow1".
col.box
Color of border around the plot background, the box, that encloses the plot, with a default of "black".
xy.ticks
Flag that indicates if tick marks and associated values on the axes are to be displayed.
xlab
Label for x-axis. For two variables specified, x and y, if xlab not specified, then the label becomes the name of the corresponding variable. If xy.ticks is FALSE, then no label is displayed. If no y variable is specified,
ylab
Label for y-axis. If not specified, then the label becomes the name of the corresponding variable. If xy.ticks is FALSE, then no label displayed.
pch
The standard plot character, with values defined in help(points). The default value is 21, a circle with both a border and filled area, specified here as col.point and col.fill. For a scatterplot, col.fill de
cex
Magnification factor for any displayed points, with default of cex=1.0.
center.line
Plots a dashed line through the middle of a run chart. The two possible values are "mean" and "median". Provides a centerline for the "mean" by default when the values randomly vary about the mean.
x.start
Optional starting date for first data value. Format must be "%Y-%m-%d" or "%Y/%m/%d".
x.by
Accompanies the x.start specification, the interval to increment the date for each sequential data value. A character string, containing one of "day", "week", "month" or "year". This s
x.reverse
Reverse the ordering of the dates. Accompanies the x.start specification.
...
Other parameter values for graphics as defined by and then processed by plot and par, including xlim, ylim, lwd, and <

Details

Results are based on the standard plot and related graphic functions, with the additional provided color capabilities and other options including a center line. The plotting procedure utilizes ``adaptive graphics'', such that different default values are chosen for different circumstances of the specified plot and data values. The goal is to produce a desired graph from simply relying upon the default values, both of the color.plot function itself, as well as the base R functions called by color.plot, such as plot. Familiarity with the options permits complete control over the computed defaults, but this familiarity becomes optional if the default values are accepted.

When two variables are specified to plot, if the values of the first variable, x are unsorted, a scatterplot is produced, that is, a plot with type="p" for points. By default the points of a scatterplot are not filled. Sorted values of the first of the two specified variables yield a function plot, that is, a plot with type="l" for a smooth plotted line or curve.

Specifying one variable leads to a run chart, with the values on the horizontal axis automatically generated. The default is the Index variable, the ordinal position of each data value. Or, dates on the horizontal axis can be specified from the specified starting date given by x.start and the accompanying increment as given by x.by. If the data values randomly vary about the mean, the default is to plot the mean as the center line of the graph, otherwise the default is to ignore the center line. The default plot type for the run chart is type="b", for both points and the corresponding connected line segments. The size of the points is automatically reduced according to the number of points of points plotted, and the cex option can override the computed default. If the area below the plotted values is specified to be filled in with color, then the default line type changes to type="l".

Commonly used graphical parameters in standard R graphics functions are generally available to color.plot, such as:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

See Also

plot, title, par.

Examples

Run this code
# scatter plot
x <- rnorm(25)
y <- rnorm(25)
# default scatterplot, x is not sorted so type set to "p"
color.plot(x, y)
# custom scatter plot
color.plot(x, y, col.point="darkred", col.fill="plum")

# function curve
x <- seq(10,500,by=1) 
y <- 18/sqrt(x)
# default function plot,  x is sorted so type set to "l"
color.plot(x, y)
# custom function plot
color.plot(x, y, ylab="My Y", , xlab="My X", col.line="blue", 
  col.bg="snow", col.area="slategray2", col.grid="lightsalmon")
  
# generate data randomly varying about a constant mean
y <- rnorm(25)
# default run chart
color.plot(y)
# customize run chart, pch=24: filled triangle point-up,
color.plot(y, lwd=2, col.point="sienna3", pch=24, 
  col.bg="mintcream", ylim=c(-3,3), center.line="median")
  
# generate steadily increasing values
y <- sort(rexp(50))
# default run chart
color.plot(y)
# run chart with border around plotted values
color.plot(y, col.area="transparent")
# time series chart, i.e., with dates, and filled area
# with option label for the x-axis
color.plot(y, x.start="2005/09/01", x.by="month", col.area="khaki", 
  xlab="Year")

# modern art
n <- sample(2:30, size=1)
x <- rnorm(n)
y <- rnorm(n)
clr <- colors()
color1 <- clr[sample(1:length(clr), size=1)]
color2 <- clr[sample(1:length(clr), size=1)]
color.plot(x, y, type="l", lty="dashed", lwd=3, col.area=color1, 
   col.line=color2, xy.ticks=FALSE, main="Modern Art", 
   cex.main=2, col.main="lightsteelblue")

Run the code above in your browser using DataLab