Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions R/stat-waffle.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ StatWaffle <- ggplot2::ggproto(
x = seq_len((ceiling(sum(.x[["values"]]) / params$n_rows)))#,
# stringsAsFactors = FALSE
) -> tdf
tdf <- snake_sort_grid(tdf)

parts_vec <- c(parts_vec, rep(NA, nrow(tdf)-length(parts_vec)))
colour_vec <- c(colour_vec, rep(NA, nrow(tdf)-length(colour_vec)))
Expand Down
8 changes: 8 additions & 0 deletions R/utils.r
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ insert_unit <- function (x, values, after = length(x)) {

}

# order an expand.grid alternatingly, providing a better layout for waffle
snake_sort_grid <- function(grid){
y <- grid[[1]]
x <- grid[[2]]
o <- order(x, ifelse(x%%2 > 0, y, -y))
grid[o,]
}

# Name ggplot grid object
# Convenience function to name grid objects
#
Expand Down
3 changes: 3 additions & 0 deletions R/waffle.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ waffle <- function(parts, rows=10, keep=TRUE, xlab=NULL, title=NULL, colors=NA,
# setup the data frame for geom_rect
dat <- expand.grid(y = 1:rows, x = seq_len(pad + (ceiling(sum(parts) / rows))))

# order the grid snake wise...
dat <- snake_sort_grid(dat)

# add NAs if needed to fill in the "rectangle"
dat$value <- c(parts_vec, rep(NA, nrow(dat) - length(parts_vec)))

Expand Down