blob: 9327a32359a729a0a6e05af890aac44d3102f7f2 [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
Marc Kupietz23daf5b2019-11-27 10:28:07 +010024#' Convert corpus frequency table of alternatives to percent
25#'
26#' Convenience function for converting frequency tables of alternative variants
27#' (generated with \code{as.alternatives=T}) to percent.
28#'
29#' @param df table returned from \code{\link{frequencyQuery}}
30#'
31#' @return original table with converted columns \code{f}, \code{conf.low} and \code{conf.high}
32#' @export
33#'
34#' @importFrom dplyr .data
35#'
36#' @examples
37#' new("KorAPConnection") %>%
38#' frequencyQuery(c("Tollpatsch", "Tolpatsch"),
39#' vc=paste0("pubDate in ", 2000:2002),
40#' as.alternatives = TRUE) %>%
41#' percent()
42percent <- function(df) {
43 df %>%
44 mutate(f = .data$f * 10^2, conf.low = .data$conf.low * 10^2, conf.high = .data$conf.high * 10^2)
45}
46
Marc Kupietz95240e92019-11-27 18:19:04 +010047#' Convert query or vc strings to plot labels
48#'
49#' Converts a vector of query or vc strings to typically appropriate legend labels
50#' by clipping off prefixes and suffixes that are common to all query strings.
51#'
52#' @param data string or vector of query or vc definition strings
53#' @return string or vector of strings with clipped off common prefixes and suffixes
54#'
55#' @examples
56#' queryStringToLabel(paste("textType = /Zeit.*/ & pubDate in", c(2010:2019)))
57#' queryStringToLabel(c("[marmot/m=mood:subj]", "[marmot/m=mood:ind]"))
58#' queryStringToLabel(c("wegen dem [tt/p=NN]", "wegen des [tt/p=NN]"))
59#'
60#' @importFrom PTXQC lcpCount
61#' @importFrom PTXQC lcsCount
62#'
63#' @export
64queryStringToLabel <- function(data) {
65 leftCommon = lcpCount(data)
66 while (leftCommon > 0 && grepl("[[:alnum:]]", substring(data[1], leftCommon, leftCommon))) {
67 leftCommon <- leftCommon - 1
68 }
69 rightCommon = lcsCount(data)
70 while (rightCommon > 0 && grepl("[[:alnum:]]", substring(data[1], rightCommon, rightCommon))) {
71 rightCommon <- rightCommon - 1
72 }
73 substring(data, leftCommon + 1, nchar(data) - rightCommon)
74}
75
Marc Kupietzbb7d2322019-10-06 21:42:34 +020076
Marc Kupietz865760f2019-10-07 19:29:44 +020077## Mute notes: "Undefined global functions or variables:"
78globalVariables(c("conf.high", "conf.low", "onRender", "webUIRequestUrl"))
79
80
81#' Experimental: Plot frequency by year graphs with confidence intervals
Marc Kupietzd68f9712019-10-06 21:48:00 +020082#'
Marc Kupietz865760f2019-10-07 19:29:44 +020083#' Experimental convenience function for plotting typical frequency by year graphs with confidence intervals using ggplot2.
84#' \bold{Warning:} This function may be moved to a new package.
85#'
86#' @param mapping Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.
87#' @param ... Other arguments passed to geom_ribbon, geom_line, and geom_click_point.
Marc Kupietzd68f9712019-10-06 21:48:00 +020088#'
89#' @examples
90#' library(ggplot2)
91#' kco <- new("KorAPConnection", verbose=TRUE)
92#' expand_grid(condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
93#' year = (2002:2018)) %>%
94#' cbind(frequencyQuery(kco, "[tt/l=Heuschrecke]",
95#' paste0(.$condition," & pubDate in ", .$year))) %>%
96#' ipm() %>%
Marc Kupietz865760f2019-10-07 19:29:44 +020097#' ggplot(aes(year, ipm, fill = condition, color = condition)) +
Marc Kupietzd68f9712019-10-06 21:48:00 +020098#' geom_freq_by_year_ci()
99#'
Marc Kupietz865760f2019-10-07 19:29:44 +0200100#' @importFrom ggplot2 ggplot aes geom_ribbon geom_line geom_point theme element_text scale_x_continuous
Marc Kupietzd68f9712019-10-06 21:48:00 +0200101#'
102#' @export
Marc Kupietz865760f2019-10-07 19:29:44 +0200103geom_freq_by_year_ci <- function(mapping = aes(ymin=conf.low, ymax=conf.high), ...) {
Marc Kupietzd68f9712019-10-06 21:48:00 +0200104 list(
Marc Kupietz865760f2019-10-07 19:29:44 +0200105 geom_ribbon(mapping,
106 alpha = .3, linetype = 0, show.legend = FALSE, ...),
107 geom_line(...),
108 geom_click_point(aes(url=webUIRequestUrl), ...),
109 theme(axis.text.x = element_text(angle = 45, hjust = 1), legend.position = c(0.8, 0.2)),
Marc Kupietzd68f9712019-10-06 21:48:00 +0200110 scale_x_continuous(breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1 + floor(((x[2]-x[1])/30)))))
111}
112
Marc Kupietz865760f2019-10-07 19:29:44 +0200113#' @importFrom ggplot2 ggproto aes GeomPoint
114GeomClickPoint <- ggproto(
115 "GeomPoint",
116 GeomPoint,
117 required_aes = c("x", "y"),
118 default_aes = aes(
119 shape = 19, colour = "black", size = 1.5, fill = NA,
120 alpha = NA, stroke = 0.5, url = NA
121 ),
122 extra_params = c("na.rm", "url"),
123 draw_panel = function(data, panel_params,
124 coord, na.rm = FALSE, showpoints = TRUE, url = NULL) {
125 GeomPoint$draw_panel(data, panel_params, coord, na.rm = na.rm)
126 }
127)
128
129#' @importFrom ggplot2 layer
130geom_click_point <- function(mapping = NULL, data = NULL, stat = "identity",
131 position = "identity", na.rm = FALSE, show.legend = NA,
132 inherit.aes = TRUE, url = NA, ...) {
133 layer(
134 geom = GeomClickPoint, mapping = mapping, data = data, stat = stat,
135 position = position, show.legend = show.legend, inherit.aes = inherit.aes,
136 params = list(na.rm = na.rm, ...)
137 )
138}
139
140
141#' @importFrom htmlwidgets onRender
142tooltip2hyperlink <- function(p, attribute="webUIRequestUrl") {
143 pattern <- paste0(attribute, ": ([^<]+)")
144 for(i in grep(attribute, p$x$data)) {
145 x <- p[["x"]][["data"]][[i]][["text"]]
146 m <- regexpr(pattern, x)
147 matches <- sub(paste0(attribute, ": "), "", regmatches(x, m))
148 p$x$data[[i]]$customdata <- matches
149 p[["x"]][["data"]][[i]][["text"]] <- sub(paste0(attribute, ":[^<]*<br ?/?>"), "", p[["x"]][["data"]][[i]][["text"]] )
150 }
151 onRender(p, "function(el, x) { el.on('plotly_click', function(d) { var url=d.points[0].customdata; if(url) { window.open(url, 'korap') } })}")
152}
153
154#' Experimental: Convert ggplot2 to plotly with hyperlinks to KorAP queries
155#'
156#' \code{RKorAPClient::ggplotly} converts a \code{ggplot2::ggplot()} object to a plotly
157#' object with hyperlinks from data points to corresponding KorAP queries.
158#' \bold{Warning:} This function may be moved to a new package.
159#'
160#' @param p a ggplot object.
161#' @param tooltip a character vector specifying which aesthetic mappings to show
162#' in the tooltip. If you want hyperlinks to KorAP queries you need to include
163#' \code{"url"} here.
164#' @param ... Other arguments passed to \code{plotly::ggplotly}
165#'
166#' @examples
167#' library(ggplot2)
168#' kco <- new("KorAPConnection", verbose=TRUE)
169#' g <- expand_grid(condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
170#' year = (2002:2018)) %>%
171#' cbind(frequencyQuery(kco, "[tt/l=Heuschrecke]",
172#' paste0(.$condition," & pubDate in ", .$year))) %>%
173#' ipm() %>%
174#' ggplot(aes(year, ipm, fill = condition, color = condition)) +
175#' ## theme_light(base_size = 20) +
176#' geom_freq_by_year_ci()
177#' p <- ggplotly(g)
178#' print(p)
179#' ## saveWidget(p, paste0(tmpdir(), "heuschrecke.html")
180#'
181#'
182#' @importFrom plotly ggplotly
183#' @importFrom htmlwidgets saveWidget
184#' @export
185ggplotly <- function(p = ggplot2::last_plot(), tooltip = c("x", "y", "colour", "url"), ...) {
186 pp <- plotly::ggplotly(p = p, tooltip = tooltip, ...)
187 tooltip2hyperlink(pp)
188}