reduce_image_size: Reduce the size of an image

View source: R/display.R

reduce_image_sizeR Documentation

Reduce the size of an image

Description

This function reduces the size of an image, archiving the original image in a separate archive sub-directory.

Usage

reduce_image_size(path_image, image_size = "50%")

Arguments

path_image

The character string of the image path name.

image_size

Percentage size reduction with 50% as the default.

Details

An internal shadow may be added when the function is called for the first time. Calling the function a second time then adds a border shadow.

Value

The path name of image returned invisibly so that the function can be used in a piped workflow.

Figures

penguins_mass_flipper_plot_reduced.png

See Also

Jumping rivers has written a series of four blogs for displaying images on web pages.

Other image manipulation functions: add_image_shadow()

Examples

# example taken from palmerpenguins example analysis of mass vs. flipper length
# https://allisonhorst.github.io/palmerpenguins/articles/examples.html

suppressPackageStartupMessages({
  suppressWarnings({
    library(palmerpenguins)
    library(fs)
    library(dplyr)
    library(ggplot2)
    library(ragg)
  })
})

# create temp directory
dir_create(path(tempdir(), "figures"))

# data
penguins_mass_flipper <- penguins %>%
  select(species, flipper_length_mm, body_mass_g)

# graph
penguins_mass_flipper_plot <- ggplot(data = penguins_mass_flipper,
                                     aes(x = flipper_length_mm,
                                         y = body_mass_g)) +
  geom_point(aes(color = species,
                 shape = species),
             size = 3,
             alpha = 0.8) +
  theme_minimal() +
  scale_color_manual(values = c("darkorange","purple","cyan4")) +
  labs(title = "Penguin size, Palmer Station LTER",
       subtitle = "Flipper length and body mass for Adelie, Chinstrap and Gentoo Penguins",
       x = "Flipper length (mm)",
       y = "Body mass (g)",
       color = "Penguin species",
       shape = "Penguin species") +
  theme(legend.position = c(0.2, 0.7),
        legend.background = element_rect(fill = "white", color = NA),
        plot.title.position = "plot",
        plot.caption = element_text(hjust = 0, face= "italic"),
        plot.caption.position = "plot")

# save graph
suppressWarnings({
  ggsave(filename = path(tempdir(), "figures", "penguins_mass_flipper_plot.png"),
       plot = penguins_mass_flipper_plot,
       device = agg_png,
       width = 6,
       height = 6,
       units = "in",
       dpi = 72)
})

# batch reduce images
suppressPackageStartupMessages({
  suppressWarnings({
    library(fs)
    library(future)
    library(furrr)
  })
})

plan(multisession)
path(tempdir(), "figures") %>% {
  suppressMessages({dir_ls(., glob = "*.png")})} %>%
  future_walk(reduce_image_size,
              image_size = "50%",
              .options = furrr_options(seed = TRUE),
              .progress = TRUE)

# move figures from temporary directory
suppressPackageStartupMessages({
   suppressWarnings({
      library(fs)
      library(here)
   })
})

if(dir_exists(here("man", "figures"))) {
   file_move(path(tempdir(), "figures", "penguins_mass_flipper_plot.png"),
             here("man", "figures", "penguins_mass_flipper_plot_reduced.png"))
}

gcfrench/store documentation built on May 17, 2024, 5:52 p.m.