Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created February 2, 2019 08:01
Show Gist options
  • Save djnavarro/7484e6934a72bbf79ed537262009be44 to your computer and use it in GitHub Desktop.
Save djnavarro/7484e6934a72bbf79ed537262009be44 to your computer and use it in GitHub Desktop.
library(isoband)
library(magick)
library(sf)
library(tidyverse)
# Adapted from Claus Wilke's code
# https://github.com/clauswilke/isoband
sf_from_image <- function(image, nbands) {
image_gray <- image %>% image_quantize(colorspace = "gray")
image_raster <- as.raster(image_gray)
d <- dim(image_raster)
m <- matrix(c((255-col2rgb(image_raster)[1,])),
nrow = d[1], ncol = d[2], byrow = TRUE)
b <- isobands(1:d[2], d[1]:1, m, 20*(0:(nbands-1)), 20*(1:nbands))
bands <- iso_to_sfg(b)
data <- st_sf(
level = letters[1:length(bands)],
geometry = st_sfc(bands)
)
}
# create an sf object with isoband
image_sf <- "danielle.jpg" %>%
image_read() %>%
image_resize(geometry = "200x200") %>%
sf_from_image(nbands = 7)
# specify a rainbow palette
lgbt <- c(
`a` = "#FFEF00", # Canary Yellow
`b` = "#FF8C00", # Dark Orange
`c` = "#E70000", # Electric Red
`d` = "#00811F", # La Salle Green
`e` = "#0044FF", # Blue (RYB)
`f` = "#760089", # Patriarch
`g` = "#333333" # Grey (highlight)
)
# draw image
pic <- image_sf %>%
ggplot(aes(fill = level, color = level)) +
geom_sf(size = 0.1, show.legend = FALSE) +
scale_color_manual(values = lgbt) +
scale_fill_manual(values = lgbt) +
coord_sf(expand = FALSE) +
theme_void() +
theme(panel.background = element_rect(fill="black"))
plot(pic)
ggsave("C:/Users/Dan/Desktop/danielle_lgbt.png", width = 5, height = 5)
@djnavarro
Copy link
Author

danielle
danielle_lgbt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment