If you always want a logo on your plots, consider adding it to the settings to your R Markdown reports.

Let’s look into two ways to add a logo to a single plot.

The first way to add a logo to a picture quickly is via annotations:

library(ggplot2)
library(png)
library(grid) # to put an image at a given location
library(gridExtra) # functions to work with pictures

gg <- ggplot(mtcars, aes(x = mpg, y = wt)) + 
  theme_minimal() +
  geom_count() + 
  labs(title = "Title Goes Here", x = "", y = "")

img2 <- readPNG("logo.png")
gg <- gg + 
  annotation_custom(rasterGrob(img2), 
                    xmin=0.95*min(mtcars$mpg)-1, xmax=0.95*min(mtcars$mpg)+1, 
                    ymin=0.3*min(mtcars$wt)-0.5, ymax=0.62*min(mtcars$wt)+0.5) +
  labs(caption = "Footer goes here") +
  theme(plot.margin=margin(5,5,20,5),
        plot.caption=element_text(colour="blue", hjust=1.05, size=15)) 
# Turn off clipping the image 
gt <- ggplot_gtable(ggplot_build(gg))
gt$layout$clip[gt$layout$name=="panel"] <- "off"

# If you want to save the plot as pdf, run lines 36 to open the file and 38 to save it.
#pdf(file = "plot_logo.pdf", width = 4, height = 5)
grid.draw(gt)

#dev.off()

In addition to a logo, it is possible to add notes and accents such as rectangles or points on top of a graph.

gg + 
  annotate("text", x = 30, y = 5, label = "This is \n not a logo but still \n useful") +
  annotate("rect", xmin = 9.9, xmax = 16, ymin = 5, ymax = 5.5, alpha = .1, col = "red")

The second way to add a logo to a plot is by using the magick package (for image processing).

Let’s see how magick works (see https://cran.r-project.org/web/packages/magick/vignettes/intro.html for the full introduction):

library(magick)

# Foreground image - get a moving banana
banana <- image_read("https://jeroen.github.io/images/banana.gif")
banana <- image_scale(banana, "150")
#image_info(banana)

# Background image
logo <- image_read("https://jeroen.github.io/images/Rlogo.png")
background <- image_background(image_scale(logo, "200"), "white", flatten = TRUE)

# Combine and flatten frames
frames <- image_composite(background, banana, offset = "+70+30")

# Turn frames into animation
animation <- image_animate(frames, fps = 10, optimize = TRUE)
print(animation)
## # A tibble: 8 x 7
##   format width height colorspace matte filesize density
##   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
## 1 gif      200    155 sRGB       TRUE         0 72x72  
## 2 gif       94    105 sRGB       TRUE         0 72x72  
## 3 gif      125    122 sRGB       TRUE         0 72x72  
## 4 gif      108    118 sRGB       TRUE         0 72x72  
## 5 gif      108    105 sRGB       TRUE         0 72x72  
## 6 gif       92    105 sRGB       TRUE         0 72x72  
## 7 gif      113    123 sRGB       TRUE         0 72x72  
## 8 gif      119    118 sRGB       TRUE         0 72x72

#image_write(animation, "Rlogo-banana.gif") # to save the picture

Now let’s add a logo to a plot: (see the source code here https://stackoverflow.com/questions/41574732/how-to-add-logo-on-ggplot2-footer)

Make a plot first, then add a logo to it:

library(ggplot2)
library(magick)
library(here) # For making the script run in your local working directory
library(magrittr) # For piping

# Make a simple plot and save it
ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() + 
  ggtitle("Cars") +
  ggsave(filename = paste0(here("_"), last_plot()$labels$title, ".png"), # if / does not work, use _ in path
         width = 5, height = 4, dpi = 300, units = 'in') # specify units here

# Call back the plot
plot <- image_read(paste0(here("_"), "Cars.png"))

# And bring in a logo
logo_raw <- image_read("http://hexb.in/hexagons/ggplot2.png") 

# Scale down the logo and give it a border and annotation
# (you can do a lot to the image/logo before adding it)
logo <- logo_raw %>%
  image_scale("100") %>% # width up to 100 pixels
  image_background("grey", flatten = TRUE) %>% # Flattening combines the layers into a single image which has the size of the first image
  image_border("grey", "600x10") %>%
  image_annotate("Powered By R", color = "white", size = 30, 
                 location = "+10+50", # these are x and y coordinates
                 gravity = "northeast") # for details on gravity, see https://www.imagemagick.org/Magick++/Enumerations.html#GravityType

# Stack images on top of each other
final_plot <- image_append(image_scale(c(plot, logo), "900"), 
                           stack = TRUE)
final_plot

# And overwrite the plot without a logo
# image_write(final_plot, paste0(here("_"), last_plot()$labels$title, ".png")) # run this line to save the last plot

Explore how magick can combine the pictures on the go:

img2 <- image_read("logo.png")
img <- image_read(system.file("img", "Rlogo.png", package="png"))
img3 <- c(img2, img)
#img <- image_scale(img, "100x100")
#image_info(img3)

logo <- img3 %>%
  image_flatten(., "Add") %>% 
  image_scale("100x100") %>% 
  image_background("grey", flatten = TRUE) %>%
  image_border("grey", "600x10") %>%
  image_annotate("Powered By R", color = "white", size = 30, 
                 location = "+10+50", gravity = "southeast") 
final_plot <- image_append(image_scale(c(plot, logo), "900"), stack = TRUE)
final_plot