Learn R Programming

sjPlot (version 1.7)

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 = "",
  legendTitle = NULL, legendLabels = NULL, axisTitle.x = NULL,
  axisTitle.y = NULL, breakTitleAt = 50, breakLegendTitleAt = 20,
  breakLegendLabelsAt = 20, geom.size = 3, geom.colors = NULL,
  showTickMarkLabels.x = TRUE, showTickMarkLabels.y = TRUE,
  showGroupFitLine = FALSE, showTotalFitLine = FALSE, showSE = FALSE,
  fitmethod = "lm", useJitter = FALSE, autojitter = TRUE,
  jitterRatio = 0.15, showRug = FALSE, hideLegend = FALSE,
  facet.grid = FALSE, printPlot = TRUE)

Arguments

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

  • http://www.strengejacke.de/sjPlot/sjp.scatter{sjPlot manual: sjp.scatter}

Examples

Run this code
# load sample date
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, showRug=TRUE)

# grouped and labelled scatter plot
sjp.scatter(efc$c160age,efc$e17age, efc$e42dep, title="Scatter Plot",
            legendTitle = get_var_labels(efc)['e42dep'],
            legendLabels = get_val_labels(efc)[['e42dep']],
            axisTitle.x = get_var_labels(efc)['c160age'],
            axisTitle.y = get_var_labels(efc)['e17age'],
            showGroupFitLine = TRUE)

# grouped and labelled scatter plot as facets
sjp.scatter(efc$c160age,efc$e17age, efc$e42dep, title="Scatter Plot",
            legendTitle = get_var_labels(efc)['e42dep'],
            legendLabels = get_val_labels(efc)[['e42dep']],
            axisTitle.x = get_var_labels(efc)['c160age'],
            axisTitle.y = get_var_labels(efc)['e17age'],
            showGroupFitLine = TRUE, facet.grid = TRUE, showSE = TRUE)

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

# -------------------------------
# auto-detection of labels
# -------------------------------
efc <- set_var_labels(efc, get_var_labels(efc))

# show axis titles
sjp.scatter(efc$c160age,efc$e17age, efc$e42dep)

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

Run the code above in your browser using DataLab