Skip to content

Commit 00d8d28

Browse files
authored
Merge pull request #174 from ModelOriented/better-collect
Better collect mechanism
2 parents fe036f4 + 01de26b commit 00d8d28

15 files changed

+242
-157
lines changed

NEWS.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44

55
- `sv_dependence()`: The new arguments `ylim` and `share_y = FALSE` allow to control the y-axis limits.
66
They help to assess the importance in multiple dependence plots ([#172](https://github.com/ModelOriented/shapviz/pull/172)).
7-
Later, we might change the default to `share_y = TRUE` (as in Python's SHAP dependence plots) ([#171](https://github.com/ModelOriented/shapviz/pull/171).
7+
Later, we might change the default to `share_y = TRUE` (as in Python's SHAP dependence plots) ([#171](https://github.com/ModelOriented/shapviz/pull/171)).
88
- `sv_interaction()` has received a new visualization: `kind = "bar"` now shows mean absolute SHAP interactions/main effects as a barplot.
9-
Its appearance can be modified by the arguments `fill` and `bar_width` [#169](https://github.com/ModelOriented/shapviz/pull/169).
9+
Its appearance can be modified by the arguments `fill` and `bar_width` ([#169](https://github.com/ModelOriented/shapviz/pull/169)).
10+
- We are now (cautiously) collecting axes, axis titles, and color guides via {patchwork} ([#171](https://github.com/ModelOriented/shapviz/pull/171)).
11+
Currently fails for `sv_force()`.
1012

11-
### User-visible changes
13+
### Minor user-visible changes
1214

13-
- We are now (cautiously) collecting axes, axis titles, and color guides via {patchwork} [#171](https://github.com/ModelOriented/shapviz/pull/171).
14-
(Currently fails for `sv_force()`.)
15-
- The color bars in `sv_dependence2D()` are less wide [#169](https://github.com/ModelOriented/shapviz/pull/169).
15+
- The color bars in `sv_dependence2D()` are less wide ([#169](https://github.com/ModelOriented/shapviz/pull/169)).
16+
- Jittering now uses a seed ([#174](https://github.com/ModelOriented/shapviz/pull/174)).
1617

1718
### Minor API changes
1819

1920
- In `sv_dependence()`, passing the same variable for `v` and `color_var` does not suppress the color axis anymore,
20-
except when `interactions = TRUE` ([#171](https://github.com/ModelOriented/shapviz/pull/171).
21+
except when `interactions = TRUE` ([#171](https://github.com/ModelOriented/shapviz/pull/171)).
22+
- `sv_dependence()` and `sv_dependence2D()` has received a `seed = 1` argument used for jittering.
23+
This does not modify the global seed ([#174](https://github.com/ModelOriented/shapviz/pull/174)).
2124

2225
### Maintenance
2326

24-
- Update code coverage version [#168](https://github.com/ModelOriented/shapviz/pull/168).
27+
- Update code coverage version ([#168](https://github.com/ModelOriented/shapviz/pull/168)).
28+
- More unit tests ([#173](https://github.com/ModelOriented/shapviz/pull/173)).
29+
- Bump minimal version of {ggplot2} ([#174](https://github.com/ModelOriented/shapviz/pull/174)).
2530

2631
# shapviz 0.9.7
2732

R/collect_axes.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Functions that help to derive collection info for patchwork
2+
3+
# Same logic for axes and axis_titles
4+
.collect_xy <- function(z) {
5+
if (z$x && z$y) {
6+
return("collect")
7+
}
8+
if (z$x) {
9+
return("collect_x")
10+
}
11+
if (z$y) {
12+
return("collect_y")
13+
}
14+
return("keep")
15+
}
16+
17+
# Takes list of ggplots and determines collection info for patchwork
18+
.collect <- function(plot_list) {
19+
plot_list <- lapply(plot_list, ggplot2::ggplot_build)
20+
ll <- lapply(plot_list, ggplot2::get_labs) # Version 3.5.2
21+
22+
titles_unique <- axes_unique <- list()
23+
for (z in c("x", "y", "colour")) {
24+
temp <- lapply(plot_list, ggplot2::get_guide_data, aesthetic = z)
25+
if (z %in% c("x", "y")) {
26+
# we should not test for equality of x when interested in y and vice versa
27+
temp <- lapply(temp, `[`, c(z, ".value", ".label"))
28+
}
29+
axes_unique[[z]] <- .all_identical(temp, ignore_null = TRUE)
30+
31+
titles_unique[[z]] <- .all_identical(
32+
lapply(ll, `[[`, z),
33+
ignore_null = (z == "colour")
34+
)
35+
}
36+
37+
out <- list(
38+
axis_titles = .collect_xy(titles_unique),
39+
axes = .collect_xy(axes_unique),
40+
guides = if (titles_unique$colour && axes_unique$colour) "collect" else "keep"
41+
)
42+
43+
return(out)
44+
}
45+
46+
# Check if the elements in z (list or vector) are all identical, ignoring NULLs
47+
.all_identical <- function(z, ignore_null = TRUE) {
48+
if (ignore_null) {
49+
z <- z[!vapply(z, is.null, logical(1L))]
50+
}
51+
n <- length(z)
52+
if (n <= 1L) {
53+
return(TRUE)
54+
}
55+
return(all(vapply(z[-1L], FUN = identical, z[[1L]], FUN.VALUE = logical(1L))))
56+
}

0 commit comments

Comments
 (0)