blob: 9a97f22d902a03360083d03523df2464a35dfd1e [file] [log] [blame]
Marc Kupietzbb7d2322019-10-06 21:42:34 +02001
2#' Convert corpus frequency table to instances per million.
3#'
4#' Convenience function for converting frequency tables to instances per
5#' million.
6#'
7#' Given a table with columns \code{f}, \code{conf.low}, and \code{conf.high}, \code{ipm} ads a \code{column ipm}
8#' und multiplies conf.low and \code{conf.high} with 10^6.
9#'
10#' @param df table returned from \code{\link{frequencyQuery}}
11#'
12#' @return original table with additional column \code{ipm} and converted columns \code{conf.low} and \code{conf.high}
13#' @export
14#'
15#' @importFrom dplyr .data
16#'
17#' @examples
18#' new("KorAPConnection") %>% frequencyQuery("Test", paste0("pubDate in ", 2000:2002)) %>% ipm()
19ipm <- function(df) {
20 df %>%
21 mutate(ipm = .data$f * 10^6, conf.low = .data$conf.low * 10^6, conf.high = .data$conf.high * 10^6)
22}
23
24
Marc Kupietzd68f9712019-10-06 21:48:00 +020025#' Plot frequency by year graphs with confidence intervals
26#'
27#' Convenience function for plotting typical frequency by year graphs with confidence intervals using ggplot2.
28#'
29#' @examples
30#' library(ggplot2)
31#' kco <- new("KorAPConnection", verbose=TRUE)
32#' expand_grid(condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
33#' year = (2002:2018)) %>%
34#' cbind(frequencyQuery(kco, "[tt/l=Heuschrecke]",
35#' paste0(.$condition," & pubDate in ", .$year))) %>%
36#' ipm() %>%
37#' ggplot(aes(year, ipm, fill = condition, color = condition, ymin = conf.low, ymax = conf.high)) +
38#' geom_freq_by_year_ci()
39#'
40#' @importFrom ggplot2 geom_ribbon geom_line geom_point theme element_text scale_x_continuous
41#'
42#' @export
43geom_freq_by_year_ci <- function() {
44 list(
45 geom_ribbon(alpha = .3, linetype = 0, show.legend = FALSE),
46 geom_line(),
47 geom_point(),
48 theme(axis.text.x = element_text(angle = 45, hjust = 1)),
49 scale_x_continuous(breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1 + floor(((x[2]-x[1])/30)))))
50}
51