Learn R Programming

sjPlot (version 2.1.0)

sjp.scatter: Plot (grouped) scatter plots

Description

Display scatter plot of two variables. Adding a grouping variable to the scatter plot is possible. Furthermore, fitted lines can be added for each group as well as for the overall plot.

Usage

sjp.scatter(x = NULL, y = NULL, grp = NULL, title = "", legend.title = NULL, legend.labels = NULL, dot.labels = NULL, axis.titles = NULL, wrap.title = 50, wrap.legend.title = 20, wrap.legend.labels = 20, geom.size = 2, label.size = 3, geom.colors = NULL, show.axis.values = TRUE, fit.line.grps = FALSE, fit.line = FALSE, show.ci = FALSE, fitmethod = "lm", jitter.dots = FALSE, emph.dots = FALSE, auto.jitter = TRUE, jitter.ratio = 0.15, show.rug = FALSE, show.legend = TRUE, facet.grid = FALSE, prnt.plot = TRUE)

Arguments

x
vector indicating the x positions. If not specified (i.e. if NULL), a range from 1 to length of y is used to spread the dots along the x axis.
y
vector indicating the y positions. If not specified (i.e. if NULL), a range from 1 to length of x is used to spread the dots along the y axis.
grp
grouping variable. If not NULL, the scatter plot will be grouped. See 'Examples'. Default is NULL, i.e. not grouping is done.
title
character vector, used as plot title. Depending on plot type and function, will be set automatically. If title = "", no title is printed.
legend.title
character vector, used as title for the plot legend.
legend.labels
character vector with labels for the guide/legend.
dot.labels
character vector with names for each coordinate pair given by x and y, so text labels are added to the plot. Must be of same length as x and y. If dot.labels has a different length, data points will be trimmed to match dot.labels. If dot.labels = NULL (default), no labels are printed.
axis.titles
character vector of length one or two, defining the title(s) for the x-axis and y-axis.
wrap.title
numeric, determines how many chars of the plot title are displayed in one line and when a line break is inserted.
wrap.legend.title
numeric, determines how many chars of the legend's title are displayed in one line and when a line break is inserted.
wrap.legend.labels
numeric, determines how many chars of the legend labels are displayed in one line and when a line break is inserted.
geom.size
size resp. width of the geoms (bar width, line thickness or point size, depending on plot type and function). Note that bar and bin widths mostly need smaller values than dot sizes.
label.size
Size of text labels if argument dot.labels is used.
geom.colors
user defined color for geoms. See 'Details' in sjp.grpfrq.
show.axis.values
logical, whether category, count or percentage values for the axis should be printed or not.
fit.line.grps
logical, if TRUE, a fitted line for each group is drawn. See fitmethod to change the fit method of the fitted lines.
fit.line
logical, if TRUE, a fitted line for the overall scatterplot is drawn. See fitmethod to change the fit method of the fitted line.
show.ci
logical, if TRUE, depending on type, a confidence interval or region is added to the plot.
fitmethod
By default, a linear method ("lm") is used for fitting the fit lines. Possible values are for instance "lm", "glm", "loess" or "auto".
jitter.dots
logical, if TRUE, points will be jittered (to avoid overplotting).
emph.dots
logical, if TRUE, overlapping points at same coordinates will be becomme larger, so point size indicates amount of overlapping.
auto.jitter
logical, if TRUE, points will be jittered according to an overlap-estimation. A matrix of x and y values is created and the amount of cells (indicating a unique point position) is calculated. If more than 15% (see jitter.ratio) of the approximated amount of unique point coordinates seem to overlap, they are automatically jittered.
jitter.ratio
ratio of tolerated overlapping (see auto.jitter). If approximated amount of overlapping points exceed this ratio, they are automatically jittered. Default is 0.15. Valid values range between 0 and 1.
show.rug
logical, if TRUE, a marginal rug plot is displayed in the graph.
show.legend
logical, if TRUE, and depending on plot type and function, a legend is added to the plot.
facet.grid
TRUE to arrange the lay out of of multiple plots in a grid of an integrated single plot. This argument calls facet_wrap or facet_grid to arrange plots. Use plot_grid to plot multiple plot-objects as an arranged grid with grid.arrange.
prnt.plot
logical, if TRUE (default), plots the results as graph. Use FALSE if you don't want to plot any graphs. In either case, the ggplot-object will be returned as value.

Value

(Insisibily) returns the ggplot-object with the complete plot (plot) as well as the data frame that was used for setting up the ggplot-object (df).

See Also

sjPlot manual: sjp.scatter

Examples

Run this code
# load sample date
library(sjmisc)
data(efc)

# simple scatter plot, auto-jittering
sjp.scatter(efc$e16sex, efc$neg_c_7)

# simple scatter plot, no jittering needed
sjp.scatter(efc$c160age, efc$e17age)

# grouped scatter plot
sjp.scatter(efc$c160age, efc$e17age, efc$e42dep)

# grouped and jittered scatter plot with marginal rug plot
sjp.scatter(efc$e16sex,efc$neg_c_7, efc$c172code, show.rug = TRUE)

# grouped and labelled scatter plot, not using the auto-detection
# of labels, but instead pass labels as arguments
sjp.scatter(efc$c160age, efc$e17age, efc$e42dep,
            title = "Scatter Plot", legend.title = get_label(efc)['e42dep'],
            legend.labels = get_labels(efc)[['e42dep']],
            axis.titles = c(get_label(efc)['c160age'], get_label(efc)['e17age']),
            fit.line.grps = TRUE)

# grouped and labelled scatter plot as facets
sjp.scatter(efc$c160age,efc$e17age, efc$e42dep, fit.line.grps = TRUE,
            facet.grid = TRUE, show.ci = TRUE)

# plot residuals of fitted models
fit <- lm(neg_c_7 ~ quol_5, data = efc)
sjp.scatter(y = fit$residuals, fit.line = TRUE)

# "hide" axis titles
sjp.scatter(efc$c160age, efc$e17age, efc$e42dep, title = "", 
            axis.titles = c("", ""))

# plot text labels
pl <- c(1:10)
for (i in 1:10) 
  pl[i] <- paste(sample(c(0:9, letters, LETTERS), 8, replace = TRUE), collapse = "")
sjp.scatter(runif(10), runif(10), dot.labels = pl)

Run the code above in your browser using DataLab