blob: 3933e8894e8b69318fca4e9df9c8355e74c18b6e [file] [log] [blame]
Marc Kupietz91145b02020-01-29 15:58:36 +01001#' Experimental: Plot interactive frequency by year graphs with confidence intervals using highcharter
2#'
3#' Experimental convenience function for plotting typical frequency by year graphs with confidence intervals using highcharter.
4#' \bold{Warning:} This function may be moved to a new package.
5#'
6#' @import highcharter
7#' @export
8#'
9#' @param df data frame like the value of a \code{\link{frequencyQuery}}
Marc Kupietz43a6ade2020-02-18 17:01:44 +010010#' @param as.alternatives boolean decides whether queries should be treated as mutually exclusive and exhaustive wrt. to some meaningful class (e.g. spelling variants of a certain word form).
Marc Kupietz91145b02020-01-29 15:58:36 +010011#' @param ylabel defaults to \% if \code{as.alternatives} is \code{true} and to "ipm" otherwise.
Marc Kupietzab0b0712020-05-04 16:24:57 +020012#' @param smooth boolean decides whether the graph is smoothed using the highcharts plot types spline and areasplinerange.
Marc Kupietz5b503f42020-04-09 15:26:00 +020013#' @param ... additional arguments passed to \code{\link{hc_add_series}}
Marc Kupietz91145b02020-01-29 15:58:36 +010014#'
15#' @examples
Marc Kupietz657d8e72020-02-25 18:31:50 +010016#' \donttest{year <- c(1990:2018)}\dontshow{year <- c(2013:2013)}
17#' \donttest{alternatives <- c("macht []{0,3} Sinn", "ergibt []{0,3} Sinn")}\dontshow{alternatives <- c("macht []{0,3} Sinn")}
Marc Kupietz91145b02020-01-29 15:58:36 +010018#' new("KorAPConnection", verbose = TRUE) %>%
Marc Kupietz657d8e72020-02-25 18:31:50 +010019#' frequencyQuery(query = alternatives,
Marc Kupietz05b22772020-02-18 21:58:42 +010020#' vc = paste("textType = /Zeit.*/ & pubDate in", year),
Marc Kupietz91145b02020-01-29 15:58:36 +010021#' as.alternatives = TRUE) %>%
22#' hc_freq_by_year_ci(as.alternatives = TRUE)
23#'
Marc Kupietz05b22772020-02-18 21:58:42 +010024#' \donttest{
Marc Kupietz10f65c42020-01-31 15:18:24 +010025#' kco <- new("KorAPConnection", verbose = TRUE)
26#' expand_grid(
27#' condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
28#' year = (2005:2011)
29#' ) %>%
30#' cbind(frequencyQuery(
31#' kco,
32#' "[tt/l=Heuschrecke]",
33#' paste0(.$condition, " & pubDate in ", .$year)
34#' )) %>%
35#' hc_freq_by_year_ci()
Marc Kupietz05b22772020-02-18 21:58:42 +010036#' }
Marc Kupietz10f65c42020-01-31 15:18:24 +010037#'
Marc Kupietzab0b0712020-05-04 16:24:57 +020038hc_freq_by_year_ci <- function(df, as.alternatives = FALSE,
39 ylabel = if(as.alternatives) "%" else "ipm",
40 smooth = FALSE,
41 ...) {
Marc Kupietz91145b02020-01-29 15:58:36 +010042 title <- ""
43 df <- df %>%
44 { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) }
45
46 if (!"year" %in% colnames(df)) {
Marc Kupietzcf1771d2020-03-04 16:03:04 +010047 df <- df %>% mutate(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = TRUE)))
Marc Kupietz91145b02020-01-29 15:58:36 +010048 }
49 if (!"condition" %in% colnames(df)) {
50 if (length(base::unique(df$query)) > 1) {
51 df <- df %>% mutate(condition = query)
Marc Kupietzcf1771d2020-03-04 16:03:04 +010052 if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) {
Marc Kupietz91145b02020-01-29 15:58:36 +010053 df <- df %>% mutate(condition = paste(condition, " & ",
Marc Kupietzcf1771d2020-03-04 16:03:04 +010054 queryStringToLabel(vc, excludePubDate = TRUE )))
Marc Kupietz91145b02020-01-29 15:58:36 +010055 }
56 } else {
Marc Kupietz5d70ffe2020-03-12 11:16:43 +010057 if (length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) {
58 title <- base::unique(df$query)
Marc Kupietzcf1771d2020-03-04 16:03:04 +010059 df <- df %>% mutate(condition = queryStringToLabel(vc, excludePubDate = TRUE ))
Marc Kupietz5d70ffe2020-03-12 11:16:43 +010060 } else {
61 df <- df %>% mutate(condition = query)
Marc Kupietz91145b02020-01-29 15:58:36 +010062 }
63 }
64 }
65 # use the D3 palette which provides 20 attractive and distinguishable colours
66 palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5")
67 highcharter::highchart() %>%
68 hc_title(text=title) %>%
Marc Kupietz91145b02020-01-29 15:58:36 +010069 hc_yAxis(
70 title = list(text = if (as.alternatives) "" else ylabel),
71 ceiling = if (as.alternatives) 100 else NULL,
72 floor = 0,
73 labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL
74 ) %>%
Marc Kupietzcf1771d2020-03-04 16:03:04 +010075 hc_xAxis(allowDecimals=FALSE) %>%
Marc Kupietz91145b02020-01-29 15:58:36 +010076 hc_add_theme(hc_theme_google(colors=palette)) %>%
77 hc_plotOptions(
Marc Kupietzcf1771d2020-03-04 16:03:04 +010078 series = list(enabled = TRUE),
Marc Kupietzab0b0712020-05-04 16:24:57 +020079 spline = list(cursor = 'pointer', point = list(events = list(
80 click = JS("function() { window.open(this.click, 'korap'); }")
81 ))),
Marc Kupietz91145b02020-01-29 15:58:36 +010082 line = list(cursor = 'pointer', point = list(events = list(
83 click = JS("function() { window.open(this.click, 'korap'); }")
84 )))) %>%
Marc Kupietzcf1771d2020-03-04 16:03:04 +010085 hc_credits(enabled = TRUE,
Marc Kupietz900d0522020-03-19 13:36:45 +010086 text = "KorAP R Client Package",
87 href = "https://github.com/KorAP/RKorAPClient/") %>%
Marc Kupietzcf1771d2020-03-04 16:03:04 +010088 hc_exporting(enabled = TRUE) %>%
Marc Kupietz91145b02020-01-29 15:58:36 +010089 hc_tooltip(
Marc Kupietzab0b0712020-05-04 16:24:57 +020090 headerFormat = '<span style="font-size: 10pt">{point.key}</span><br/>',
Marc Kupietz91145b02020-01-29 15:58:36 +010091 formatter = JS(paste0("function (tooltip) {
92 var str = tooltip.defaultFormatter.call(this, tooltip);
93 if(Array.isArray(str)) {
94 str = str.join('');
95 }
96 for (var i = 0; i < this.points.length; i++) {
97 str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)');
98 }
99 return str.replace(/@/g, '", ylabel, "')
100 } ")),
Marc Kupietzcf1771d2020-03-04 16:03:04 +0100101 crosshairs = TRUE,
Marc Kupietz91145b02020-01-29 15:58:36 +0100102 valueDecimals = 2,
Marc Kupietzcf1771d2020-03-04 16:03:04 +0100103 shared = TRUE,
Marc Kupietz91145b02020-01-29 15:58:36 +0100104 valueSuffix = paste0('\U2009', ylabel)
105 ) %>%
Marc Kupietzab0b0712020-05-04 16:24:57 +0200106 hc_add_series_korap_frequencies(df, smooth, as.alternatives, ...)
Marc Kupietz91145b02020-01-29 15:58:36 +0100107}
108
Marc Kupietza4f36532020-02-03 22:50:08 +0100109## Mute notes: "no visible binding for global variable:"
110globalVariables(c("value", "query", "condition", "vc"))
111
Marc Kupietzab0b0712020-05-04 16:24:57 +0200112hc_add_series_korap_frequencies <- function(hc, df, smooth = FALSE,
113 as.alternatives = FALSE,
114 ...) {
Marc Kupietz91145b02020-01-29 15:58:36 +0100115 index <- 0
Marc Kupietzab0b0712020-05-04 16:24:57 +0200116 type <- ifelse(smooth, "spline", "line")
117 areatype <- ifelse(smooth, "areasplinerange", "arearange")
Marc Kupietz91145b02020-01-29 15:58:36 +0100118 for(q in unique(df$condition)) {
119 dat <- df[df$condition==q,]
120 hc <- hc %>% hc_add_series(
121 marker = list(radius = 2),
122 name = q,
123 data = data.frame(
124 year = dat$year,
125 value = if (as.alternatives) dat$f else dat$ipm,
126 count = dat$totalResults,
127 click = dat$webUIRequestUrl
128 ),
129 hcaes(year, value),
Marc Kupietzab0b0712020-05-04 16:24:57 +0200130 type = type,
Marc Kupietz91145b02020-01-29 15:58:36 +0100131 colorIndex = index,
Marc Kupietz5b503f42020-04-09 15:26:00 +0200132 zIndex = 1,
133 ...
Marc Kupietz91145b02020-01-29 15:58:36 +0100134 ) %>%
135 hc_add_series(
136 name = "ci",
137 data = dat[,c('year', 'conf.low', 'conf.high')],
138 hcaes(x = year, low = conf.low, high = conf.high),
Marc Kupietzab0b0712020-05-04 16:24:57 +0200139 type = areatype,
Marc Kupietz91145b02020-01-29 15:58:36 +0100140 fillOpacity = 0.3,
141 lineWidth = 0,
Marc Kupietzcf1771d2020-03-04 16:03:04 +0100142 marker = list(enabled = FALSE),
143 enableMouseTracking = FALSE,
Marc Kupietz91145b02020-01-29 15:58:36 +0100144 linkedTo= ':previous',
145 colorIndex = index,
146 zIndex = 0
147 )
148 index <- index+1
149 }
150 hc
151}
Marc Kupietz882f08c2020-03-18 13:30:31 +0100152
153.onAttach <- function(libname = find.package("RKorAPClient"),
154 pkgname = "RKorAPClient") {
155 packageStartupMessage(
156 "If you intend to use the Highcharts plot options, please note that Highcharts (www.highcharts.com) is a Highsoft software product which is not free for commercial and governmental use."
157 )
158}
159