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}} |
Marc Kupietz | 43a6ade | 2020-02-18 17:01:44 +0100 | [diff] [blame] | 10 | #' @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 Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 11 | #' @param ylabel defaults to \% if \code{as.alternatives} is \code{true} and to "ipm" otherwise. |
| 12 | #' |
| 13 | #' @examples |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 14 | #' \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 Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 16 | #' new("KorAPConnection", verbose = TRUE) %>% |
Marc Kupietz | 657d8e7 | 2020-02-25 18:31:50 +0100 | [diff] [blame] | 17 | #' frequencyQuery(query = alternatives, |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 18 | #' vc = paste("textType = /Zeit.*/ & pubDate in", year), |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 19 | #' as.alternatives = TRUE) %>% |
| 20 | #' hc_freq_by_year_ci(as.alternatives = TRUE) |
| 21 | #' |
Marc Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 22 | #' \donttest{ |
Marc Kupietz | 10f65c4 | 2020-01-31 15:18:24 +0100 | [diff] [blame] | 23 | #' 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 Kupietz | 05b2277 | 2020-02-18 21:58:42 +0100 | [diff] [blame] | 34 | #' } |
Marc Kupietz | 10f65c4 | 2020-01-31 15:18:24 +0100 | [diff] [blame] | 35 | #' |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 36 | hc_freq_by_year_ci <- function(df, as.alternatives = FALSE, ylabel = if(as.alternatives) "%" else "ipm") { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 37 | title <- "" |
| 38 | df <- df %>% |
| 39 | { if(! as.alternatives) ipm(.) else RKorAPClient::percent(.) } |
| 40 | |
| 41 | if (!"year" %in% colnames(df)) { |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 42 | df <- df %>% mutate(year = as.integer(queryStringToLabel(df$vc, pubDateOnly = TRUE))) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 43 | } |
| 44 | if (!"condition" %in% colnames(df)) { |
| 45 | if (length(base::unique(df$query)) > 1) { |
| 46 | df <- df %>% mutate(condition = query) |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 47 | if(length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 48 | df <- df %>% mutate(condition = paste(condition, " & ", |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 49 | queryStringToLabel(vc, excludePubDate = TRUE ))) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 50 | } |
| 51 | } else { |
Marc Kupietz | 5d70ffe | 2020-03-12 11:16:43 +0100 | [diff] [blame] | 52 | if (length(base::unique(queryStringToLabel(df$vc, excludePubDate = TRUE ))) > 1) { |
| 53 | title <- base::unique(df$query) |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 54 | df <- df %>% mutate(condition = queryStringToLabel(vc, excludePubDate = TRUE )) |
Marc Kupietz | 5d70ffe | 2020-03-12 11:16:43 +0100 | [diff] [blame] | 55 | } else { |
| 56 | df <- df %>% mutate(condition = query) |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | } |
| 60 | # use the D3 palette which provides 20 attractive and distinguishable colours |
| 61 | palette <- c("#1F77B4", "#FF7F0E", "#2CA02C", "#D62728", "#9467BD", "#8C564B", "#E377C2", "#7F7F7F", "#BCBD22", "#17BECF", "#AEC7E8", "#FFBB78", "#98DF8A", "#FF9896", "#C5B0D5", "#C49C94", "#F7B6D2", "#C7C7C7", "#DBDB8D", "#9EDAE5") |
| 62 | highcharter::highchart() %>% |
| 63 | hc_title(text=title) %>% |
| 64 | hc_chart(zoomType="xy") %>% |
| 65 | hc_yAxis( |
| 66 | title = list(text = if (as.alternatives) "" else ylabel), |
| 67 | ceiling = if (as.alternatives) 100 else NULL, |
| 68 | floor = 0, |
| 69 | labels = if(as.alternatives) list(format = paste0("{value}\U2009", ylabel)) else NULL |
| 70 | ) %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 71 | hc_xAxis(allowDecimals=FALSE) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 72 | hc_add_theme(hc_theme_google(colors=palette)) %>% |
| 73 | hc_plotOptions( |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 74 | series = list(enabled = TRUE), |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 75 | line = list(cursor = 'pointer', point = list(events = list( |
| 76 | click = JS("function() { window.open(this.click, 'korap'); }") |
| 77 | )))) %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 78 | hc_credits(enabled = TRUE, |
Marc Kupietz | 900d052 | 2020-03-19 13:36:45 +0100 | [diff] [blame^] | 79 | text = "KorAP R Client Package", |
| 80 | href = "https://github.com/KorAP/RKorAPClient/") %>% |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 81 | hc_exporting(enabled = TRUE) %>% |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 82 | hc_tooltip( |
| 83 | formatter = JS(paste0("function (tooltip) { |
| 84 | var str = tooltip.defaultFormatter.call(this, tooltip); |
| 85 | if(Array.isArray(str)) { |
| 86 | str = str.join(''); |
| 87 | } |
| 88 | for (var i = 0; i < this.points.length; i++) { |
| 89 | str = str.replace(/([0-9.,]+.?)", ylabel, "/, this.points[i].point.count+' ($1@)'); |
| 90 | } |
| 91 | return str.replace(/@/g, '", ylabel, "') |
| 92 | } ")), |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 93 | crosshairs = TRUE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 94 | valueDecimals = 2, |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 95 | shared = TRUE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 96 | valueSuffix = paste0('\U2009', ylabel) |
| 97 | ) %>% |
| 98 | hc_add_series_korap_frequencies(df, as.alternatives) |
| 99 | } |
| 100 | |
Marc Kupietz | a4f3653 | 2020-02-03 22:50:08 +0100 | [diff] [blame] | 101 | ## Mute notes: "no visible binding for global variable:" |
| 102 | globalVariables(c("value", "query", "condition", "vc")) |
| 103 | |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 104 | hc_add_series_korap_frequencies <- function(hc, df, as.alternatives = FALSE) { |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 105 | index <- 0 |
| 106 | for(q in unique(df$condition)) { |
| 107 | dat <- df[df$condition==q,] |
| 108 | hc <- hc %>% hc_add_series( |
| 109 | marker = list(radius = 2), |
| 110 | name = q, |
| 111 | data = data.frame( |
| 112 | year = dat$year, |
| 113 | value = if (as.alternatives) dat$f else dat$ipm, |
| 114 | count = dat$totalResults, |
| 115 | click = dat$webUIRequestUrl |
| 116 | ), |
| 117 | hcaes(year, value), |
| 118 | type = 'line', |
| 119 | colorIndex = index, |
| 120 | zIndex = 1 |
| 121 | ) %>% |
| 122 | hc_add_series( |
| 123 | name = "ci", |
| 124 | data = dat[,c('year', 'conf.low', 'conf.high')], |
| 125 | hcaes(x = year, low = conf.low, high = conf.high), |
| 126 | type = 'arearange', |
| 127 | fillOpacity = 0.3, |
| 128 | lineWidth = 0, |
Marc Kupietz | cf1771d | 2020-03-04 16:03:04 +0100 | [diff] [blame] | 129 | marker = list(enabled = FALSE), |
| 130 | enableMouseTracking = FALSE, |
Marc Kupietz | 91145b0 | 2020-01-29 15:58:36 +0100 | [diff] [blame] | 131 | linkedTo= ':previous', |
| 132 | colorIndex = index, |
| 133 | zIndex = 0 |
| 134 | ) |
| 135 | index <- index+1 |
| 136 | } |
| 137 | hc |
| 138 | } |
Marc Kupietz | 882f08c | 2020-03-18 13:30:31 +0100 | [diff] [blame] | 139 | |
| 140 | .onAttach <- function(libname = find.package("RKorAPClient"), |
| 141 | pkgname = "RKorAPClient") { |
| 142 | packageStartupMessage( |
| 143 | "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." |
| 144 | ) |
| 145 | } |
| 146 | |