Skip to content

Instantly share code, notes, and snippets.

@jalapic
Last active August 29, 2015 14:17
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 jalapic/b7bf6221e50248f46cc8 to your computer and use it in GitHub Desktop.
Save jalapic/b7bf6221e50248f46cc8 to your computer and use it in GitHub Desktop.
### Playing around with waffle function to change where the blocks start from / finish
wafflex <- function (parts, rows = 10, xlab = NULL, title = NULL, colors = NA,
size = 2, flip = FALSE, reverse = FALSE, equal = TRUE, pad = 0, from = NULL)
{
part_names <- names(parts)
if (length(part_names) < length(parts)) {
part_names <- c(part_names, LETTERS[1:length(parts) -
length(part_names)])
}
if (all(is.na(colors))) {
colors <- brewer.pal(length(parts), "Set2")
}
parts_vec <- unlist(sapply(1:length(parts), function(i) {
rep(LETTERS[i + 1], parts[i])
}))
if (reverse) {
parts_vec <- rev(parts_vec)
}
dat <- expand.grid(y = 1:rows, x = seq_len(pad + (ceiling(sum(parts)/rows))))
dat$value <- c(parts_vec, rep(NA, nrow(dat) - length(parts_vec)))
if (from == "topright") {
dat <- expand.grid(y = rows:1, x = seq_len(pad + (ceiling(sum(parts)/rows))))
dat$value <- c(parts_vec, rep(NA, nrow(dat) - length(parts_vec)))
}
if (from == "topleft") {
dat <- expand.grid(y = 1:rows, x = rev(seq_len(pad + (ceiling(sum(parts)/rows)))))
dat$value <- c(parts_vec, rep(NA, nrow(dat) - length(parts_vec)))
}
if (flip) {
gg <- ggplot(dat, aes(x = y, y = x, fill = value))
}
else {
gg <- ggplot(dat, aes(x = x, y = y, fill = value))
}
gg <- gg + geom_tile(color = "white", size = size)
gg <- gg + labs(x = xlab, y = NULL, title = title)
gg <- gg + scale_x_continuous(expand = c(0, 0))
gg <- gg + scale_y_continuous(expand = c(0, 0))
gg <- gg + scale_fill_manual(name = "", values = colors,
labels = part_names)
gg <- gg + guides(fill = guide_legend(override.aes = list(colour = NULL)))
if (equal) {
gg <- gg + coord_equal()
}
gg <- gg + theme_bw()
gg <- gg + theme(panel.grid = element_blank())
gg <- gg + theme(panel.border = element_blank())
gg <- gg + theme(panel.background = element_blank())
gg <- gg + theme(panel.margin = unit(0, "null"))
gg <- gg + theme(axis.text = element_blank())
gg <- gg + theme(axis.title.x = element_text(size = 10))
gg <- gg + theme(axis.ticks = element_blank())
gg <- gg + theme(axis.line = element_blank())
gg <- gg + theme(axis.ticks.length = unit(0, "null"))
gg <- gg + theme(axis.ticks.margin = unit(0, "null"))
gg <- gg + theme(plot.title = element_text(size = 18))
gg <- gg + theme(plot.background = element_blank())
gg <- gg + theme(plot.margin = unit(c(0, 0, 0, 0), "null"))
gg <- gg + theme(plot.margin = rep(unit(0, "null"), 4))
gg
}
#### end of function
### examples
waffle(c(5, 7, 11), rows=3) #how to make sure filled from top ?
wafflex(c(5, 7, 11), rows=3) #same as waffle
wafflex(c(5, 7, 11), rows=3, from="topright") # works
# what happens with flip
waffle(c(5, 7, 11), rows=10, flip=T) #how to fill up top row ???
wafflex(c(5, 7, 11), rows=10, flip=T) #same as waffle
wafflex(c(5, 7, 11), rows=10, from="topright", flip=T) #topright method roots to topright, not what want
wafflex(c(5, 7, 11), rows=10, from="topleft", flip=T) #works
#### UK parliament example
library(XML)
doc <- readHTMLTable("http://en.wikipedia.org/wiki/House_of_Commons_of_the_United_Kingdom")
temp <- doc[[4]][c(2,4)]
temp<-temp[2:15,]
colnames(temp)<-c("Party", "Seats")
temp
mycolors <- c("blue", "red", "gold", "tomato", "yellow1", "gray70", "green", "seagreen4", "springgreen1",
"purple", "darkgoldenrod", "olivedrab", "lightcoral", "black")
z <- as.numeric(as.character(temp$Seats))
names(z)<-as.character(temp$Party)
waffle(z, rows=10, colors=mycolors, size=1)
wafflex(z, rows=10, colors=mycolors, size=1, from="topright") +
ggtitle("UK House of Commons MPs by Party") +
theme(
plot.title = element_text(hjust=0,vjust=1, size=rel(1.9)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment