Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tile layout improvement: makes parts contiguous #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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