Skip to content

Instantly share code, notes, and snippets.

@cwickham
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save cwickham/f4937624a882a7f3cc4e to your computer and use it in GitHub Desktop.

Select an option

Save cwickham/f4937624a882a7f3cc4e to your computer and use it in GitHub Desktop.
Happy 4th of July!
library(plyr)
library(ggplot2)
# from: http://www.codeproject.com/Articles/18149/Draw-a-US-Flag-using-C-and-GDI
star_coords <- function(x, y, r, r1){
a <- 72 * pi /180
b <- a/2
df <- data.frame(
x = x + c(0, r1*sin(b), r*cos(pi/2 - a), r1*cos(a + b - pi/2), r*sin(b),
0, -r*sin(b), -r1*cos(a + b - pi/2), - r*cos(pi/2 - a), -r1*sin(b)),
y = y + c(r, r1*cos(b), r*sin(pi/2 - a), -r1*sin(a + b - pi/2), -r*cos(b),
-r1, -r*cos(b), -r1*sin(a + b - pi/2), r*sin(pi/2 - a), r1*cos(b)))
}
stars_cents <- expand.grid(
x = seq(0.063, 0.76 - 0.063, 0.063),
y = 1 - seq(0.054, 7/13, 0.054))[c(TRUE, FALSE), ]
stars <- mdply(stars_cents, star_coords, r = 0.0616/2, r1 = 0.38*0.0616/2)
stars$group <- rep(1:nrow(stars_cents), each = 10)
stripes <- data.frame(xmin = 0, xmax = 1.9,
ymin = seq(0, 1-1/13, 2/13), ymax = seq(1/13, 1, 2/13),
fill = "#B22234")
union <- data.frame(xmin = 0, xmax = 0.76,
ymin = 1 - 7/13, ymax = 1, fill = "#3C3B6E")
print(ggplot(mapping = aes(xmin = xmin, xmax = xmax,
ymin = ymin, ymax = ymax, fill = fill)) +
geom_rect(data = stripes) +
geom_rect(data = union) +
geom_polygon(aes(x = x, y = y, group = group), data = stars, fill = "white",
inherit.aes = FALSE) +
scale_fill_identity() +
scale_shape_identity() +
scale_y_continuous("", limits = c(0,1), expand = c(0,0)) +
scale_x_continuous("", limits = c(0,1.9), expand = c(0,0)) +
coord_equal() +
theme(panel.background = element_rect(fill = "white"),
axis.text = element_blank(),
axis.ticks = element_blank()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment