lessR (version 2.5)

ScatterPlot: Scatterplot for One (Dot Plot) or Two Variables


Abbreviation: sp

Genderates scatterplots for one or two variables. For two variables a scatterplot is produced accompanyied by the analysis of the correlation coefficient. For a data frame, a scatterplot matrix and correlation matrix are produced for all numeric variables in the data frame. If the values of the first specified value are sorted, then points are connected via line segments. The first variable can be numeric or a factor. The second variable must be numeric. For Likert style response data of two variables, so that each value has less than 10 unique integer values, the points in the plot are transformed into a bubble plot with the size of each bubble, i.e., point, determined by the corresponding joint frequency. An alternate name for ScatterPlot is just Plot.

One enhancement over the standard R 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, grid lines, and border can each be explicitly specified, with default colors provided by one of the pre-defined color themes as defined by the set function.

If a scatterplot of two numeric variables is displayed, then the corresponding correlation coefficient as well as the hypothesis test of zero population correlation and the 95% confidence interval are also displayed. The same numeric values of the standard R function cor.test function are generated, though in a more readable format. Also, an option for the .95 data ellipse from John Fox's car package can enclose the points of the scatterplot.

For one variable, based on the standard R function stripchart, plots a one dimensional scatterplot, that is, a dot chart, also called a strip chart. Also identifies outliers according to the criteria specified by a box plot and displays the summary statistics for the variable. The dot plot is also invoked with the function names DotPlot or just dp, which are just alternate names for ScatterPlot when a single variable is referenced.


ScatterPlot(x, y=NULL, by=NULL, dframe=mydata, type=NULL, n.cat=getOption("n.cat"),

col.pts=NULL, col.fill=NULL, trans.pts=getOption("trans.pts"), shape.pts="circle",

col.line=NULL, col.area=NULL, col.box="black", col.grid=NULL, col.bg=NULL, colors=c("blue", "gray", "rose", "green", "gold", "red"),

cex.axis=.85, col.axis="gray30", col.ticks="gray30", xy.ticks=TRUE, xlab=NULL, ylab=NULL, main=NULL, cex=NULL, x.start=NULL, x.end=NULL, y.start=NULL, y.end=NULL, time.start=NULL, time.by=NULL, time.reverse=FALSE,

kind=c("default", "regular", "bubble", "sunflower"),

fit.line=c("none", "loess", "ls"), col.fit.line="grey55",

col.bubble=NULL, bubble.size=.25, col.flower=NULL,

ellipse=FALSE, col.ellipse="lightslategray", fill.ellipse=TRUE,

pt.reg="circle", pt.out="circle", col.out30="firebrick2", col.out15="firebrick4", new=TRUE,


pdf.file=NULL, pdf.width=5, pdf.height=5, ...)



DotPlot(...) dp(...)


If both x and y are specified, then the x values are plotted on the horizontal axis. If x is not sorted, a scatterplot is produced. If x is sorted, then a function is plotted with a smooth line. If only x is specified with no y, then
Coordinates of points in the plot on the vertical axis.
An optional grouping variable such that the points of all (x,y) pairs are plotted in the same plotting symbol and/or same color, with a different symbol or symbol and/or color for each group. Applies only to kind="regular"
Optional data frame that contains one or both of the variables of interest, default is mydata.
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 "
When analyzing all the variables in a data frame, specifies the largest number of unique values of variable of a numeric data type for which the variable will be analyzed as categorical. Set to 0 to turn off.
Border color of the plotted points. If there is a by variable, the color of plotted points for each level of the by (grouping) variable, specified as a vector, one value, point symbol, for each level of by.
For plotted points, the interior color of the points. By default, is a partially transparent version of the border color, col.pts.
Transparency of the plotted points, from opaque at 0 to completely transparent at 1. Default is 0.66.
The standard plot character, with values defined in points. The default value is 21, a circle with both a border and filled area, specified here with col.pts and col.fill
Color of any plotted line segments, with a default of "darkblue".
Color of area under the plotted line segments.
Color of border around the plot background, the box, that encloses the plot, with a default of "black".
Color of the grid lines, with a default of "grey90".
Color of the plot background.
Sets the color palette.
Scale magnification factor, which by defaults displays the axis values to be smaller than the axis labels.
Color of the font used to label the axis values.
Color of the ticks used to label the axis values.
Flag that indicates if tick marks and associated values on the axes are to be displayed.
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 v
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.
Label for the title of the graph. If the corresponding variable labels exist in the data frame mylabels, then the title is set by default from the corresponding variable labels.
Magnification factor for any displayed points, with default of cex=1.0.
For Likert style response data, the starting integer value of the x-axis. Useful if the actual data do not include all possible values.
For Likert style response data, the ending integer value of the x-axis. Useful if the actual data do not include all possible values.
For Likert style response data, the starting integer value of the y-axis. Useful if the actual data do not include all possible values.
For Likert style response data, the ending integer value of the y-axis. Useful if the actual data do not include all possible values.
Optional starting date for first data value. Format must be "%Y-%m-%d" or "%Y/%m/%d". If using with x.reverse, the first date is after the data are reverse sorted. Not needed if data are a time series with
Accompanies the time.start specification, the interval to increment the date for each sequential data value. A character string, containing one of "day", "week", "month" or "year"
When TRUE, reverse the ordering of the dates, particularly when the data are listed such that first row of data is the newest. Accompanies the time.start specification.
Default is "default", which becomes a "regular" scatterplot for most data. If Likert style response data is plotted, that is, each variable has less than 10 integer values, then instead by default a bubble plot is
The best fitting line. Default value is "none", with options for "loess" and "ls".
Color of the best fitting line, if the fit.line option is invoked.
Color of the bubbles if a bubble plot of the frequencies is plotted.
Size of the bubbles in a bubble plot of Likert style data.
Color of the flowers if a sunflower plot of the frequencies is plotted.
If TRUE, enclose a scatterplot with the .95 data ellipse from the car package.
Color of the ellipse.
If TRUE, fill the ellipse with a translucent shade of col.ellipse.
For dot plot, type of regular (non-outlier) point. Default is 21, a circle with specified fill.
For a dot plot, type of point for outliers. Default is 19, a filled circle.
For a dot plot, color of severe outliers.
For a dot plot, color of potential outliers.
If TRUE, then display text output in console window.
If FALSE, then add the dot plot to an existing graph.
Name of the pdf file to which graphics are redirected.
Width of the pdf file in inches.
Height of the pdf file in inches.
Other parameter values for graphics as defined by and then processed by plot and par, including xlim, ylim, lwd,


DATA If the variable is in a data frame, the input data frame has the assumed name of mydata. If this data frame is named something different, then specify the name with the dframe option. Regardless of its name, the data frame need not be attached to reference the variable directly by its name, that is, no need to invoke the mydata$name notation. If two variables are specified, both variables should be in the data frame, or one of the variables is in the data frame and the other in the user's workspace, the global environment.

ADAPTIVE GRAPHICS Results for two variablesare 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 ScatterPlot chooses different default values for different characteristics 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 ScatterPlot function itself, as well as the base R functions called by ScatterPlot, such as plot. Familiarity with the options permits complete control over the computed defaults, but this familiarity is intended to be optional for most situations.

TWO VARIABLE PLOT When two variables are specified to plot, by default if the values of the first variable, x, are unsorted, or if there are unequal intervals between adjacent values, or if there is missing data for either variable, a scatterplot is produced, that is, a call to the standard R plot function with type="p" for points. By default, sorted values with equal intervals between adjacent values of the first of the two specified variables yields a function plot if there is no missing data for either variable, that is, a call to the standard R plot function with type="l", which connects each adjacent pair of points with a line segment.

BY VARIABLE A variable specified with by= is a grouping variable that specifies that the plot is produced with the points for each group plotted with a different shape and/or color. By default, the shapes vary by group, and the color of the plot symbol remains the same for the groups. The default shapes, in this order, are "circle", "diamond", "square", "triup" for a triangle pointed up, and "tridown" for a triangle pointed down.

To explicitly vary the shapes, use shape.pts and a list of shape values in the standard R form with the c function to combine a list of values, one specified shape for each group, as shown in the examples. To explicitly vary the colors, use col.pts, such as with R standard color names. If col.pts is specified without shape.pts, then colors are varied, but not shapes. To vary both shapes and colors, specify values for both options, always with one shape or color specified for each level of the by variable.

Shapes beyond the standard list of named shapes, such as "circle", are also available as single characters. Any single letter, uppercase or lowercase, any single digit, and the characters "+", "*" and "#" are available, as illustrated in the examples. In the use of shape.pts, either use standard named shapes, or individual characters, but not both in a single specification.

SCATTERPLOT ELLIPSE For a scatterplot of two numeric variables, the ellipse=TRUE option draws the .95 data ellipse as computed by the dataEllipse function, written by Georges Monette and John Fox, from the car package. Usually the minimum and maximum values of the axes should be manually extended beyond their default to accommodate the entire ellipse. To accomplish this extension, use the xlim and ylim options, such as xlim=c(30,350). Obtaining the desired axes limits may involve multiple runs of the ScatterPlot function. To provide more control over the display of the data ellipse beyond the provided col.ellipse and fill.ellipse options, run the dataEllipse function directly with the plot.points=FALSE option following ScatterPlot with ellipse=FALSE, the default.

ONE VARIABLE PLOT The one variable plot is a one variable scatterplot, that is, a dot chart. Results are based on the standard stripchart function. Colors are provided by default and can also be specified.

MULTIPLE VARIABLE PLOT If the variable, x is a data frame, then the data frame must contain only numeric variables. If not, the first non-numeric variable is noted and the procedure ends. Otherwise, the procedure generates the scatterplot matrix with the R pairs function as well as the correlation matrix of all the variables in the data frame with the R cor function.

LIKERT DATA A scatterplot of Likert type data is problematic because there are so few possibilities for points in the scatterplot. For example, for a scatterplot of two five-point Likert response data, there are only 25 possible paired values to plot, so most of the plotted points overlap with others. In this situation, that is, when there are less than 10 values for each of the two variables, a bubble plot is automatically provided, with the size of each point relative to the joint frequency of the paired data values. A sunflower plot can be requested in lieu of the bubble plot.

VARIABLE LABELS Although standard R does not provide for variable labels, lessR can store the labels in a data frame called mylabels, obtained from the Read function. If this labels data frame exists, then the corresponding variable label is by default listed as the label for the corresponding axis and on the text output. For more information, see Read.

COLOR The default background color of col.bg=ghostwhite provides a very mild cool tone with a slight emphasis on blue. The entire color theme can be specified at the system level with the lessR function set using the colors option. Or, use the same option for ScatterPlot to set the color theme just for one scatterplot. The default color theme is blue, but a gray scale is available with "gray", and other themes are available as explained in the help function for set.

Colors can also be changed for individual aspects of a scatterplot as well. To provide a warmer tone by slightly enhancing red, try col.bg=snow. Obtain a very light gray with col.bg=gray99. To darken the background gray, try col.bg=gray97 or lower numbers. See the lessR function showColors which provides an example of all available named colors.

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.

ADDITIONAL OPTIONS Commonly used graphical parameters that are available to the standard R function plot are also generally available to ScatterPlot, such as:

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


Monette, G. and Fox, J., dataEllipse function from the car package.

See Also

plot, stripchart, title, par, Correlation, set.


