Learn R Programming

sjPlot (version 1.8.3)

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, pointLabels = 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, show.se = 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
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, showRug = 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",
            legendTitle = get_label(efc)['e42dep'],
            legendLabels = get_labels(efc)[['e42dep']],
            axisTitle.x = get_label(efc)['c160age'],
            axisTitle.y = get_label(efc)['e17age'],
            showGroupFitLine = TRUE)

# grouped and labelled scatter plot as facets
sjp.scatter(efc$c160age,efc$e17age, efc$e42dep,
            showGroupFitLine = TRUE,
            facet.grid = TRUE,
            show.se = TRUE)

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

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

# 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), pointLabels = pl)

Run the code above in your browser using DataLab