PlotSlope: Simple slopes plot

Description Usage Arguments Details Value Author(s) Examples

Description

Simple slope plot for two and three way interactions.

Usage

1
2
PlotSlope(object, namemod = "default", namex = "default",
namey = "default", limitx = "default", limity = "default")

Arguments

object

an object of class "simpleSlope".

namemod

a character verctor of the moderator points. If "default" is used, default setting is printed.

namex

name of the predictor. If "default" is used, the predictor name in the dataframe is printed.

namey

name of the dependent variable. If "default" is used, the dataframe name is printed.

limitx

a numeric vector for setting limits of x axis.

limity

a numeric vector for setting limits of y axis.

Details

Plot for Simple slope analysis.

Value

PlotSlope returns an object of class "ggplot".

Author(s)

Alberto Mirisola and Luciano Seta

Examples

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
	
	## Default plot for three way interaction
	
	library(car)
	data(Highway1)
	model3<-lmres(rate~len*trks*sigs1, centered=c("len","trks","sigs1"),data=Highway1)
	S_slopes<-simpleSlope(model3,pred="len",mod1="trks", mod2="sigs1")
	Plot<-PlotSlope(S_slopes)
	
	## Personalized plot for three way interaction
	
	library(car)
	data(Highway1)
	model3<-lmres(rate~len*trks*sigs1, centered=c("len","trks","sigs1"),data=Highway1)
	S_slopes<-simpleSlope(model3,pred="len",mod1="trks", mod2="sigs1")
	Plot<-PlotSlope(S_slopes, namemod=c("Low truck volume (-1SD),
	Low number of signals per mile (-1 SD)","Low truck volume (-1SD),
	High number of signals per mile (+1 SD)","High truck volume (+1SD),
	Low number of signals per mile (-1 SD)","High truck volume (+1SD), 
	High number of signals per mile (+1 SD)"),
	namex="length of the Highway1\n segment in miles",
	namey="1973 accident rate \n per million vehicle miles",
	limitx=c(-9,9), limity=c(-2,9))


## The function is currently defined as
function(object, namemod = "default", 
namex = "default", namey = "default", limitx = "default", 
limity = "default") {

pmatr <- object$Points
nomY <- object$nomY
nomX <- object$nomX
X_1L <- object$X_1L
X_1H <- object$X_1H

if (object$orde == 2) {
nam <- dimnames(object$simple_slope)[1]
nam <- nam[[1]]
r1 <- nam[1]
r2 <- nam[2]

xini <- rep(X_1L, 4)
xend <- rep(X_1H, 4)
fact <- c(5, 6)
mat <- cbind(fact, xini, pmatr[, 1], xend, pmatr[, 2])
mat <- as.data.frame(mat)
names(mat) <- c("fact", "xini", "yini", "xend", "yend")
p <- ggplot(mat, aes(x = xini, y = yini))
p1 <- p + geom_segment(aes(xend = xend, yend = yend))
p1 <- p1 + scale_x_continuous(nomX) + scale_y_continuous(nomY)
p1 <- p1 + geom_point(size = 3, aes(shape = factor(fact))) + 
geom_point(aes(x = xend, y = yend, shape = factor(fact)), 
size = 3)

if (length(namemod) == 1) {
p1 <- p1 + scale_shape(name = "Moderator", breaks = c(5, 
6), labels = c(r1, r2))
  }
if (length(namemod) > 1) {
if (length(namemod) != 2) {
stop("length of namemod vector must be = 2")
  }
p1 <- p1 + scale_shape(name = "Moderator", breaks = c(5, 
6), labels = namemod)
  }

if (namex != "default") {
if (length(limitx) == 2) {
p1 <- p1 + scale_x_continuous(namex, limits = limitx)
  }
else {
p1 <- p1 + scale_x_continuous(namex)
  }

  }

if (namey != "default") {
if (length(limity) == 2) {
p1 <- p1 + scale_y_continuous(namey, limits = limity)
  }
else {
p1 <- p1 + scale_y_continuous(namey)
  }
  }



return(p1)
  }

if (object$orde == 3) {

nam <- dimnames(object$simple_slope)[1]
nam <- nam[[1]]
r1 <- nam[1]
r2 <- nam[2]
r3 <- nam[3]
r4 <- nam[4]


xini <- rep(X_1L, 4)
xend <- rep(X_1H, 4)
fact <- c(5, 6, 7, 8)
mat <- cbind(fact, xini, pmatr[, 1], xend, pmatr[, 2])
mat <- as.data.frame(mat)
names(mat) <- c("fact", "xini", "yini", "xend", "yend")
p <- ggplot(mat, aes(x = xini, y = yini))
p1 <- p + geom_segment(aes(xend = xend, yend = yend))
p1 <- p1 + scale_x_continuous(nomX) + scale_y_continuous(nomY)
p1 <- p1 + geom_point(size = 3, aes(shape = factor(fact))) + 
geom_point(aes(x = xend, y = yend, shape = factor(fact)), 
size = 3)
if (length(namemod) == 1) {
p1 <- p1 + scale_shape(name = "Moderators Combination", 
breaks = c(5, 6, 7, 8), labels = c(r1, r2, r3, 
r4))
  }
if (length(namemod) > 1) {
if (length(namemod) != 4) {
stop("length of namemod vector must be = 4")
  }
p1 <- p1 + scale_shape(name = "Moderators Combination", 
breaks = c(5, 6, 7, 8), labels = namemod)
  }
p2 <- p1

if (namex != "default") {
if (length(limitx) == 2) {
p2 <- p2 + scale_x_continuous(namex, limits = limitx)
  }
else {
p2 <- p2 + scale_x_continuous(namex)
  }

  }

if (namey != "default") {
if (length(limity) == 2) {
p2 <- p2 + scale_y_continuous(namey, limits = limity)
  }
else {
p2 <- p2 + scale_y_continuous(namey)
  }
  }

return(p2)
  }

  }

Example output

Loading required package: ggplot2
Loading required package: car
Scale for 'x' is already present. Adding another scale for 'x', which will
replace the existing scale.
Scale for 'y' is already present. Adding another scale for 'y', which will
replace the existing scale.
function (object, namemod = "default", namex = "default", namey = "default", 
    limitx = "default", limity = "default") 
{
    pmatr <- object$Points
    nomY <- object$nomY
    nomX <- object$nomX
    X_1L <- object$X_1L
    X_1H <- object$X_1H
    if (object$orde == 2) {
        nam <- dimnames(object$simple_slope)[1]
        nam <- nam[[1]]
        r1 <- nam[1]
        r2 <- nam[2]
        xini <- rep(X_1L, 4)
        xend <- rep(X_1H, 4)
        fact <- c(5, 6)
        mat <- cbind(fact, xini, pmatr[, 1], xend, pmatr[, 2])
        mat <- as.data.frame(mat)
        names(mat) <- c("fact", "xini", "yini", "xend", "yend")
        p <- ggplot(mat, aes(x = xini, y = yini))
        p1 <- p + geom_segment(aes(xend = xend, yend = yend))
        p1 <- p1 + scale_x_continuous(nomX) + scale_y_continuous(nomY)
        p1 <- p1 + geom_point(size = 3, aes(shape = factor(fact))) + 
            geom_point(aes(x = xend, y = yend, shape = factor(fact)), 
                size = 3)
        if (length(namemod) == 1) {
            p1 <- p1 + scale_shape(name = "Moderator", breaks = c(5, 
                6), labels = c(r1, r2))
        }
        if (length(namemod) > 1) {
            if (length(namemod) != 2) {
                stop("length of namemod vector must be = 2")
            }
            p1 <- p1 + scale_shape(name = "Moderator", breaks = c(5, 
                6), labels = namemod)
        }
        if (namex != "default") {
            if (length(limitx) == 2) {
                p1 <- p1 + scale_x_continuous(namex, limits = limitx)
            }
            else {
                p1 <- p1 + scale_x_continuous(namex)
            }
        }
        if (namey != "default") {
            if (length(limity) == 2) {
                p1 <- p1 + scale_y_continuous(namey, limits = limity)
            }
            else {
                p1 <- p1 + scale_y_continuous(namey)
            }
        }
        return(p1)
    }
    if (object$orde == 3) {
        nam <- dimnames(object$simple_slope)[1]
        nam <- nam[[1]]
        r1 <- nam[1]
        r2 <- nam[2]
        r3 <- nam[3]
        r4 <- nam[4]
        xini <- rep(X_1L, 4)
        xend <- rep(X_1H, 4)
        fact <- c(5, 6, 7, 8)
        mat <- cbind(fact, xini, pmatr[, 1], xend, pmatr[, 2])
        mat <- as.data.frame(mat)
        names(mat) <- c("fact", "xini", "yini", "xend", "yend")
        p <- ggplot(mat, aes(x = xini, y = yini))
        p1 <- p + geom_segment(aes(xend = xend, yend = yend))
        p1 <- p1 + scale_x_continuous(nomX) + scale_y_continuous(nomY)
        p1 <- p1 + geom_point(size = 3, aes(shape = factor(fact))) + 
            geom_point(aes(x = xend, y = yend, shape = factor(fact)), 
                size = 3)
        if (length(namemod) == 1) {
            p1 <- p1 + scale_shape(name = "Moderators Combination", 
                breaks = c(5, 6, 7, 8), labels = c(r1, r2, r3, 
                  r4))
        }
        if (length(namemod) > 1) {
            if (length(namemod) != 4) {
                stop("length of namemod vector must be = 4")
            }
            p1 <- p1 + scale_shape(name = "Moderators Combination", 
                breaks = c(5, 6, 7, 8), labels = namemod)
        }
        p2 <- p1
        if (namex != "default") {
            if (length(limitx) == 2) {
                p2 <- p2 + scale_x_continuous(namex, limits = limitx)
            }
            else {
                p2 <- p2 + scale_x_continuous(namex)
            }
        }
        if (namey != "default") {
            if (length(limity) == 2) {
                p2 <- p2 + scale_y_continuous(namey, limits = limity)
            }
            else {
                p2 <- p2 + scale_y_continuous(namey)
            }
        }
        return(p2)
    }
}

pequod documentation built on May 2, 2019, 7:11 a.m.

Related to PlotSlope in pequod...