blob: cdf1362a75b0d37cb7c11af2376c33c7b23854a3 [file] [log] [blame]
Marc Kupietz08d222b2020-01-16 09:19:08 +01001library(RKorAPClient)
2library(highcharter)
3
4hc_freq_by_year_ci <- function(df, as.alternatives = F, ylabel = if(as.alternatives) "%" else "ipm") {
5 title <- ""
6 df <- df %>%
7 { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) }
8
9 if (!"year" %in% colnames(df)) {
10 df <- df %>% mutate(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = T)))
11 }
12 if (!"condition" %in% colnames(df)) {
13 if (length(base::unique(df$query)) > 1) {
14 df <- df %>% mutate(condition = query)
15 if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) {
16 df <- df %>% mutate(condition = paste(condition, " & ",
17 queryStringToLabel(vc, excludePubDate = T )))
18 }
19 } else {
20 title <- base::unique(df$query)
21 if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) {
22 df <- df %>% mutate(condition = queryStringToLabel(vc, excludePubDate = T ))
23 }
24 }
25 }
26 # use the D3 palette which provides 20 attractive and distinguishable colours
27 palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5")
28 highchart() %>%
29 hc_title(text=title) %>%
30 hc_chart(zoomType="xy") %>%
31 hc_yAxis(
32 title = list(text = if (as.alternatives) "" else ylabel),
33 ceiling = if (as.alternatives) 100 else NULL,
34 floor = 0,
35 labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL
36 ) %>%
37 hc_xAxis(allowDecimals=F) %>%
38 hc_add_theme(hc_theme_google(colors=palette)) %>%
39 hc_plotOptions(
40 series = list(enabled = T),
41 line = list(cursor = 'pointer', point = list(events = list(
42 click = JS("function() { window.open(this.click, 'korap'); }")
43 )))) %>%
44 hc_credits(enabled = T,
45 text = "KorAP R Client Pakckage",
46 href = "//github.com/KorAP/RKorAPClient/") %>%
47 hc_exporting(enabled = T) %>%
48 hc_tooltip(
49 formatter = JS(paste0("function (tooltip) {
50 var str = tooltip.defaultFormatter.call(this, tooltip);
51 if(Array.isArray(str)) {
52 str = str.join('');
53 }
54 for (var i = 0; i < this.points.length; i++) {
55 str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)');
56 }
57 return str.replace(/@/g, '", ylabel, "')
58 } ")),
59 crosshairs = T,
60 valueDecimals = 2,
61 shared = T,
62 valueSuffix = paste0('\U2009', ylabel)
63 ) %>%
64 hc_add_series_korap_frequencies(df, as.alternatives)
65}
66
67hc_add_series_korap_frequencies <- function(hc, df, as.alternatives = F) {
68 index <- 0
69 for(q in unique(df$condition)) {
70 dat <- df[df$condition==q,]
71 hc <- hc %>% hc_add_series(
72 marker = list(radius = 2),
73 name = q,
74 data = data.frame(
75 year = dat$year,
76 value = if (as.alternatives) dat$f else dat$ipm,
77 count = dat$totalResults,
78 click = dat$webUIRequestUrl
79 ),
80 hcaes(year, value),
81 type = 'line',
82 colorIndex = index,
83 zIndex = 1
84 ) %>%
85 hc_add_series(
86 name = "ci",
87 data = dat[,c('year', 'conf.low', 'conf.high')],
88 hcaes(x = year, low = conf.low, high = conf.high),
89 type = 'arearange',
90 fillOpacity = 0.3,
91 lineWidth = 0,
92 marker = list(enabled = F),
93 enableMouseTracking = F,
94 linkedTo= ':previous',
95 colorIndex = index,
96 zIndex = 0
97 )
98 index <- index+1
99 }
100 hc
101}
102
103plotHighchart <- function(query = "Schlumpf",
104 years = c(2000:2010),
105 as.alternatives = length(query) > 1,
106 vc = "textType = /Zeit.*/ & availability!=QAO-NC-LOC:ids & pubDate in",
107 kco = new("KorAPConnection", verbose=T) ) {
108 hc <-
109 frequencyQuery(kco, query, paste(vc, years), as.alternatives=as.alternatives) %>%
110 hc_freq_by_year_ci(as.alternatives)
111 print(hc)
112 hc
113}
114
115saveHCPlot <- function(hc, fname) {
116 htmlwidgets::saveWidget(hc, file=fname, selfcontained = T)
117}
118
119#h1 <-plotHighchart(c("macht []{0,3} Sinn", "ergibt []{0,3} Sinn"), c(1980:2018))
120h1 <- plotHighchart(c("Leser | Lesern | Lesers", 'Leserin | Leserinnen', 'LeserIn | LeserInnen', '"Leser[_\\*]in.*"'), c(1985:2018), as.alternatives = F)
121#plotHighchart(c("Tollpatsch", "Tolpatsch"), c(1991:2018))
122