Skip to content

Instantly share code, notes, and snippets.

@coolbutuseless
Created May 7, 2019 10:48
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 coolbutuseless/70fa91fce66c875a9bf813411361d40a to your computer and use it in GitHub Desktop.
Save coolbutuseless/70fa91fce66c875a9bf813411361d40a to your computer and use it in GitHub Desktop.
geom_streamline for some image manipulation
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