File: plot_type_slope.R

package info (click to toggle)
r-cran-sjplot 2.8.17%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,596 kB
  • sloc: sh: 13; makefile: 2
file content (191 lines) | stat: -rw-r--r-- 5,044 bytes parent folder | download
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
plot_type_slope <- function(model,
                            terms,
                            rm.terms,
                            ci.lvl,
                            colors,
                            show.data,
                            jitter,
                            facets,
                            axis.title,
                            case,
                            useResiduals,
                            ...) {

  alpha <- .2
  show.loess <- TRUE

  if (missing(facets)) facets <- TRUE

  # additional arguments?

  add.args <- lapply(match.call(expand.dots = F)$`...`, function(x) x)
  if ("alpha" %in% names(add.args)) alpha <- eval(add.args[["alpha"]])
  if ("show.loess" %in% names(add.args)) show.loess <- eval(add.args[["show.loess"]])


  # set color defaults

  colors <- col_check2(colors, if (isTRUE(show.loess)) 3 else 2)

  if (isTRUE(show.loess)) {
    lineColor <- colors[1]
    loessLineColor <- colors[2]
    pointColor <- colors[3]
  } else {
    lineColor <- colors[1]
    pointColor <- colors[2]
  }



  # retrieve amount of predictor variables and
  # retrieve column names of dataset so we can identify in which
  # column the data for each predictor is.

  model_data <- insight::get_data(model, verbose = FALSE)
  depvar.label <- sjlabelled::get_label(model_data[[1]], def.value = insight::find_response(model), case = case)
  predvars <- insight::find_predictors(model, component = "conditional", flatten = TRUE)


  # tell user that interaction terms are not supported by this method

  if (sjmisc::str_contains(deparse(stats::formula(model)), c(":", "*"), logic = "or")) {
    warning("Interaction terms are not supported by this plot type. Output for interaction terms may be inappropriate.", call. = F)
  }


  # remove estimates?

  if (!is.null(rm.terms)) {
    remcols <- match(rm.terms, predvars)

    if (!sjmisc::is_empty(remcols))
      predvars <- predvars[-remcols]
  }


  # select specific setimates?

  if (!is.null(terms)) {
    remcols <- match(terms, predvars)

    if (!sjmisc::is_empty(remcols))
      predvars <- predvars[remcols]
  }


  # retrieve name of dependent variable

  response <- ifelse(isTRUE(useResiduals), "residuals", depvar.label)

  # iterate all predictors

  mydat <- suppressWarnings(purrr::map_df(predvars, function(p_v) {

    # make sure we have the variable in our data. for mixed
    # models, we might have some random effects which were not
    # in the model frame

    if (obj_has_name(model_data, p_v)) {

      if (useResiduals) {
        data_frame(
          x = sjlabelled::as_numeric(model_data[[p_v]]),
          y = stats::residuals(model),
          group = sjlabelled::get_label(model_data[[p_v]], def.value = p_v, case = case)
        )
      } else {
        data_frame(
          x = sjlabelled::as_numeric(model_data[[p_v]]),
          y = insight::get_response(model),
          group = sjlabelled::get_label(model_data[[p_v]], def.value = p_v, case = case)
        )
      }

    }
  }))


  # facets, all in one plot

  if (facets) {

    p <- ggplot(mydat, aes(x = .data$x, y = .data$y)) +
      stat_smooth(
        method = "lm", se = !is.na(ci.lvl), colour = lineColor,
        fill = lineColor, alpha = alpha, level = ci.lvl
      )

    if (isTRUE(show.loess))
      p <- p + stat_smooth(method = "loess", colour = loessLineColor, se = FALSE)


    # plot raw data if requested

    if (show.data) {
      if (!is.null(jitter))
        p <- p + geom_jitter(alpha = .2, colour = pointColor, shape = 16, width = jitter)
      else
        p <- p + geom_point(alpha = .2, colour = pointColor, shape = 16)
    }



    p <- p + facet_wrap(~group, scales = "free")


    # set plot labs
    p <- p + labs(x = NULL, y = response)

  } else {

    p <- list()

    for (p_v in unique(mydat$group)) {

      dat <- dplyr::filter(mydat, .data$group == !! p_v)

      pl <- ggplot(dat, aes(x = .data$x, y = .data$y)) +
        stat_smooth(
          method = "lm", se = !is.na(ci.lvl), colour = lineColor,
          fill = lineColor, alpha = alpha, level = ci.lvl
        )

      if (isTRUE(show.loess))
        pl <- pl + stat_smooth(method = "loess", colour = loessLineColor, se = FALSE)


      # plot raw data if requested

      if (show.data)
        pl <- pl + geom_point(alpha = .2, colour = pointColor, shape = 16)


      # set plot labs. check if we have custom axis titles

      if (!is.null(axis.title)) {

        if (is.list(axis.title)) {
          xt <- axis.title[[length(p) + 1]][1]
          yt <- axis.title[[length(p) + 1]][2]
        } else {
          xt <- axis.title[1]
          yt <- axis.title[2]
        }

      } else {
        xt <- p_v
        yt <- response
      }

      pl <- pl +
        labs(x = xt, y = yt)


      # add plot object to list
      p[[length(p) + 1]] <- pl
    }
  }

  p
}