Run this code
# scatterplot
# create simulated data, no population mean difference
# X has two values only, Y is numeric
# put into a data frame, required for formula version
n <- 12
f <- sample(c("Group1","Group2"), size=n, replace=TRUE)
x <- round(rnorm(n=n, mean=50, sd=10), 2)
y <- round(rnorm(n=n, mean=50, sd=10), 2)
z <- round(rnorm(n=n, mean=50, sd=10), 2)
mydata <- data.frame(f,x,y,z)
rm(f); rm(x); rm(y); rm(z)

# default scatterplot, x is not sorted so type is set to "p"
# although data not attached, access each variable directly by its name
ScatterPlot(x, y)
# short name
# compare to standard R plot, which requires the mydata$ notation
plot(mydata$x, mydata$y)
# save scatterplot to a pdf file
ScatterPlot(x, y, pdf.file="MyScatterScatterPlot.pdf")
# scatterplot, with ellipse and extended axes to accommodate the ellipse
ScatterPlot(x, y, ellipse=TRUE, xlim=c(20,80), ylim=c(20,80))
# scatterplot, with loess line 
ScatterPlot(x, y, fit.line="loess")
# increase span (smoothing) from default of .75
ScatterPlot(x, y, fit.line="loess", span=1.25)
# custom scatterplot
ScatterPlot(x, y, col.pts="darkred", col.fill="plum")
# scatterplot with a gray scale color theme 
ScatterPlot(x, y, colors="gray")

# by variable scatterplot with default point color, vary shapes
ScatterPlot(x,y, by=f)
# by variable scatterplot with custom colors, keeps only 1 shape
ScatterPlot(x,y, by=f, col.pts=c("hotpink", "steelblue"))
# by variable with characters for plotting symbols
# reduce the size of the plotted symbols with cex<1
ScatterPlot(x, y, by=f, shape.pts=c("F","M"), cex=.6)
# vary both shape and color 
ScatterPlot(x, y, by=f, col.pts=c("hotpink", "steelblue"),
# by variable dot plot with custom colors, keeps only 1 shape
ScatterPlot(x, by=f, col.pts=c("hotpink", "steelblue"))

# bubble plot of simulated Likert data, 1 to 7 scale
# size of each plotted point (bubble) depends on its joint frequency
# triggered by default when  < 10 unique values for each variable
x1 <- sample(1:7, size=100, replace=TRUE)
x2 <- sample(1:7, size=100, replace=TRUE)
# compare to usual scatterplot of Likert data, transparency helps
ScatterPlot(x1,x2, kind="regular", cex=3, trans.pts=.7)
# plot Likert data and get sunflower plot with loess line
ScatterPlot(x1,x2, kind="sunflower", fit.line="loess")

# scatterplot of continuous Y against categorical X, a factor
Pain <- sample(c("None", "Some", "Much", "Massive"), size=25, replace=TRUE)
Pain <- factor(Pain, levels=c("None", "Some", "Much", "Massive"), ordered=TRUE)
Cost <- round(rnorm(25,1000,100),2)
ScatterPlot(Pain, Cost)
# for this purpose, improved version of standard R stripchart
stripchart(Cost ~ Pain, vertical=TRUE)

# function curve
x <- seq(10,500,by=1) 
y <- 18/sqrt(x)
# x is sorted with equal intervals so type set to "l" for line
ScatterPlot(x, y)
# custom function plot
ScatterPlot(x, y, ylab="My Y", xlab="My X", col.line="blue", 
  col.bg="snow", col.area="lightsteelblue", col.grid="lightsalmon")

# Default dot plot
# can also specify DotPlot(y) or dp(y)
# Dot plot with custom colors for outliers
ScatterPlot(y, pt.reg=23, col.out15="hotpink", col.out30="darkred")

# 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)]
ScatterPlot(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", kind="regular",

# -----------------------------------------------
# variables in a different data frame than mydata
# -----------------------------------------------

# variables of interest are in a data frame which is not the default mydata
# although data not attached, access the variable directly by its name
ScatterPlot(Years, Salary, by=Gender, dframe=datEmployee)

Run the code above in your browser using DataCamp Workspace