Skip to content

Commit bfa1ad8

Browse files
committed
nest entire grid_swarm layout procedure within group for better layering of groups with order aesthetic
1 parent 9b42ba1 commit bfa1ad8

File tree

4 files changed

+203
-131
lines changed

4 files changed

+203
-131
lines changed

R/RcppExports.R

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
22
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
33

4+
#' Size diff literal for C++ arrays / vectors
5+
#' @noRd
6+
NULL
7+
48
#' Size literal for C++ arrays / vectors
59
#' @noRd
610
NULL
@@ -9,16 +13,14 @@ NULL
913
#' @noRd
1014
NULL
1115

12-
#' Can we place `candidate` at this position given the last placed dot and
13-
#' the previous rows of dots placed so far?
14-
#' @param reverse <scalar [logical]> are we placing dots in reverse order?
15-
#' @param candidate <scalar [numeric]> candidate x position
16-
#' @param rows <[list] of [numeric]> list of previous rows of placed dots
17-
#' @param n_rows_back <scalar [integer]> actual number of previous rows to consider
18-
#' @param ygrid <scalar [integer]> max possible number of previous rows in the
19-
#' y grid that could overlap with this candidate
20-
#' @param xsize <scalar [numeric]> horizontal spacing between dots
21-
#' @returns <scalar [logical]> can we place candidate here?
16+
#' Attempt to place a candidate dot in a target row
17+
#' @param candidate candidate x position
18+
#' @param xsize horizontal spacing between dots
19+
#' @param ygrid size of the y grid (corresponding to 1 + the number of adjacent rows above or
20+
#' below this row that could overlap with dots in this row)
21+
#' @param rows rows of already-placed dots
22+
#' @param target_row iterator pointing at row in `rows` to attempt to place `candidate` in
23+
#' @returns `true` if the candidate was placed successfully
2224
#' @noRd
2325
NULL
2426

@@ -31,26 +33,34 @@ NULL
3133

3234
#' const end iterator for forward or reverse iteration
3335
#' @param reverse iterate in reverse?
34-
#' @param T iterable type
35-
#' @param vec object to iterate over
36+
#' @param C iterable type
37+
#' @param container object to iterate over
3638
#' @noRd
3739
NULL
3840

39-
#' Place dots in a single row in the grid_swarm algorithm
41+
#' push onto front or back of a container
42+
#' @param front push onto front?
43+
#' @param C container type
44+
#' @param container object to push onto
45+
#' @noRd
46+
NULL
47+
48+
#' Attempt to place dots in a specific row in the grid_swarm algorithm
4049
#' @param reverse are we placing dots in reverse order?
4150
#' @param both is this a mirrored layout (`side == "both"`?)
42-
#' @param xsize <scalar [numeric]> horizontal spacing between dots
43-
#' @param ygrid <scalar [integer]> max possible number of previous rows in the
44-
#' y grid that could overlap with this candidate
45-
#' @param remaining vector of dots to be placed
46-
#' @param rows <[list] of [numeric]> list of previous rows of placed dots
47-
#' @param rows_bottom <[list] of [numeric]> list of previous bottom rows of placed dots
48-
#' (when `both == true`)
51+
#' @param candidates dots to be placed
52+
#' @param next_candidates swap space used for next `candidates`
53+
#' @param xsize horizontal spacing between dots
54+
#' @param ygrid size of the y grid (corresponding to 1 + the number of adjacent rows above or
55+
#' below this row that could overlap with dots in this row)
56+
#' @param rows rows of already-placed dots
57+
#' @param rows_bottom bottom rows of already-placed dots (when `both == true`)
58+
#' @param row_i index of `rows` and `rows_bottom` to place candidates in.
4959
#' @returns `true` if `remaining` may still have dots to place and `false` otherwise
5060
#' @noRd
5161
NULL
5262

53-
#' Place dots in `n` rows in the grid_swarm algorithm
63+
#' Place dots in `n` rows in the grid_swarm algorithm, alternating `reverse`
5464
#' See `place_row()`
5565
#' @returns `true` if `remaining` may still have dots to place and `false` otherwise
5666
#' @noRd
@@ -71,3 +81,14 @@ grid_swarm_ <- function(xs, xsize, ysize, ygrid, side) {
7181
.Call(`_ggdist_grid_swarm_`, xs, xsize, ysize, ygrid, side)
7282
}
7383

