Skip to content

Commit

Permalink
Break munching out into own function
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jun 24, 2010
1 parent d8e9afe commit b23dc71
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ License: GPL
URL: http://had.co.nz/ggplot2/
LazyLoad: false
LazyData: true
Collate: aaa-.r aaa-compare.r aaa-constants.r aaa-examples.r aaa-html.r aaa-rdoc.r aes.r annotation.r coord-.r coord-cartesian-.r coord-cartesian-equal.r coord-cartesian-flipped.r coord-map.r coord-polar.r coord-transform.r date-time-breaks.r date-time.r dhist.r facet-.r facet-grid-.r facet-labels.r facet-viewports.r facet-wrap.r formats.r fortify-lm.r fortify-map.r fortify-spatial.r fortify.r geom-.r geom-abline.r geom-bar-.r geom-bar-histogram.r geom-bin2d.r geom-blank.r geom-boxplot.r geom-crossbar.r geom-defaults.r geom-error.r geom-errorh.r geom-freqpoly.r geom-hex.r geom-hline.r geom-linerange.r geom-path-.r geom-path-contour.r geom-path-density2d.r geom-path-line.r geom-path-step.r geom-point-.r geom-point-jitter.r geom-pointrange.r geom-polygon.r geom-quantile.r geom-rect.r geom-ribbon-.r geom-ribbon-density.r geom-rug.r geom-segment.r geom-smooth.r geom-text.r geom-tile.r geom-vline.r grob-absolute.r grob-background.r grob-grid.r grob-null.r guides-axis.r guides-grid.r guides-legend.r labels.r layer.r matrix.r plot-build.r plot-construction.r plot-last.r plot-render.r plot-surrounds.r plot.r position-.r position-collide.r position-dodge.r position-fill.r position-identity.r position-jitter.r position-stack.r quick-plot.r save.r scale-.r scale-continuous-.r scale-continuous-alpha.r scale-continuous-colour.r scale-convenience.r scale-date.r scale-datetime.r scale-defaults.r scale-discrete-.r scale-discrete-colour.r scale-discrete-grey.r scale-discrete-position.r scale-identity.r scale-linetype.r scale-manual.r scale-shape.r scale-size.r scales-.r stat-.r stat-bin.r stat-bin2d.r stat-binhex.r stat-boxplot.r stat-contour.r stat-density-2d.r stat-density.r stat-function.r stat-identity.r stat-qq.r stat-quantile.r stat-smooth-methods.r stat-smooth.r stat-spoke.r stat-sum.r stat-summary.r stat-unique.r stat-vline.r summary.r templates.r theme-defaults.r theme-elements.r theme.r trans-.r trans-scales.r utilities-break.r utilities-colour.r utilities-discrete.r utilities-facet.r utilities-grid.r utilities-layer.r utilities-matrix.r utilities-position.r utilities-resolution.r utilities.r xxx-codegen.r xxx-digest.r xxx.r zxx.r
Collate: aaa-.r aaa-compare.r aaa-constants.r aaa-examples.r aaa-html.r aaa-rdoc.r aes.r annotation.r coord-.r coord-cartesian-.r coord-cartesian-equal.r coord-cartesian-flipped.r coord-map.r coord-polar.r coord-transform.r date-time-breaks.r date-time.r dhist.r facet-.r facet-grid-.r facet-labels.r facet-viewports.r facet-wrap.r formats.r fortify-lm.r fortify-map.r fortify-spatial.r fortify.r geom-.r geom-abline.r geom-bar-.r geom-bar-histogram.r geom-bin2d.r geom-blank.r geom-boxplot.r geom-crossbar.r geom-defaults.r geom-error.r geom-errorh.r geom-freqpoly.r geom-hex.r geom-hline.r geom-linerange.r geom-path-.r geom-path-contour.r geom-path-density2d.r geom-path-line.r geom-path-step.r geom-point-.r geom-point-jitter.r geom-pointrange.r geom-polygon.r geom-quantile.r geom-rect.r geom-ribbon-.r geom-ribbon-density.r geom-rug.r geom-segment.r geom-smooth.r geom-text.r geom-tile.r geom-vline.r grob-absolute.r grob-background.r grob-grid.r grob-null.r guides-axis.r guides-grid.r guides-legend.r labels.r layer.r matrix.r plot-build.r plot-construction.r plot-last.r plot-render.r plot-surrounds.r plot.r position-.r position-collide.r position-dodge.r position-fill.r position-identity.r position-jitter.r position-stack.r quick-plot.r save.r scale-.r scale-continuous-.r scale-continuous-alpha.r scale-continuous-colour.r scale-convenience.r scale-date.r scale-datetime.r scale-defaults.r scale-discrete-.r scale-discrete-colour.r scale-discrete-grey.r scale-discrete-position.r scale-identity.r scale-linetype.r scale-manual.r scale-shape.r scale-size.r scales-.r stat-.r stat-bin.r stat-bin2d.r stat-binhex.r stat-boxplot.r stat-contour.r stat-density-2d.r stat-density.r stat-function.r stat-identity.r stat-qq.r stat-quantile.r stat-smooth-methods.r stat-smooth.r stat-spoke.r stat-sum.r stat-summary.r stat-unique.r stat-vline.r summary.r templates.r theme-defaults.r theme-elements.r theme.r trans-.r trans-scales.r utilities-break.r utilities-colour.r utilities-discrete.r utilities-facet.r utilities-grid.r utilities-layer.r utilities-matrix.r utilities-position.r utilities-resolution.r utilities.r xxx-codegen.r xxx-digest.r xxx.r zxx.r coord-munch.r
25 changes: 6 additions & 19 deletions R/coord-.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,17 @@ Coord <- proto(TopLevel, expr={
rescale(data, 0:1, range, clip = clip)
}

munch <- function(., data, details, npieces=50) {
munch <- function(., data, details, segment_length = 0.01) {
data <- add_group(data)
data <- .$transform(data, details)

if (!.$muncher()) {
.$transform(data, details)
data
} else {
data <- add_group(data)

groups <- split(data, data$group)
munched_groups <- lapply(groups, function(df) .$munch_group(df, details, npieces))
do.call("rbind", munched_groups)
munch_data(data, segment_length)
}
}

munch_group <- function(., data, details, npieces=50) {
n <- nrow(data)

x <- approx(data$x, n = npieces * (n - 1) + 1)$y
y <- approx(data$y, n = npieces * (n - 1) + 1)$y

cbind(
.$transform(data.frame(x=x, y=y), details),
data[c(rep(1:(n-1), each=npieces), n), setdiff(names(data), c("x", "y"))]
)
}

compute_aspect <- function(., ranges) {
NULL
}
Expand Down
30 changes: 30 additions & 0 deletions R/coord-munch.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
munch_data <- function(data, segment_length) {


ddply(data, "group", munch_group)
}

munch_group <- function(data, npieces = 50) {
n <- nrow(data)

x <- approx(data$x, n = npieces * (n - 1) + 1)$y
y <- approx(data$y, n = npieces * (n - 1) + 1)$y

data.frame(
x = x, y = y,
data[c(rep(1:(n-1), each=npieces), n), setdiff(names(data), c("x", "y"))]
)
}

# if (!.$muncher()) return(data)
# n <- nrow(data)
#
# # Distance between points - NA indicates a break
# dist <- with(data, sqrt((x[-n] - x[-1]) ^ 2 + (y[-n] - y[-1]) ^ 2))
# dist[data$group[-1] != data$group[-n]] <- NA
#
# # How many new segments to create per old segment
# expand <- ceiling(dist / segment_length)
#
# x <- approx(data$x, n = npieces * (n - 1) + 1)$y
# y <- approx(data$y, n = npieces * (n - 1) + 1)$y

0 comments on commit b23dc71

Please sign in to comment.