Created
June 24, 2010 14:32
-
-
Save hadley/451520 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#' For munching, only grobs are lines and polygons: everything else is | |
#' transfomed into those special cases by the geom. | |
#' @examples | |
#' nz <- data.frame(map("nz", plot=FALSE)[c("x","y")]) | |
#' munch_data(nz, segment_length = 0.1) | |
munch_data <- function(data, segment_length) { | |
data <- add_group(data) | |
n <- nrow(data) | |
aes_df <- data[setdiff(names(data), c("x", "y"))] | |
# Distance between points - NA indicates a break / terminal points | |
dist <- with(data, sqrt((x[-n] - x[-1]) ^ 2 + (y[-n] - y[-1]) ^ 2)) | |
dist[data$group[-1] != data$group[-n]] <- NA | |
# How many extra new to create per old segment | |
extra <- floor(dist / segment_length) | |
# The hard part: figure out how to do the interpolation in a vectorised | |
# start and end, and number of pieces between them | |
# start + seq(0, 1, length = n + 1)[-n] * end | |
data.frame( | |
x = x, y = y, | |
aes_df[rep(seq_len(nrow(aes_df)), by = expand)] | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment