Created
March 5, 2020 01:21
-
-
Save uribo/c02eb120a5933e5ad2bb2039a126275a to your computer and use it in GitHub Desktop.
imagerで異なる画像を読み込んでggplot2::facet_wrap()で表示する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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