Skip to content

Commit 98c49fe

Browse files
authored
Eliminate reshape dependency (#3639)
1 parent bd1bffd commit 98c49fe

16 files changed

+107
-35
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ Imports:
2727
isoband,
2828
MASS,
2929
mgcv,
30-
reshape2,
3130
rlang (>= 0.3.0),
3231
scales (>= 0.5.0),
3332
stats,
@@ -185,6 +184,7 @@ Collate:
185184
'position-stack.r'
186185
'quick-plot.r'
187186
'range.r'
187+
'reshape-add-margins.R'
188188
'save.r'
189189
'scale-.r'
190190
'scale-alpha.r'

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* ggplot2 no longer depends on reshape2, which means that it no longer
4+
(recursively) needs plyr, stringr, or stringi packages.
5+
36
* `geom_sf()` now determines the legend type automatically (@microly, #3646).
47

58
* `scale_x_continuous()` and `scale_y_continuous()` gains an `n.breaks` argument

R/aes-group-order.r

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@
3030
#' a + geom_bar(aes(fill = factor(vs)))
3131
#'
3232
#' # Using linetypes
33-
#' rescale01 <- function(x) (x - min(x)) / diff(range(x))
34-
#' ec_scaled <- data.frame(
35-
#' date = economics$date,
36-
#' lapply(economics[, -(1:2)], rescale01))
37-
#' ecm <- reshape2::melt(ec_scaled, id.vars = "date")
38-
#' f <- ggplot(ecm, aes(date, value))
33+
#' f <- ggplot(economics_long, aes(date, value01))
3934
#' f + geom_line(aes(linetype = variable))
4035
#'
4136
#' # Using facets

R/facet-grid-.r

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ NULL
3232
#' one with `vars(cyl, am)`. Each output
3333
#' column gets displayed as one separate line in the strip
3434
#' label. This function should inherit from the "labeller" S3 class
35-
#' for compatibility with [labeller()]. You can use different labeling
36-
#' functions for different kind of labels, for example use [label_parsed()] for
37-
#' formatting facet labels. [label_value()] is used by default,
35+
#' for compatibility with [labeller()]. You can use different labeling
36+
#' functions for different kind of labels, for example use [label_parsed()] for
37+
#' formatting facet labels. [label_value()] is used by default,
3838
#' check it for more details and pointers to other options.
3939
#' @param as.table If `TRUE`, the default, the facets are laid out like
4040
#' a table with highest values at the bottom-right. If `FALSE`, the
@@ -215,8 +215,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
215215
}
216216

217217
# Add margins
218-
base <- reshape2::add_margins(base, list(names(rows), names(cols)), params$margins)
219-
# Work around bug in reshape2
218+
base <- reshape_add_margins(base, list(names(rows), names(cols)), params$margins)
220219
base <- unique(base)
221220

222221
# Create panel info dataset
@@ -252,7 +251,7 @@ FacetGrid <- ggproto("FacetGrid", Facet,
252251
# Compute faceting values and add margins
253252
margin_vars <- list(intersect(names(rows), names(data)),
254253
intersect(names(cols), names(data)))
255-
data <- reshape2::add_margins(data, margin_vars, params$margins)
254+
data <- reshape_add_margins(data, margin_vars, params$margins)
256255

257256
facet_vals <- eval_facets(c(rows, cols), data, params$plot_env)
258257

R/geom-histogram.r

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
#' # You can specify a function for calculating binwidth, which is
8989
#' # particularly useful when faceting along variables with
9090
#' # different ranges because the function will be called once per facet
91-
#' mtlong <- reshape2::melt(mtcars)
92-
#' ggplot(mtlong, aes(value)) + facet_wrap(~variable, scales = 'free_x') +
91+
#' ggplot(economics_long, aes(value)) +
92+
#' facet_wrap(~variable, scales = 'free_x') +
9393
#' geom_histogram(binwidth = function(x) 2 * IQR(x) / (length(x)^(1/3)))
9494
geom_histogram <- function(mapping = NULL, data = NULL,
9595
stat = "bin", position = "stack",

R/geom-map.r

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,23 @@ NULL
4545
#' expand_limits(positions) + ylim(0, 3)
4646
#'
4747
#' # Better example
48-
#' crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
49-
#' crimesm <- reshape2::melt(crimes, id = 1)
5048
#' if (require(maps)) {
49+
#'
50+
#' crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
51+
#'
52+
#' # Equivalent to crimes %>% tidyr::pivot_longer(Murder:Rape)
53+
#' vars <- lapply(names(crimes)[-1], function(j) {
54+
#' data.frame(state = crimes$state, variable = j, value = crimes[[j]])
55+
#' })
56+
#' crimes_long <- do.call("rbind", vars)
57+
#'
5158
#' states_map <- map_data("state")
5259
#' ggplot(crimes, aes(map_id = state)) +
5360
#' geom_map(aes(fill = Murder), map = states_map) +
5461
#' expand_limits(x = states_map$long, y = states_map$lat)
5562
#'
5663
#' last_plot() + coord_map()
57-
#' ggplot(crimesm, aes(map_id = state)) +
64+
#' ggplot(crimes_long, aes(map_id = state)) +
5865
#' geom_map(aes(fill = value), map = states_map) +
5966
#' expand_limits(x = states_map$long, y = states_map$lat) +
6067
#' facet_wrap( ~ variable)

R/guide-colorbar.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
#' @export
5252
#' @family guides
5353
#' @examples
54-
#' df <- reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
54+
#' df <- expand.grid(X1 = 1:10, X2 = 1:10)
55+
#' df$value <- df$X1 * df$X2
5556
#'
5657
#' p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
5758
#' p2 <- p1 + geom_point(aes(size = value))

R/guide-colorsteps.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#'
1818
#' @family guides
1919
#' @examples
20-
#' df <- reshape2::melt(outer(1:10, 1:10), varnames = c("X1", "X2"))
20+
#' df <- expand.grid(X1 = 1:10, X2 = 1:10)
21+
#' df$value <- df$X1 * df$X2
2122
#'
2223
#' p <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
2324
#'

R/guide-legend.r

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@
6161
#' @family guides
6262
#' @examples
6363
#' \donttest{
64-
#' df <- reshape2::melt(outer(1:4, 1:4), varnames = c("X1", "X2"))
64+
#' df <- expand.grid(X1 = 1:10, X2 = 1:10)
65+
#' df$value <- df$X1 * df$X2
6566
#'
6667
#' p1 <- ggplot(df, aes(X1, X2)) + geom_tile(aes(fill = value))
6768
#' p2 <- p1 + geom_point(aes(size = value))

R/reshape-add-margins.R

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Inlined from reshape2
2+
reshape_add_margins <- function(df, vars, margins = TRUE) {
3+
margin_vars <- reshape_margins(vars, margins)
4+
5+
# Return data frame if no margining necessary
6+
if (length(margin_vars) == 0) return(df)
7+
8+
# Prepare data frame for addition of margins
9+
addAll <- function(x) {
10+
x <- addNA(x, TRUE)
11+
factor(x, levels = c(levels(x), "(all)"), exclude = NULL)
12+
}
13+
vars <- unique(unlist(margin_vars))
14+
df[vars] <- lapply(df[vars], addAll)
15+
16+
rownames(df) <- NULL
17+
18+
# Loop through all combinations of margin variables, setting
19+
# those variables to (all)
20+
margin_dfs <- lapply(margin_vars, function(vars) {
21+
df[vars] <- rep(list(factor("(all)")), length(vars))
22+
df
23+
})
24+
25+
do.call("rbind", margin_dfs)
26+
}
27+
28+
reshape_margins <- function(vars, margins = NULL) {
29+
if (is.null(margins) || identical(margins, FALSE)) return(NULL)
30+
31+
all_vars <- unlist(vars)
32+
if (isTRUE(margins)) {
33+
margins <- all_vars
34+
}
35+
36+
# Start by grouping margins by dimension
37+
dims <- lapply(vars, intersect, margins)
38+
39+
# Next, ensure high-level margins include lower-levels
40+
dims <- mapply(function(vars, margin) {
41+
lapply(margin, downto, vars)
42+
}, vars, dims, SIMPLIFY = FALSE, USE.NAMES = FALSE)
43+
44+
# Finally, find intersections across all dimensions
45+
seq_0 <- function(x) c(0, seq_along(x))
46+
indices <- expand.grid(lapply(dims, seq_0), KEEP.OUT.ATTRS = FALSE)
47+
# indices <- indices[rowSums(indices) > 0, ]
48+
49+
lapply(seq_len(nrow(indices)), function(i){
50+
unlist(mapply("[", dims, indices[i, ], SIMPLIFY = FALSE))
51+
})
52+
}
53+
54+
55+
upto <- function(a, b) {
56+
b[seq_len(match(a, b, nomatch = 0))]
57+
}
58+
downto <- function(a, b) {
59+
rev(upto(a, rev(b)))
60+
}

man/aes_group_order.Rd

Lines changed: 1 addition & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_histogram.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/geom_map.Rd

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/guide_colourbar.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/guide_coloursteps.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/guide_legend.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)