#' Rebrand a ggplot with logo and optionally localize it for Hebrew (using RTL layout)
#'
#'
#' @param plot The ggplot on which to apply the changes.
#' @param logo_location May be "left", "right", or "none".
#' @param rtl Logical. Should right to left layout be applied on the chart.
#' @param annotation_label A string to annotate the logo.
#' @return An updated ggplot object rebranded to the Sarid look and feel.
#' If the logo option wasn't disabled (by logo_location = NULL) a gridExtra object is returned.
#' Otherwise, a ggplot2 object is returned.
#'
#' @examples
#' base_plot <- ggplot(mtcars, aes(x = disp, y = mpg, color = factor(cyl))) + geom_point()
#' sarid_brand(base_plot)
#' sarid_brand(base_plot + theme_sarid(), rtl = T)
#' sarid_brand(base_plot + theme_sarid(), logo_location = "left", annotation_label = "Sarid Research. Trusted Insights, Proven Results.")
#' sarid_brand(base_plot + theme_sarid() + facet_wrap(~vs), logo_location = "none")
#' sarid_brand(base_plot + theme_sarid() + facet_wrap(~vs), logo_location = "right", annotation_label = "\u202bמכון שריד. מחקר שמביא תוצאות.")
#'
#' @seealso sarid_theme
#'
#' @export
sarid_brand <- function(plot,
logo_location = c("right", "left", "none"),
rtl = T,
annotation_label = ''){
if (rtl){
return_plot <- plot + ggplot2::theme(plot.title = ggplot2::element_text(hjust = 1),
plot.subtitle = ggplot2::element_text(hjust = 1),
plot.caption = ggplot2::element_text(hjust = 0))
} else {
return_plot <- plot
}
# Create the logo as a ggplot
logo_positions <- tibble::tibble(x = c("a", "b", "c", "c"),
type = c("blue", "blue", "transparent", "blue"),
height = c(4, 8, 4, 4))
logo_plot <- ggplot2::ggplot(logo_positions, ggplot2::aes(x = x, y = height, fill = type)) +
ggplot2::geom_col(color = "white", width = 0.65) +
ggplot2::scale_fill_manual(values = c("transparent" = "#ffffff", "blue" = "#0066ff")) +
ggplot2::theme_void() +
ggplot2::theme(legend.position = "none") +
ggplot2::coord_equal(ratio = 0.35)
blank_plot <- ggplot2::ggplot() + ggplot2::theme_void() +
ggplot2::coord_cartesian(xlim = c(0, 1), ylim = c(0, 1))
# figure out where to put it
if (logo_location[1] == "right"){
# position the annotation of the logo to the right
blank_plot <- blank_plot +
ggplot2::annotate("text", x = 0, y = 0.3, color = "#0066ff",
label = annotation_label, hjust = 0)
logo_right_aligned <- gridExtra::grid.arrange(blank_plot, logo_plot,
ncol = 2, widths = c(0.9, 0.1))
final_alignment <- gridExtra::grid.arrange(return_plot, logo_right_aligned,
nrow = 2, heights = c(0.9, 0.1))
} else if (logo_location[1] == "left") {
# position the annotation of the logo to the left
blank_plot <- blank_plot +
ggplot2::annotate("text", x = 1, y = 0.3, color = "#0066ff",
label = annotation_label, hjust = 1)
logo_left_aligned <- gridExtra::grid.arrange(logo_plot, blank_plot,
ncol = 2, widths = c(0.1, 0.9))
final_alignment <- gridExtra::grid.arrange(return_plot, logo_left_aligned,
nrow = 2, heights = c(0.9, 0.1))
} else {
final_alignment <- return_plot
}
# return final result
return(final_alignment)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.