84+
#' Re-center contiguous clusters around their mean y position so that
85+
#' small clusters are visually centered (rather than e.g. a cluster of
86+
#' two points having one point on the origin line and one above it)
87+
#' @param x sorted numeric vector of dot positions
88+
#' @param y sorted numeric vector of dot heights, same length as x
89+
#' @returns modified `y`
90+
#' @noRd
91+
recenter_swarm_clusters_ <- function(x, y, binwidth) {
92+
.Call(`_ggdist_recenter_swarm_clusters_`, x, y, binwidth)
93+
}
94+

R/bin_dots.R

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,8 @@ recenter_swarm_clusters = function(binner, dots, binning) {
262262
# re-center contiguous clusters around their mean y position so that
263263
# small clusters are visually centered (rather than e.g. a cluster of
264264
# two points having one point on the origin line and one above it)
265-
dots$bin = cumsum(c(1L, diff(dots$x) >= binning$binwidth))
266-
ddply_(dots, "bin", function(bin_df) {
267-
bin_df$y = bin_df$y - mean(bin_df$y)
268-
bin_df
269-
})
265+
dots$y = recenter_swarm_clusters_(dots$x, dots$y, binning$binwidth)
266+
dots
270267
}
271268

272269
#' Get the height of a swarm binning

src/RcppExports.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,36 @@ BEGIN_RCPP
2222
END_RCPP
2323
}
2424
// grid_swarm_
25-
SEXP grid_swarm_(std::vector<std::deque<double>> xs, const double xsize, const double ysize, const std::size_t ygrid, const int side);
25+
SEXP grid_swarm_(std::vector<std::deque<double>> xs, const double xsize, const double ysize, const std::ptrdiff_t ygrid, const int side);
2626
RcppExport SEXP _ggdist_grid_swarm_(SEXP xsSEXP, SEXP xsizeSEXP, SEXP ysizeSEXP, SEXP ygridSEXP, SEXP sideSEXP) {
2727
BEGIN_RCPP
2828
Rcpp::RObject rcpp_result_gen;
2929
Rcpp::traits::input_parameter< std::vector<std::deque<double>> >::type xs(xsSEXP);
3030
Rcpp::traits::input_parameter< const double >::type xsize(xsizeSEXP);
3131
Rcpp::traits::input_parameter< const double >::type ysize(ysizeSEXP);
32-
Rcpp::traits::input_parameter< const std::size_t >::type ygrid(ygridSEXP);
32+
Rcpp::traits::input_parameter< const std::ptrdiff_t >::type ygrid(ygridSEXP);
3333
Rcpp::traits::input_parameter< const int >::type side(sideSEXP);
3434
rcpp_result_gen = Rcpp::wrap(grid_swarm_(xs, xsize, ysize, ygrid, side));
3535
return rcpp_result_gen;
3636
END_RCPP
3737
}
38+
// recenter_swarm_clusters_
39+
SEXP recenter_swarm_clusters_(Rcpp::NumericVector& x, Rcpp::NumericVector& y, const double binwidth);
40+
RcppExport SEXP _ggdist_recenter_swarm_clusters_(SEXP xSEXP, SEXP ySEXP, SEXP binwidthSEXP) {
41+
BEGIN_RCPP
42+
Rcpp::RObject rcpp_result_gen;
43+
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type x(xSEXP);
44+
Rcpp::traits::input_parameter< Rcpp::NumericVector& >::type y(ySEXP);
45+
Rcpp::traits::input_parameter< const double >::type binwidth(binwidthSEXP);
46+
rcpp_result_gen = Rcpp::wrap(recenter_swarm_clusters_(x, y, binwidth));
47+
return rcpp_result_gen;
48+
END_RCPP
49+
}
3850

3951
static const R_CallMethodDef CallEntries[] = {
4052
{"_ggdist_wilkinson_bin_to_right_", (DL_FUNC) &_ggdist_wilkinson_bin_to_right_, 2},
4153
{"_ggdist_grid_swarm_", (DL_FUNC) &_ggdist_grid_swarm_, 5},
54+
{"_ggdist_recenter_swarm_clusters_", (DL_FUNC) &_ggdist_recenter_swarm_clusters_, 3},
4255
{NULL, NULL, 0}
4356
};
4457

0 commit comments

Comments
 (0)