Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 1 | #' 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}} |
| 10 | #' @param as.alternatives boolean decides whether queries should be treatet queries as mutually exclusive and exahustive wrt. to some meaningful class (e.g. spelling variants of a certain word form). |
| 11 | #' @param ylabel defaults to \% if \code{as.alternatives} is \code{true} and to "ipm" otherwise. |
| 12 | #' |
| 13 | #' @examples |
| 14 | #' new("KorAPConnection", verbose = TRUE) %>% |
| 15 | #' frequencyQuery(query = c("macht []{0,3} Sinn", "ergibt []{0,3} Sinn"), |
| 16 | #' vc = paste("textType = /Zeit.*/ & pubDate in", c(2010:2014)), |
| 17 | #' as.alternatives = TRUE) %>% |
| 18 | #' hc_freq_by_year_ci(as.alternatives = TRUE) |
| 19 | #' |
| 20 | hc_freq_by_year_ci <- function(df, as.alternatives = F, ylabel = if(as.alternatives) "%" else "ipm") { |
| 21 | title <- "" |
| 22 | df <- df %>% |
| 23 | { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) } |
| 24 | |
| 25 | if (!"year" %in% colnames(df)) { |
| 26 | df <- df %>% mutate(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = T))) |
| 27 | } |
| 28 | if (!"condition" %in% colnames(df)) { |
| 29 | if (length(base::unique(df$query)) > 1) { |
| 30 | df <- df %>% mutate(condition = query) |
| 31 | if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) { |
| 32 | df <- df %>% mutate(condition = paste(condition, " & ", |
| 33 | queryStringToLabel(vc, excludePubDate = T ))) |
| 34 | } |
| 35 | } else { |
| 36 | title <- base::unique(df$query) |
| 37 | if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = T ))) > 1) { |
| 38 | df <- df %>% mutate(condition = queryStringToLabel(vc, excludePubDate = T )) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | # use the D3 palette which provides 20 attractive and distinguishable colours |
| 43 | palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5") |
| 44 | highcharter::highchart() %>% |
| 45 | hc_title(text=title) %>% |
| 46 | hc_chart(zoomType="xy") %>% |
| 47 | hc_yAxis( |
| 48 | title = list(text = if (as.alternatives) "" else ylabel), |
| 49 | ceiling = if (as.alternatives) 100 else NULL, |
| 50 | floor = 0, |
| 51 | labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL |
| 52 | ) %>% |
| 53 | hc_xAxis(allowDecimals=F) %>% |
| 54 | hc_add_theme(hc_theme_google(colors=palette)) %>% |
| 55 | hc_plotOptions( |
| 56 | series = list(enabled = T), |
| 57 | line = list(cursor = 'pointer', point = list(events = list( |
| 58 | click = JS("function() { window.open(this.click, 'korap'); }") |
| 59 | )))) %>% |
| 60 | hc_credits(enabled = T, |
| 61 | text = "KorAP R Client Pakckage", |
| 62 | href = "//github.com/KorAP/RKorAPClient/") %>% |
| 63 | hc_exporting(enabled = T) %>% |
| 64 | hc_tooltip( |
| 65 | formatter = JS(paste0("function (tooltip) { |
| 66 | var str = tooltip.defaultFormatter.call(this, tooltip); |
| 67 | if(Array.isArray(str)) { |
| 68 | str = str.join(''); |
| 69 | } |
| 70 | for (var i = 0; i < this.points.length; i++) { |
| 71 | str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)'); |
| 72 | } |
| 73 | return str.replace(/@/g, '", ylabel, "') |
| 74 | } ")), |
| 75 | crosshairs = T, |
| 76 | valueDecimals = 2, |
| 77 | shared = T, |
| 78 | valueSuffix = paste0('\U2009', ylabel) |
| 79 | ) %>% |
| 80 | hc_add_series_korap_frequencies(df, as.alternatives) |
| 81 | } |
| 82 | |
| 83 | hc_add_series_korap_frequencies <- function(hc, df, as.alternatives = F) { |
| 84 | index <- 0 |
| 85 | for(q in unique(df$condition)) { |
| 86 | dat <- df[df$condition==q,] |
| 87 | hc <- hc %>% hc_add_series( |
| 88 | marker = list(radius = 2), |
| 89 | name = q, |
| 90 | data = data.frame( |
| 91 | year = dat$year, |
| 92 | value = if (as.alternatives) dat$f else dat$ipm, |
| 93 | count = dat$totalResults, |
| 94 | click = dat$webUIRequestUrl |
| 95 | ), |
| 96 | hcaes(year, value), |
| 97 | type = 'line', |
| 98 | colorIndex = index, |
| 99 | zIndex = 1 |
| 100 | ) %>% |
| 101 | hc_add_series( |
| 102 | name = "ci", |
| 103 | data = dat[,c('year', 'conf.low', 'conf.high')], |
| 104 | hcaes(x = year, low = conf.low, high = conf.high), |
| 105 | type = 'arearange', |
| 106 | fillOpacity = 0.3, |
| 107 | lineWidth = 0, |
| 108 | marker = list(enabled = F), |
| 109 | enableMouseTracking = F, |
| 110 | linkedTo= ':previous', |
| 111 | colorIndex = index, |
| 112 | zIndex = 0 |
| 113 | ) |
| 114 | index <- index+1 |
| 115 | } |
| 116 | hc |
| 117 | } |