Created
June 17, 2019 17:47
-
-
Save erichare/e2081bbbbaaebe5c8fc6200798b786c2 to your computer and use it in GitHub Desktop.
Drawing a Droste Effect Polygon with ggplot2 3.2.0
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(tidyverse) | |
polygon_values <- tibble( | |
id = 1:2, | |
value = c(-2, 2) | |
) | |
polygon_positions <- tibble( | |
id = c(1, 1, 1, 2, 2, 2, 2), | |
x = c(1, 2, 3, 5, 6, 7, 6), | |
y = c(1, 2, 1, 1.5, 2, 1.5, 1) | |
) | |
datapoly <- polygon_values %>% | |
left_join(polygon_positions) | |
datapoly$subid <- 1 | |
# create sub holes within polygons | |
for(i in 1:3) { | |
holes <- datapoly %>% | |
filter(subid == i) %>% | |
group_by(id, value, subid) %>% | |
summarise(x = list(x + 0.5 * (mean(x) - x)), | |
y = list(y + 0.5 * (mean(y) - y))) %>% | |
unnest() %>% | |
ungroup() | |
holes$subid <- i + 1 | |
datapoly <- rbind(datapoly, holes) | |
} | |
p <- ggplot(datapoly, aes(x = x, y = y)) + | |
geom_polygon(aes(fill = value, group = id, subgroup = subid), | |
color = "black", show.legend = FALSE) + | |
scale_fill_continuous() + | |
theme_void() | |
p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment