crossplot_print_stats: A convience function to print the plot stats and a regression...

Description Usage Arguments Value Examples

View source: R/crossplot_print_stats.R

Description

A convience function to print the plot stats and a regression line

Usage

1
2
3
4
crossplot_print_stats(p, text.pos, stats = c("slope", "r.squared"),
  reg.color = "black", reg.linetype = "solid", log.reg = FALSE,
  weighted = FALSE, sprintf.format = "%.2f", xlabel = NULL,
  ylabel = NULL, reg.label.se = TRUE, mean.label.se = FALSE, ...)

Arguments

p

a crossplot ggplot2 plot. Can be generated from crossplot

text.pos

the text position to be passed to cowplot::draw_label() as a numeric vector of length 2 with x and y.

stats

character vector with the stats that will be printed (in order). Possible statistics include "slope", "intercept", "r.squared", "adj.r.squared", "mean.x", "mean.y" "var.x", "var.y", "sd.x", "sd.y". Defualt is c("slope", "r.squared")

reg.color

a string with the ggplot2 color for the regression line

reg.linetype

a string with the ggplot2 linetype for the regression line

log.reg

logical. If set to TRUE, the regression will be estimated in logs. Defualt is FALSE

weighted

logical. If set to TRUE, the regression and all statistics will be weighted using the size variable form the ggplot2 plot. Default is FALSE

sprintf.format

character string with the sprintf format.

xlabel

the label for the x-axis variable

ylabel

the label for the y-axis variable

reg.label.se

logical. If set to TRUE, the regression standard errors will be printed. Default is TRUE

mean.label.se

logical If set to TRUE, the standard errors for the means will be printed. Default is FALSE

...

Other arguments to be passed to cowplot::draw_label. See ?cowplot::draw_label for options and more details.

Value

a ggplot2 plot with the regression line and label added

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
data(mtcars)
p <- crossplot(mtcars, x.var = "hp", y.var = "mpg", size.var = "wt",
               shapes.var = "cyl")
##Print weighted regression line and weighted stats
crossplot_print_stats(p, text.pos = c(200, 25), weighted = TRUE)
##Print unweighted regression line and unweighted stats
crossplot_print_stats(p, text.pos = c(200, 25), weighted = FALSE)

##passing other arguments to cowplot::draw_label.
##See ?cowplot::draw_label for more details
crossplot_print_stats(p, text.pos = c(200, 25), weighted = FALSE,
                      fontface = "bold")

## -- Controlling for qsec weighted by wt -- ##

##First see the results using a normal regression
mod <- lm(mpg ~ hp + qsec, mtcars, weights = wt)
library(sandwich); library(lmtest)
coeftest(mod, vcov = sandwich)
##Now in a plot
p2 <- crossplot(mtcars, x.var = "hp", y.var = "mpg", size.var = "wt",
                shapes.var = "cyl", control.vars = "qsec")
crossplot_print_stats(p2, text.pos = c(0, 7), weighted = TRUE)

ChandlerLutz/crossplotr documentation built on May 6, 2019, 9:56 a.m.