|
1 | 1 | #' @title Scaling Factor for Combined Histogramm Plots |
2 | 2 | #' |
3 | | -#' @description Requires a dataframe as produced by datsteps(). |
4 | | -#' Meaning 6 columns in the following order: |
5 | | -#' * ID, |
6 | | -#' * group, |
7 | | -#' * minimum/earliest date, |
8 | | -#' * maximum/latest date, |
9 | | -#' * weight, |
10 | | -#' * 'DAT_Steps' |
| 3 | +#' @description Requires a dataframe as produced by datsteps() or a number as DAT_df_steps. Calculated the value with which the |
| 4 | +#' y-axis of a density graph should be multplied in order to be visible in the corresponding histogram. |
11 | 5 | #' |
12 | | -#' @param DAT_df a dataframe as returned by datsteps |
13 | | -#' @param bw the bandwidth to use for the density function and histogram. Should be stepsize used to |
14 | | -#' create the dataframe. If a df as returned by datsteps() is given, stepsize is automatically assigned |
15 | | -#' using the corresponding attribute. |
| 6 | +#' @param DAT_df_steps a dataframe as returned by datsteps |
| 7 | +#' @param binwidth the bandwidth to use for the density function and histogram. Should be stepsize used to |
| 8 | +#' create the dataframe. If a df as returned by datsteps() is given, stepsize can be automatically assigned |
| 9 | +#' using the corresponding attribute (binwidth = "stepsize") |
16 | 10 | #' |
17 | 11 | #' @return the value with which to scale the density curve to a histogram plot so that both will be visible |
18 | 12 | #' |
19 | 13 | #' @examples |
20 | 14 | #' DAT_df_steps <- datsteps(DAT_df[1:100,], stepsize = 25) |
21 | 15 | #' get.histogramscale(DAT_df_steps) |
22 | 16 | #' |
| 17 | +#' get.histogramscale(500, binwidth = 20) |
| 18 | +#' |
23 | 19 | #' @export get.histogramscale |
24 | 20 |
|
25 | | -get.histogramscale <- function(DAT_df, bw = "auto") { |
26 | | - if (bw == "auto") { |
27 | | - if (!is.null(attributes(DAT_df)$stepsize)) { |
28 | | - bw <- attributes(DAT_df)$stepsize |
29 | | - } else { |
30 | | - stop("Either specify stepsize/bandwidth (bw = ) or use a data.frame as returned by datsteps()") |
| 21 | +get.histogramscale <- function(DAT_df_steps, binwidth = "stepsize") { |
| 22 | + if (check.number(DAT_df_steps)) { |
| 23 | + nrow <- DAT_df_steps |
| 24 | + if (binwidth == "stepsize") { |
| 25 | + stop("'binwidth == stepsize' cannot be used with a number, supply either a dataframe as returned by datsteps or a numerical bindwidth") |
| 26 | + } |
| 27 | + } else { |
| 28 | + nrow <- nrow(DAT_df_steps) |
| 29 | + if (binwidth == "stepsize") { |
| 30 | + binwidth <- attributes(DAT_df_steps)$stepsize |
| 31 | + if (is.null(binwidth)) { |
| 32 | + stop("Supply numerical binwidth or dataframe as returned by datsteps") |
| 33 | + } |
| 34 | + } else if (!check.number(binwidth)) { |
| 35 | + stop('Supply numerical binwidth or use binwidth = "stepsize".') |
31 | 36 | } |
32 | | - } else if (!is.numeric(bw)) { |
33 | | - stop("Either specify stepsize/bandwidth (bw = ) or use a data.frame as returned by datsteps()") |
34 | 37 | } |
35 | | - density <- density(DAT_df$DAT_step, weights = DAT_df$weight, bw = bw) |
36 | | - breaks <- range(DAT_df$DAT_step) |
37 | | - breaks <- breaks[2] - breaks [1] |
38 | | - breaks <- round(breaks / bw, digits = 0) |
39 | | - hist <- hist(DAT_df$DAT_step, breaks = breaks) |
40 | | - histogramscale <- max(hist$counts) / max(density$y) |
| 38 | + histogramscale <- nrow * binwidth |
41 | 39 | return(histogramscale) |
42 | 40 | } |
0 commit comments