Add convenience function geom_freq_by_year_ci
Change-Id: Id4f9c5ead478cb2dcb7d7a48580b501bd5e0c752
diff --git a/NAMESPACE b/NAMESPACE
index bb3fd5f..0e11e6f 100644
--- a/NAMESPACE
+++ b/NAMESPACE
@@ -44,6 +44,12 @@
importFrom(dplyr,select)
importFrom(dplyr,starts_with)
importFrom(dplyr,summarise)
+importFrom(ggplot2,element_text)
+importFrom(ggplot2,geom_line)
+importFrom(ggplot2,geom_point)
+importFrom(ggplot2,geom_ribbon)
+importFrom(ggplot2,scale_x_continuous)
+importFrom(ggplot2,theme)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,year)
importFrom(magrittr,"%>%")
diff --git a/R/misc.R b/R/misc.R
index 50b8821..9a97f22 100644
--- a/R/misc.R
+++ b/R/misc.R
@@ -22,3 +22,30 @@
}
+#' Plot frequency by year graphs with confidence intervals
+#'
+#' Convenience function for plotting typical frequency by year graphs with confidence intervals using ggplot2.
+#'
+#' @examples
+#' library(ggplot2)
+#' kco <- new("KorAPConnection", verbose=TRUE)
+#' expand_grid(condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
+#' year = (2002:2018)) %>%
+#' cbind(frequencyQuery(kco, "[tt/l=Heuschrecke]",
+#' paste0(.$condition," & pubDate in ", .$year))) %>%
+#' ipm() %>%
+#' ggplot(aes(year, ipm, fill = condition, color = condition, ymin = conf.low, ymax = conf.high)) +
+#' geom_freq_by_year_ci()
+#'
+#' @importFrom ggplot2 geom_ribbon geom_line geom_point theme element_text scale_x_continuous
+#'
+#' @export
+geom_freq_by_year_ci <- function() {
+ list(
+ geom_ribbon(alpha = .3, linetype = 0, show.legend = FALSE),
+ geom_line(),
+ geom_point(),
+ theme(axis.text.x = element_text(angle = 45, hjust = 1)),
+ scale_x_continuous(breaks = function(x) seq(ceiling(x[1]), floor(x[2]), by = 1 + floor(((x[2]-x[1])/30)))))
+}
+
diff --git a/man/geom_freq_by_year_ci.Rd b/man/geom_freq_by_year_ci.Rd
new file mode 100644
index 0000000..ffdbdb3
--- /dev/null
+++ b/man/geom_freq_by_year_ci.Rd
@@ -0,0 +1,23 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/misc.R
+\name{geom_freq_by_year_ci}
+\alias{geom_freq_by_year_ci}
+\title{Plot frequency by year graphs with confidence intervals}
+\usage{
+geom_freq_by_year_ci()
+}
+\description{
+Convenience function for plotting typical frequency by year graphs with confidence intervals using ggplot2.
+}
+\examples{
+library(ggplot2)
+kco <- new("KorAPConnection", verbose=TRUE)
+expand_grid(condition = c("textDomain = /Wirtschaft.*/", "textDomain != /Wirtschaft.*/"),
+ year = (2002:2018)) \%>\%
+ cbind(frequencyQuery(kco, "[tt/l=Heuschrecke]",
+ paste0(.$condition," & pubDate in ", .$year))) \%>\%
+ ipm() \%>\%
+ ggplot(aes(year, ipm, fill = condition, color = condition, ymin = conf.low, ymax = conf.high)) +
+ geom_freq_by_year_ci()
+
+}