Skip to content

Instantly share code, notes, and snippets.

@uribo
Created March 5, 2020 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uribo/c02eb120a5933e5ad2bb2039a126275a to your computer and use it in GitHub Desktop.
Save uribo/c02eb120a5933e5ad2bb2039a126275a to your computer and use it in GitHub Desktop.
imagerで異なる画像を読み込んでggplot2::facet_wrap()で表示する
library(imager)
library(ggplot2)
image_to_rgbdf <- function(file) {
imager::load.image(file) %>%
as.data.frame(wide = "c") %>%
dplyr::mutate(rgb_val = rgb(c.1, c.2, c.3))
}
df_plot <-
# 画像ファイルが含まれるパスを指定して、purrrで一括処理(読み込み、df化するとことまで)
fs::dir_ls("path_to_image") %>%
purrr::set_names(basename(.)) %>%
purrr::map_dfr(
~ image_to_rgbdf(.x),
.id = "file")
df_plot %>%
ggplot(aes(x, -y)) +
geom_raster(aes(fill = rgb_val)) +
scale_fill_identity() +
facet_wrap(~ file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment