Skip to content

Commit

Permalink
update for ggplot2v3.5
Browse files Browse the repository at this point in the history
- integrate Pana's updates; #269
- add new param `staplewidth`
- remove copy of dapply
- tests: remove failing tests (will fix and bring them back eventually later)
- tests: dont use `ggiraphOutput()`
- deprecate `ggiraph()`
  • Loading branch information
davidgohel authored Feb 23, 2024
1 parent 82819d7 commit 3672a99
Show file tree
Hide file tree
Showing 46 changed files with 1,304 additions and 1,001 deletions.
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Package: ggiraph
Type: Package
Title: Make 'ggplot2' Graphics Interactive
Description: Create interactive 'ggplot2' graphics using 'htmlwidgets'.
Version: 0.8.8
Version: 0.8.9.002
Authors@R: c(
person("David", "Gohel", role = c("aut", "cre"),
email = "david.gohel@ardata.fr"),
Expand All @@ -26,14 +26,14 @@ Copyright: See file COPYRIGHTS.
Encoding: UTF-8
SystemRequirements: libpng
Imports:
grid, ggplot2 (>= 3.4.0),
grid, ggplot2 (>= 3.5.0),
htmlwidgets (>= 1.5),
stats,
htmltools,
Rcpp (>= 1.0),
systemfonts,
purrr, rlang, uuid,
vctrs
vctrs, cli
LinkingTo: Rcpp, systemfonts
Suggests:
knitr,
Expand All @@ -51,8 +51,9 @@ Suggests:
VignetteBuilder: knitr
URL: https://davidgohel.github.io/ggiraph/
BugReports: https://github.com/davidgohel/ggiraph/issues
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Remotes: tidyverse/ggplot2@rc/3.5.0
Collate:
'RcppExports.R'
'ipar.R'
Expand Down
17 changes: 5 additions & 12 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ S3method(drawDetails,interactive_text_grob)
S3method(drawDetails,interactive_xspline_grob)
S3method(element_grob,interactive_element)
S3method(element_grob,interactive_element_text)
S3method(guide_gengrob,interactive_colourbar)
S3method(guide_gengrob,interactive_coloursteps)
S3method(guide_gengrob,interactive_guide)
S3method(guide_geom,interactive_bins)
S3method(guide_geom,interactive_guide)
S3method(guide_geom,interactive_legend)
S3method(guide_train,interactive_bins)
S3method(guide_train,interactive_colourbar)
S3method(guide_train,interactive_coloursteps)
S3method(guide_train,interactive_legend)
S3method(makeContent,interactive_curve_grob)
S3method(makeContent,interactive_label_grob)
S3method(makeContent,interactive_repeltree_grob)
S3method(makeContent,interactive_roundrect_grob)
S3method(makeContext,interactive_dotstack_grob)
Expand Down Expand Up @@ -70,6 +59,10 @@ export(GeomInteractiveText)
export(GeomInteractiveTile)
export(GeomInteractiveViolin)
export(GeomInteractiveVline)
export(GuideInteractiveBins)
export(GuideInteractiveColourbar)
export(GuideInteractiveColoursteps)
export(GuideInteractiveLegend)
export(StatInteractiveBoxplot)
export(annotate_interactive)
export(annotation_raster_interactive)
Expand Down Expand Up @@ -288,7 +281,6 @@ importFrom(htmlwidgets,createWidget)
importFrom(htmlwidgets,shinyRenderWidget)
importFrom(htmlwidgets,shinyWidgetOutput)
importFrom(htmlwidgets,sizingPolicy)
importFrom(purrr,compact)
importFrom(purrr,detect_index)
importFrom(purrr,flatten)
importFrom(purrr,imap)
Expand All @@ -298,6 +290,7 @@ importFrom(rlang,arg_match)
importFrom(rlang,caller_env)
importFrom(rlang,env_get_list)
importFrom(rlang,eval_tidy)
importFrom(rlang,inherits_any)
importFrom(rlang,is_named)
importFrom(rlang,is_scalar_character)
importFrom(rlang,is_scalar_double)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# ggiraph 0.8.9

## Changes

- adapt guides to ggplot '3.5.0'
- deprecate ggiraph

# ggiraph 0.8.8

## Issues
Expand Down
21 changes: 21 additions & 0 deletions R/geom_boxplot_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ GeomInteractiveBoxplot <- ggproto(
outlier.alpha = NULL,
notch = FALSE,
notchwidth = 0.5,
staplewidth = 0,
varwidth = FALSE,
flipped_aes = FALSE,
.ipar = IPAR_NAMES) {
Expand Down Expand Up @@ -242,10 +243,30 @@ GeomInteractiveBoxplot <- ggproto(
outliers_grob <- NULL
}

if (staplewidth != 0) {
staples <- data_frame0(
x = rep((data$xmin - data$x) * staplewidth + data$x, 2),
xend = rep((data$xmax - data$x) * staplewidth + data$x, 2),
y = c(data$ymax, data$ymin),
yend = c(data$ymax, data$ymin),
alpha = c(NA_real_, NA_real_),
!!!common,
.size = 2
)
staples <- flip_data(staples, flipped_aes)
staple_grob <- GeomInteractiveSegment$draw_panel(
staples, panel_params, coord,
lineend = lineend, .ipar = .ipar
)
} else {
staple_grob <- NULL
}

ggname(
"geom_boxplot_interactive",
grobTree(
outliers_grob,
staple_grob,
GeomInteractiveSegment$draw_panel(whiskers, panel_params, coord, lineend = lineend, .ipar = .ipar),
GeomInteractiveCrossbar$draw_panel(box, fatten = fatten, panel_params, coord,
lineend = lineend,
Expand Down
31 changes: 16 additions & 15 deletions R/geom_label_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@ GeomInteractiveLabel <- ggproto(
if (is.null(coords$tooltip_fill)) {
coords$tooltip_fill <- coords$fill
}
add_interactive_attrs(gr, coords, ipar = .ipar)
for (i in seq_along(gr$children)) {
for (j in seq_along(gr$children[[i]]$children)) {
if (inherits(gr$children[[i]]$children[[j]], "roundrect")) {
gr$children[[i]]$children[[j]] <- add_interactive_attrs(
gr$children[[i]]$children[[j]],
data = coords[i, ], ipar = .ipar
)
} else if (inherits(gr$children[[i]]$children[[j]], "titleGrob")) {
gr$children[[i]]$children[[j]]$children[[1]] <- add_interactive_attrs(
gr$children[[i]]$children[[j]]$children[[1]],
data = coords[i, ], ipar = .ipar
)
}
}
}
gr
}
)

#' @export
makeContent.interactive_label_grob <- function(x) {
gr <- NextMethod()
data <- get_interactive_data(x)
data_attr <- get_data_attr(x)
ipar <- get_ipar(x)
for (i in seq_along(gr$children)) {
gr$children[[i]] <- add_interactive_attrs(
gr$children[[i]], data = data, data_attr = data_attr, ipar = ipar
)
}
gr
}
88 changes: 21 additions & 67 deletions R/geom_path_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,88 +37,39 @@ GeomInteractivePath <- ggproto(
linemitre = 10,
na.rm = FALSE,
.ipar = IPAR_NAMES) {
if (!anyDuplicated(data$group)) {
message_wrap(
"geom_path: Each group consists of only one observation. ",
"Do you need to adjust the group aesthetic?"
)
gr <- GeomPath$draw_panel(
data = data,
panel_params = panel_params,
coord = coord,
arrow = arrow,
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre,
na.rm = na.rm)

if (inherits(gr, "zeroGrob")) {
return(gr)
}

# must be sorted on group
data <- data[order(data$group), , drop = FALSE]
munched <- coord_munch(coord, data, panel_params)

# Silently drop lines with less than two points, preserving order
rows <-
stats::ave(seq_len(nrow(munched)), munched$group, FUN = length)
munched <- munched[rows >= 2, ]
if (nrow(munched) < 2)
return(zeroGrob())

# Work out whether we should use lines or segments
attr <- dapply(munched, "group", function(df) {
linetype <- unique(df$linetype)
new_data_frame(list(
solid = identical(linetype, 1) || identical(linetype, "solid"),
constant = nrow(unique(df[, c("alpha", "colour", "linewidth", "linetype")])) == 1
),
n = 1)
})
solid_lines <- all(attr$solid)
constant <- all(attr$constant)
if (!solid_lines && !constant) {
abort(
"geom_path_interactive: If you are using dotted or dashed lines",
", colour, size and linetype must be constant over the line",
call. = FALSE
)
}

# Work out grouping variables for grobs
n <- nrow(munched)
group_diff <- munched$group[-1] != munched$group[-n]
start <- c(TRUE, group_diff)
end <- c(group_diff, TRUE)
constant <- length(gr$x0) == n

if (!constant) {
gr <- segmentsGrob(
munched$x[!end],
munched$y[!end],
munched$x[!start],
munched$y[!start],
default.units = "native",
arrow = arrow,
gp = gpar(
col = alpha(munched$colour, munched$alpha)[!end],
fill = alpha(munched$colour, munched$alpha)[!end],
lwd = munched$linewidth[!end] * .pt,
lty = munched$linetype[!end],
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)
group_diff <- munched$group[-1] != munched$group[-n]
end <- c(group_diff, TRUE)
add_interactive_attrs(gr, munched, rows = !end, ipar = .ipar)
} else {
id <- match(munched$group, unique(munched$group))
gr <- polylineGrob(
munched$x,
munched$y,
id = id,
default.units = "native",
arrow = arrow,
gp = gpar(
col = alpha(munched$colour, munched$alpha)[start],
fill = alpha(munched$colour, munched$alpha)[start],
lwd = munched$linewidth[start] * .pt,
lty = munched$linetype[start],
lineend = lineend,
linejoin = linejoin,
linemitre = linemitre
)
)
add_interactive_attrs(gr, munched, ipar = .ipar)
}

}
)

Expand Down Expand Up @@ -160,7 +111,10 @@ GeomInteractiveStep <-
parameters = interactive_geom_parameters,
draw_key = interactive_geom_draw_key,
draw_panel = function(data, panel_params, coord, direction = "hv", .ipar = IPAR_NAMES) {
data <- dapply(data, "group", stairstep, direction = direction)
ldata <- split(data, data$group)
ldata <- lapply(ldata, stairstep, direction = direction)
data <- do.call(rbind, ldata)
row.names(data) <- NULL
GeomInteractivePath$draw_panel(data, panel_params, coord, .ipar = .ipar)
}
)
Expand Down Expand Up @@ -199,5 +153,5 @@ stairstep <- function(data, direction = "hv") {
data_attr <- data[xs, setdiff(names(data), c("x", "y"))]
}

new_data_frame(c(list(x = x, y = y), data_attr))
data_frame0(x = x, y = y, data_attr)
}
12 changes: 7 additions & 5 deletions R/geom_pointrange_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ GeomInteractivePointrange <- ggproto(
panel_params,
coord,
fatten = 4,
flipped_aes = FALSE,
flipped_aes = FALSE,
na.rm = FALSE,
.ipar = IPAR_NAMES) {
if (is.null(data[[flipped_names(flipped_aes)$y]]))
if (is.null(data[[flipped_names(flipped_aes)$y]])) {
return(
GeomInteractiveLinerange$draw_panel(data, panel_params, coord, flipped_aes = flipped_aes, .ipar = .ipar)
GeomInteractiveLinerange$draw_panel(data, panel_params, coord, flipped_aes = flipped_aes, na.rm = na.rm, .ipar = .ipar)
)
}

ggname("geom_pointrange", gTree(
children = gList(
GeomInteractiveLinerange$draw_panel(data, panel_params, coord, flipped_aes = flipped_aes, .ipar = .ipar),
GeomInteractivePoint$draw_panel(transform(data, size = size * fatten), panel_params, coord, .ipar = .ipar)
GeomInteractiveLinerange$draw_panel(data, panel_params, coord, flipped_aes = flipped_aes, na.rm = na.rm, .ipar = .ipar),
GeomInteractivePoint$draw_panel(transform(data, size = size * fatten), panel_params, coord, na.rm = na.rm, .ipar = .ipar)
)
))
}
Expand Down
17 changes: 16 additions & 1 deletion R/geom_violin_interactive.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,19 @@ GeomInteractiveViolin <- ggproto(
}
)

create_quantile_segment_frame <- ggplot2:::create_quantile_segment_frame
create_quantile_segment_frame <- function(data, draw_quantiles) {
dens <- cumsum(data$density) / sum(data$density)
ecdf <- stats::approxfun(dens, data$y, ties = "ordered")
ys <- ecdf(draw_quantiles) # these are all the y-values for quantiles

# Get the violin bounds for the requested quantiles.
violin.xminvs <- (stats::approxfun(data$y, data$xminv))(ys)
violin.xmaxvs <- (stats::approxfun(data$y, data$xmaxv))(ys)

# We have two rows per segment drawn. Each segment gets its own group.
data_frame0(
x = vctrs::vec_interleave(violin.xminvs, violin.xmaxvs),
y = rep(ys, each = 2),
group = rep(ys, each = 2)
)
}
8 changes: 1 addition & 7 deletions R/ggiraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#' @param selection_type row selection mode ("single", "multiple", "none")
#' when widget is in a Shiny application.
#' @param selected_css css to apply when element is selected (shiny only).
#' @param dep_dir Deprecated; the path where the output files are stored. If `NULL`,
#' the current path for temporary files is used.
#' @export
#' @keywords internal
ggiraph <- function(code, ggobj = NULL,
Expand All @@ -35,12 +33,8 @@ ggiraph <- function(code, ggobj = NULL,
zoom_max = 1,
selection_type = "multiple",
selected_css = NULL,
dep_dir = NULL,
...) {
message("Function `ggiraph()` is replaced by `girafe()` and will be removed soon.")
if( !missing(dep_dir) ){
warning("argument `dep_dir` has been deprecated.")
}
.Deprecated(new = "girafe", old = "ggiraph")

x <- girafe(code = code, ggobj = ggobj, pointsize = pointsize,
width_svg = width_svg, height_svg = height_svg, ...)
Expand Down
Loading

0 comments on commit 3672a99

Please sign in to comment.