Created
May 7, 2019 10:48
-
-
Save coolbutuseless/70fa91fce66c875a9bf813411361d40a to your computer and use it in GitHub Desktop.
geom_streamline for some image manipulation
This file contains hidden or 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
suppressPackageStartupMessages({ | |
library(dplyr) | |
library(ggplot2) | |
library(metR) | |
library(tidyr) | |
}) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Load a black and white Mona Lisa. | |
# Flip the intensity | |
# Flip the y axis | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
img <- png::readPNG('mona.png') | |
img <- 1 - img[nrow(img):1,] | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# Convert to a data.frame for ggplot | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
img_df <- as.data.frame(img) %>% | |
gather(x, valuex) %>% | |
mutate( | |
x = readr::parse_number(x), | |
y = rep(seq(nrow(img)), ncol(img)) | |
) | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# metR::geom_streamline FTW | |
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
ggplot(img_df, aes(x, y, dx = valuex, dy = valuex)) + | |
geom_streamline(L = 8, min.L = 0.5, res = 1, | |
arrow.length = 0.1, alpha = 0.6) + | |
coord_equal() + | |
theme_void() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment