## plot_examples.R | gsgb
## SGB | 10.06.2020
## ---------------------------
#' Beispiele SGB-Grafiken
#'
#' Hier werden verschiedene Beispiele fuer typische SGB-Grafiken unter Verwendung des SGB-Themes
#' und der SGB Farben beschrieben. Zudem zeigt es auch, wie Grafiken als png, emf (vektorisierte
#' Grafiken fuer Word) oder als pdf gespeichert werden koennen.
#'
#' @seealso
#' \code{\link{scale_color_discrete}} und \code{\link{scale_fill_discrete}} fuer diskrete Variablen.
#' \code{\link{scale_color_gradient}} und \code{\link{scale_fill_gradient}} fuer kontinuerliche Variablen.
#' \code{\link{usecol}} um komplexere Paletten zu mischen.
#' \code{\link{seecol}} um alle SGB-Paletten und -Farben zu anzusehen.
#'
#' @examples
#'
#' \dontrun{
#' library(ggplot2)
#' library(gsgb)
#' theme_set(theme_sgb(base_size = 9))
#'
#' ## Linien ------------------------------------------------
#' # z.B. Zeitreihen
#' ggplot(kof, aes(x = time, y = value, colour = branche)) +
#' geom_line(size = 1) +
#' geom_hline(yintercept = 0, size = 0.5) + # dickere Linie bei 0
#' labs(x = "", y = "")
#'
#' # Farben & Zeitvariable anpassen
#' ggplot(mangelindikator, aes(x = time, y = value, colour = label)) +
#' geom_line(size = 1) +
#' scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
#' scale_color_manual(values = rev(usecol(pal_sgb_dunkelblau))) +
#' labs(x = "", y = "Mangelindikator")
#'
#'
#' ## Saeulen ------------------------------------------------
#' # geom_bar: y-Achse bildet Anzahl der Beobachtungen ab
#' ggplot(mpg, aes(as.factor(year), fill = drv)) +
#' geom_bar(position = "dodge")
#'
#' # Farben anpassen
#' ggplot(mpg, aes(as.factor(year), fill = drv)) +
#' geom_bar(position = "dodge") +
#' scale_fill_manual(values = usecol(c(SGBrot, SGBgelb, pal_sgb_dunkelblau[4])))
#'
#' # geom_col: y-Achse ist eine Variable aus den Daten
#' ggplot(dplyr::filter(mangelindikator, time == "2020-01-01" | time == "2004-01-01")) +
#' geom_col(aes(x = as.factor(time), y = value, fill = label), position = "dodge") +
#' labs(x = "", y = "Mangelindikator")
#'
#'
#' ## Punkte ------------------------------------------------
#' # Diskrete Farbvariable (hier Species)
#' ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
#' geom_point()
#'
#' # Kontinuierliche Farbvariable (hier Sepal.Length)
#' ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Sepal.Length)) +
#' geom_point()
#'
#'
#' ## Raster ------------------------------------------------
#' # Diskrete Farbvariable
#' df <- data.frame(
#' x = rep(c(2, 5, 7, 9, 12), 2),
#' y = rep(c(1, 2), each = 5),
#' z = factor(rep(1:5, each = 2)))
#' ggplot(df, aes(x, y)) +
#' geom_tile(aes(fill = z)) +
#' scale_fill_manual(values = usecol(pal_sgb))
#'
#' # Kontinuierliche Farbvariable
#' ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
#' geom_raster()
#'
#'
#' ## Kuchen-Diagramm ------------------------------------------------
#' library(magrittr)
#' df <- data.frame(group = c("Oberstes 1%", "Uebrige 9%", "Unterste 90%"),
#' value = c(42, 33, 25))
#' df <- df %>%
#' dplyr::arrange(dplyr::desc(group)) %>%
#' dplyr::mutate(prop = value / sum(df$value) * 100) %>%
#' dplyr::mutate(ypos = cumsum(prop) - 0.5 * prop)
#'
#' ggplot(df, aes(x = "", y = prop, fill = group)) +
#' geom_bar(stat = "identity",
#' width = 1,
#' color = "white") +
#' coord_polar("y", start = 0) +
#' geom_text(aes(y = ypos, label = scales::percent(prop / 100)),
#' color = "white",
#' size = 5) +
#' theme_sgb_blank() + # minimales Theme ohne Achsen und andere Beschriftungen
#' theme(
#' legend.position = "right",
#' legend.direction = "vertical",
#' legend.justification = c("right", "center"))
#'
#'
#' ## Als png speichern ------------------------------------------------
#' ggplot(kof) +
#' geom_line(aes(x = time, y = value, colour = branche), size = 1) +
#' geom_hline(yintercept = 0, size = 0.5) +
#' labs(x = "", y = "")
#' ggsave("docs/lineplot.jpeg", wide = TRUE)
#'
#'
#' ## Als EMF speichern ------------------------------------------------
#' # (emf ist ein Vektorformat, das von Word akzeptiert wird)
#' devEMF::emf(file = "docs/lineplot2.emf", width = 6.2, height = 2.75) # width/height in inches
#' ggplot(mangelindikator, aes(x = time, y = value, colour = label)) +
#' geom_line(size = 1) +
#' scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
#' labs(x = "", y = "Mangelindikator")
#' dev.off()
#'
#'
#' ## Als PDF speichern --------------------------------------------
#' # Eine Seite
#' pdf("docs/barplot.pdf", height = 20/2.54, width = 27/2.54)
#' ggplot(mpg, aes(as.factor(year), fill = drv)) +
#' geom_bar(position = "dodge") +
#' labs(x = "", y = "")
#' dev.off()
#'
#' # Mehrere Seiten
#' pdf("docs/barplot_mult.pdf", onefile = TRUE,
#' height = 20/2.54, width = 27/2.54,
#' paper = "a4r")
#' ggplot(mpg, aes(as.factor(year), fill = drv)) +
#' geom_bar(position = "dodge") +
#' labs(x = "", y = "")
#' ggplot(kof) +
#' geom_line(aes(x = time, y = value, colour = branche), size = 1) +
#' geom_hline(yintercept = 0, size = 0.5) +
#' labs(x = "", y = "")
#' dev.off()
#'
#' }
#'
#' @family plot functions
#'
#' @name ggexample
#' @import ggplot2
#'
NULL
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.