File: sjplot.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 (328 lines) | stat: -rw-r--r-- 10,468 bytes parent folder | download | duplicates (3)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#' @title Wrapper to create plots and tables within a pipe-workflow
#' @name sjplot
#'
#' @description This function has a pipe-friendly argument-structure, with the
#'              first argument always being the data, followed by variables that
#'              should be plotted or printed as table. The function then transforms
#'              the input and calls the requested sjp.- resp. sjt.-function
#'              to create a plot or table. \cr \cr
#'              Both \code{sjplot()} and \code{sjtab()} support grouped data frames.
#'
#' @param data A data frame. May also be a grouped data frame (see 'Note' and
#'          'Examples').
#' @param ... Names of variables that should be plotted, and also further
#'          arguments passed down to the \pkg{sjPlot}-functions. See 'Examples'.
#' @param fun Plotting function. Refers to the function name of \pkg{sjPlot}-functions.
#'          See 'Details' and 'Examples'.
#'
#' @return See related sjp. and sjt.-functions.
#'
#' @note The \code{...}-argument is used, first, to specify the variables from \code{data}
#'       that should be plotted, and, second, to name further arguments that are
#'       used in the subsequent plotting functions. Refer to the online-help of
#'       supported plotting-functions to see valid arguments.
#'       \cr \cr
#'       \code{data} may also be a grouped data frame (see \code{\link[dplyr]{group_by}})
#'       with up to two grouping variables. Plots are created for each subgroup then.
#'
#' @details Following \code{fun}-values are currently supported:
#'          \describe{
#'             \item{\code{"aov1"}}{calls \code{\link{sjp.aov1}}. The first
#'             two variables in \code{data} are used (and required) to create the plot.
#'             }
#'             \item{\code{"grpfrq"}}{calls \code{\link{plot_grpfrq}}. The first
#'             two variables in \code{data} are used (and required) to create the plot.
#'             }
#'             \item{\code{"likert"}}{calls \code{\link{plot_likert}}. \code{data}
#'             must be a data frame with items to plot.
#'             }
#'             \item{\code{"stackfrq"}}{calls \code{\link{tab_stackfrq}}.
#'             \code{data} must be a data frame with items to create the table.
#'             }
#'             \item{\code{"xtab"}}{calls \code{\link{plot_xtab}} or \code{\link{tab_xtab}}.
#'             The first two variables in \code{data} are used (and required)
#'             to create the plot or table.
#'             }
#'          }
#'
#' @examples
#' library(dplyr)
#' data(efc)
#'
#' # Grouped frequencies
#' efc %>% sjplot(e42dep, c172code, fun = "grpfrq")
#'
#' # Grouped frequencies, as box plots
#' efc %>% sjplot(e17age, c172code, fun = "grpfrq",
#'                type = "box", geom.colors = "Set1")
#'
#' \dontrun{
#' # table output of grouped data frame
#' efc %>%
#'   group_by(e16sex, c172code) %>%
#'   select(e42dep, n4pstu, e16sex, c172code) %>%
#'   sjtab(fun = "xtab", use.viewer = FALSE) # open all tables in browser}
#'
#' @importFrom sjmisc is_empty
#' @importFrom sjlabelled copy_labels get_label get_labels
#' @importFrom dplyr filter
#' @importFrom tidyr nest
#' @importFrom stats complete.cases
#' @export
sjplot <- function(data, ..., fun = c("grpfrq", "xtab", "aov1", "likert")) {
  # check if x is a data frame
  if (!is.data.frame(data)) stop("`data` must be a data frame.", call. = F)

  # match arguments
  fun <- match.arg(fun)

  # evaluate arguments, generate data
  x <- get_dot_data(data, match.call(expand.dots = FALSE)$`...`)

  # check remaining arguments
  args <- match.call(expand.dots = FALSE)$`...`
  args <- args[names(args) != ""]

  p <- NULL
  pl <- NULL

  # do we have a grouped data frame?
  if (inherits(x, "grouped_df")) {
    # get grouped data
    grps <- get_grouped_data(x)

    # now plot everything
    for (i in seq_len(nrow(grps))) {
      # copy back labels to grouped data frame
      tmp <- sjlabelled::copy_labels(grps$data[[i]], x)

      # prepare argument list, including title
      tmp.args <- get_grouped_title(x, grps, args, i, sep = "\n")

      # plot
      plots <- plot_sj(tmp, fun, tmp.args)

      # add plots, check for NULL results
      if (!is.null(plots$p)) pl <- c(pl, list(plots$p))
      if (!is.null(plots$pl)) pl <- c(pl, plots$pl)
    }
  } else {
    # plot
    plots <- plot_sj(x, fun, args)
    # we only have one plot call
    p <- plots$p
    pl <- plots$pl
  }

  # print all plots
  if (!is.null(pl)) {
    for (p in pl) suppressWarnings(graphics::plot(p))
    invisible(pl)
  } else {
    suppressWarnings(graphics::plot(p))
    invisible(p)
  }
}


