#' Title #' #' @param path #' @param add #' @param upscale #' #' @return #' @export #' @importFrom jpeg readJPEG #' @importFrom graphics rasterImage #' @examples #' img <- readJPEG(system.file("img","Rlogo.jpg",package="jpeg")) plot_jpeg <- function(path, add=FALSE, upscale = TRUE) { require('jpeg') jpg = readJPEG(path, native=T) # read the file res = dim(jpg)[2:1] # get the resolution, [x, y] if (upscale){ res <- res * 3 } if (!add) # initialize an empty plot area if add==FALSE plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',yaxs='i', xaxt='n',yaxt='n',xlab='',ylab='',bty='n', oma=c(0,0,1,1), mar=c(0,0,1,1) ) rasterImage(jpg,1,1,res[1],res[2],interpolate=TRUE) } #' Title #' #' @param filename #' #' @return #' @export #' @importFrom png readPNG #' @importFrom grid rasterGrob #' #' @examples get_png <- function(filename) { grid::rasterGrob(png::readPNG(filename), interpolate = TRUE) } #' Title #' #' @param plot #' @param logo #' @param margin_param #' #' @return #' @importFrom ggplot2 theme #' @importFrom gridExtra arrangeGrob #' @export #' #' @examples add_logo <- function(plot, logo, margin_param = NULL) { if(has_caption(plot)) { # convert the caption size to a negative number and on the "pt" scale # p1$theme$plot.caption$size * 1.1 # Count the number of lines, which is this + 1 # For each line, we can add a certain negative space to align the logo cap_lines <- measure_caption(plot) if (!is.null(margin_param)) { plot <- plot + theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt")) } else { plot <- plot + theme(plot.margin = unit(c(7, 7, cap_lines * -52, 7), "pt")) } } else { plot <- plot + theme(plot.margin = unit(c(7, 7, -14, 7), "pt")) } arrangeGrob(plot, logo, heights = c(0.93, 0.1), padding = unit(0.1, "line")) } #' Title #' #' @param gg #' #' @return #' @importFrom stringr str_count #' @export #' #' @examples measure_caption <- function(gg) { if (has_caption(gg)) { stringr::str_count(gg$labels$caption, pattern = "\n") + 1 } else { 1 } } #' Title #' #' @param gg #' #' @return #' @export #' #' @examples has_caption <- function(gg) { any(names(gg$labels) == "caption") } #' Title #' #' @param plot_list #' @param logo #' @param nrow #' @param widths #' @param margin_param #' #' @return #' @importFrom gridExtra arrangeGrob #' @importFrom ggplot2 theme #' @export #' #' @examples add_logo_ga <- function(plot_list, logo, nrow = 1, widths = NULL, margin_param = NULL) { # Change position of logo depending on if plot has a caption if (!is.null(margin_param)) { margin <- theme(plot.margin = unit(c(7, 7, margin_param, 7), "pt")) } else if (any(unlist(lapply(plot_list, has_caption)))) { cap_lines <- measure_caption(plot_list[[1]]) # measure caption in first plot margin <- theme(plot.margin = unit(c(7, 7, -7 * sqrt(cap_lines), 7), "pt")) } else { margin <- theme(plot.margin = unit(c(7, 7, 7, 7), "pt")) } if (nrow > 1) { len <- length(plot_list) plot_list[[len]] <- plot_list[[len]] + margin } else { plot_list <- lapply(plot_list, "+", margin) } if (nrow != 1) { hold <- arrangeGrob(grobs = plot_list, nrow = nrow, ncol = 1, widths = widths) } else { hold <- arrangeGrob(grobs = plot_list, nrow = 1, ncol = 2, widths = widths) } arrangeGrob(hold, logo, heights = c(0.93, .07)) } #' Get a Civilytics Logo grob #' #' @return #' @export #' #' @examples make_logo_grob <- function() { logo_grob <- ggplot(mapping = aes(x = 0:1, y = 1)) + theme_void() + annotation_custom(get_png(system.file("img", "civilytics_logo.png", package="civilytics")), xmin = 0.825, xmax = 1) logo_grob }