blob: c9c9ac1448c6d98fc96d7b8afa08e9f7e6a1aada [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.
12#'
13#' @examples
Marc Kupietz657d8e72020-02-25 18:31:50 +010014#' \donttest{year <- c(1990:2018)}\dontshow{year <- c(2013:2013)}
15#' \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 +010016#' new("KorAPConnection", verbose = TRUE) %>%
Marc Kupietz657d8e72020-02-25 18:31:50 +010017#' frequencyQuery(query = alternatives,
Marc Kupietz05b22772020-02-18 21:58:42 +010018#' vc = paste("textType = /Zeit.*/ & pubDate in", year),
Marc Kupietz91145b02020-01-29 15:58:36 +010019#' as.alternatives = TRUE) %>%
20#' hc_freq_by_year_ci(as.alternatives = TRUE)
21#'
Marc Kupietz05b22772020-02-18 21:58:42 +010022#' \donttest{
Marc Kupietz10f65c42020-01-31 15:18:24 +010023#' kco <- new("KorAPConnection", verbose = TRUE)
24#' expand_grid(
25#' condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
26#' year = (2005:2011)
27#' ) %>%
28#' cbind(frequencyQuery(
29#' kco,
30#' "[tt/l=Heuschrecke]",
31#' paste0(.$condition, " & pubDate in ", .$year)
32#' )) %>%
33#' hc_freq_by_year_ci()
Marc Kupietz05b22772020-02-18 21:58:42 +010034#' }
Marc Kupietz10f65c42020-01-31 15:18:24 +010035#'
Marc Kupietz91145b02020-01-29 15:58:36 +010036hc_freq_by_year_ci <- function(df, as.alternatives = F, ylabel = if(as.alternatives) "%" else "ipm") {
37 title <- ""
38 df <- df %>%
39 { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) }
40
41 if (!"year" %in% colnames(df)) {
42 df <- df %>% mutate(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = T)))
43 }
44 if (!"condition" %in% colnames(df)) {
45 if (length(base::unique(df$query)) > 1) {
46 df <- df %>% mutate(condition = query)
47 if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) {
48 df <- df %>% mutate(condition = paste(condition, " & ",
49 queryStringToLabel(vc, excludePubDate = T )))
50 }
51 } else {
52 title <- base::unique(df$query)
53 if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) {
54 df <- df %>% mutate(condition = queryStringToLabel(vc, excludePubDate = T ))
55 }
56 }
57 }
58 # use the D3 palette which provides 20 attractive and distinguishable colours
59 palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5")
60 highcharter::highchart() %>%
61 hc_title(text=title) %>%
62 hc_chart(zoomType="xy") %>%
63 hc_yAxis(
64 title = list(text = if (as.alternatives) "" else ylabel),
65 ceiling = if (as.alternatives) 100 else NULL,
66 floor = 0,
67 labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL
68 ) %>%
69 hc_xAxis(allowDecimals=F) %>%
70 hc_add_theme(hc_theme_google(colors=palette)) %>%
71 hc_plotOptions(
72 series = list(enabled = T),
73 line = list(cursor = 'pointer', point = list(events = list(
74 click = JS("function() { window.open(this.click, 'korap'); }")
75 )))) %>%
76 hc_credits(enabled = T,
77 text = "KorAP R Client Pakckage",
78 href = "//github.com/KorAP/RKorAPClient/") %>%
79 hc_exporting(enabled = T) %>%
80 hc_tooltip(
81 formatter = JS(paste0("function (tooltip) {
82 var str = tooltip.defaultFormatter.call(this, tooltip);
83 if(Array.isArray(str)) {
84 str = str.join('');
85 }
86 for (var i = 0; i < this.points.length; i++) {
87 str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)');
88 }
89 return str.replace(/@/g, '", ylabel, "')
90 } ")),
91 crosshairs = T,
92 valueDecimals = 2,
93 shared = T,
94 valueSuffix = paste0('\U2009', ylabel)
95 ) %>%
96 hc_add_series_korap_frequencies(df, as.alternatives)
97}
98
Marc Kupietza4f36532020-02-03 22:50:08 +010099## Mute notes: "no visible binding for global variable:"
100globalVariables(c("value", "query", "condition", "vc"))
101
Marc Kupietz91145b02020-01-29 15:58:36 +0100102hc_add_series_korap_frequencies <- function(hc, df, as.alternatives = F) {
103 index <- 0
104 for(q in unique(df$condition)) {
105 dat <- df[df$condition==q,]
106 hc <- hc %>% hc_add_series(
107 marker = list(radius = 2),
108 name = q,
109 data = data.frame(
110 year = dat$year,
111 value = if (as.alternatives) dat$f else dat$ipm,
112 count = dat$totalResults,
113 click = dat$webUIRequestUrl
114 ),
115 hcaes(year, value),
116 type = 'line',
117 colorIndex = index,
118 zIndex = 1
119 ) %>%
120 hc_add_series(
121 name = "ci",
122 data = dat[,c('year', 'conf.low', 'conf.high')],
123 hcaes(x = year, low = conf.low, high = conf.high),
124 type = 'arearange',
125 fillOpacity = 0.3,
126 lineWidth = 0,
127 marker = list(enabled = F),
128 enableMouseTracking = F,
129 linkedTo= ':previous',
130 colorIndex = index,
131 zIndex = 0
132 )
133 index <- index+1
134 }
135 hc
136}