R/plot_my_legend.R

Defines functions plot_my_data .get_data

Documented in plot_my_data

#' Plot with customized labels in the legend
#'
#' The function accepts a vector with two labels for the legend. We want
#' to try to use special characters (subscripts, ...) with `expression`
#'
#' @details The function creates data with the three columns `x`, `y` and `group`
#'
#' @param legend_labels The labels for the two groups as a vector of lenght two
#' @param n (10) Number of data points per group
#'
#' @return A `ggplot2`-object to print
#'
#' @import ggplot2
#' @importFrom stats rnorm
#'
#' @examples
#' lab <- c("A", "B")
#' p <- exprLegend::plot_my_data(legend_labels = lab)
#' print(p)
#'
#' @export
#'
plot_my_data <- function(legend_labels, n = 10) {

    if (length(legend_labels) != 2) stop("We expect 2 labels.")

    dat <- .get_data(n)

    p <- ggplot(dat, aes(x, y)) +
            geom_point(aes(color = group), size = 3) +
            scale_color_manual(values = c("goldenrod", "seagreen"),
                           labels = legend_labels) +
            theme(legend.text.align = 0,
                  legend.text = element_text(size = 12))
    p
}



#' create data
#'
#' @param n Number of rows/datapoints
#'
.get_data <- function(n) {
    data.frame(x = rep(1:n, 2),
               y = c(rnorm(n, 10, 2), rnorm(n, 5, 2)),
               group = rep(c("A", "B"), each = n)
    )
}
tinu-schneider/exprLegend documentation built on May 7, 2019, 9:34 a.m.