#' @rdname sjplot
#' @export
sjtab <- function(data, ..., fun = c("xtab", "stackfrq")) {
  # check if x is a data frame
  if (!is.data.frame(data)) stop("`data` must be a data frame.", call. = F)

  # match fun-arguments
  fun <- match.arg(fun)

  # evaluate arguments, generate data
  x <- get_dot_data(data, match.call(expand.dots = FALSE)$`...`)

  tabs.list <- list()

  # check remaining arguments
  args <- match.call(expand.dots = FALSE)$`...`
  args <- args[names(args) != ""]

  # do we have a grouped data frame?
  if (inherits(x, "grouped_df")) {
    # get grouped data
    grps <- get_grouped_data(x)

    # now plot everything
    for (i in seq_len(nrow(grps))) {
      # copy back labels to grouped data frame
      tmp <- sjlabelled::copy_labels(grps$data[[i]], x)

      # prepare argument list, including title
      tmp.args <- get_grouped_title(x, grps, args, i, sep = "<br>")

      # table
      tl <- tab_sj(tmp, fun, tmp.args)

      # save list
      tabs.list[[length(tabs.list) + 1]] <- tl
    }

    final.table <- paste0(
      tl$header,
      tl$page.style,
      "\n</head>\n<body>\n"
    )

    final.knitr <- ""

    # iterate table list
    for (i in seq_len(length(tabs.list))) {
      final.table <- paste0(final.table, tabs.list[[i]]$page.content, sep = "\n<p>&nbsp;</p>\n")
      final.knitr <- paste0(final.knitr, tabs.list[[i]]$knitr, sep = "\n<p>&nbsp;</p>\n")
    }

    # close html tags
    final.table <- paste0(final.table, "\n</body>\n</html>")

    # return all tables
    return(structure(
      class = c("sjTable", "sjtab"),
      list(
        page.style = tl$page.style,
        header = tl$header,
        page.content = final.table,
        page.complete = final.table,
        knitr = final.knitr,
        file = eval(args[["file"]]),
        viewer = if (is.null(args[["use.viewer"]])) TRUE else eval(args[["use.viewer"]])
      )
    ))
  } else {
    # plot
    tab_sj(x, fun, args)
  }
}


get_grouped_plottitle <- function(x, grps, i, sep = "\n") {
  # prepare title for group
  tp <- get_title_part(x, grps, 1, i)
  title <- sprintf("%s: %s", tp[1], tp[2])

  # do we have another groupng variable?
  if (length(dplyr::group_vars(x)) > 1) {
    # prepare title for group
    tp <- get_title_part(x, grps, 2, i)
    title <- sprintf("%s%s%s: %s", title, sep, tp[1], tp[2])
  }

  title
}


get_grouped_title <- function(x, grps, args, i, sep = "\n") {
  # prepare title for group
  tp <- get_title_part(x, grps, 1, i)
  title <- sprintf("%s: %s", tp[1], tp[2])

  # do we have another groupng variable?
  if (length(dplyr::group_vars(x)) > 1) {
    # prepare title for group
    tp <- get_title_part(x, grps, 2, i)
    title <- sprintf("%s%s%s: %s", title, sep, tp[1], tp[2])
  }

  # add title argument to argument list
  c(args, `title` = title)
}


#' @importFrom sjlabelled get_values get_label get_labels
get_title_part <- function(x, grps, level, i) {
  # prepare title for group
  var.name <- colnames(grps)[level]

  # get values from value labels
  vals <- sjlabelled::get_values(x[[var.name]])
  # if we have no value labels, get values directly
  if (is.null(vals)) vals <- unique(x[[var.name]])
  # find position of value labels for current group
  lab.pos <- which(vals == grps[[var.name]][i])

  # get variable and value labels
  t1 <- sjlabelled::get_label(x[[var.name]], def.value = var.name)
  t2 <- sjlabelled::get_labels(x[[var.name]])[lab.pos]

  # if we have no value label, use value instead
  if (is.null(t2)) t2 <- vals[lab.pos]

  # generate title
  c(t1, t2)
}


#' @importFrom rlang .data
#' @importFrom dplyr select filter group_modify group_vars
#' @importFrom stats complete.cases
#'
get_grouped_data <- function(x) {
  # retain observations that are complete wrt grouping vars, then nest
  grps <- x %>%
    dplyr::group_modify(~ dplyr::filter(.x, stats::complete.cases(.y))) %>%
    tidyr::nest()

  # arrange data
  if (length(dplyr::group_vars(x)) == 1)
    reihe <- order(grps[[1]])
  else
    reihe <- order(grps[[1]], grps[[2]])
  grps <- grps[reihe, ]

  grps
}


plot_sj <- function(x, fun, args) {
  p <- NULL
  pl <- NULL

  # choose plottype, and call plot-function with or w/o additional arguments
  if (sjmisc::is_empty(args)) {
    if (fun  == "grpfrq") {
      p <- plot_grpfrq(x[[1]], x[[2]])
    } else if (fun  == "likert") {
      p <- plot_likert(x)
    } else if (fun  == "xtab") {
      p <- plot_xtab(x[[1]], x[[2]])
    } else if (fun  == "aov1") {
      p <- sjp.aov1(x[[1]], x[[2]])
    }
  } else {
    if (fun  == "grpfrq") {
      p <- do.call(plot_grpfrq, args = c(list(var.cnt = x[[1]], var.grp = x[[2]]), args))
    } else if (fun  == "likert") {
      p <- do.call(plot_likert, args = c(list(items = x), args))
    } else if (fun  == "xtab") {
      p <- do.call(plot_xtab, args = c(list(x = x[[1]], grp = x[[2]]), args))
    } else if (fun  == "aov1") {
      p <- do.call(sjp.aov1, args = c(list(var.dep = x[[1]], var.grp = x[[2]]), args))
    }
  }

  list(p = p, pl = pl)
}


tab_sj <- function(x, fun, args) {

  # choose plottype, and call plot-function with or w/o additional arguments
  if (sjmisc::is_empty(args)) {
    if (fun  == "xtab") {
      tab_xtab(x[[1]], x[[2]])
    } else if (fun  == "stackfrq") {
      tab_stackfrq(x)
    }
  } else {
    if (fun  == "stackfrq") {
      do.call(tab_stackfrq, args = c(list(items = x), args))
    } else if (fun  == "xtab") {
      do.call(tab_xtab, args = c(list(var.row = x[[1]], var.col = x[[2]]), args))
    }
  }
}