Skip to content

make plotly axes default to ggplots axes#156

Merged
vandenman merged 6 commits into
jasp-stats:masterfrom
vandenman:fix/plotly_axes
Jul 2, 2026
Merged

make plotly axes default to ggplots axes#156
vandenman merged 6 commits into
jasp-stats:masterfrom
vandenman:fix/plotly_axes

Conversation

@vandenman

Copy link
Copy Markdown
Contributor

Fixes the issue we discussed over Mattermost (is there an issue for this?)

Reprex in R

library(jaspGraphs)
library(ggplot2)

set.seed(1)

sample_df <- data.frame(x = rnorm(30), y = rnorm(30))

ggplt2 <- ggplot(data = sample_df, mapping = aes(x = x, y = y)) +
  geom_point() +
  scale_x_continuous(breaks = seq(-8, 5, 1), limits = c(-8, 5)) +
  jaspGraphs::geom_rangeframe()

plplt2 <- jaspGraphs::convertGgplotToPlotly(ggplt2, FALSE)
print(plplt2)

The code is not very pretty, but the idea is:

  • Leave tickmode at array (ggplot2) default).
  • Instead, add custom js code that changes tickmode to "auto" whenever the plot is panned or zoomed.
  • Revert to the original tickmode (and thus ticks) when a user resets the axes (or double-clicks).

This also required some changes to the rangeframe code.
According to chatgpt, there is no R way of doing this.

@vandenman vandenman requested a review from juliuspfadt June 25, 2026 22:00
@juliuspfadt

juliuspfadt commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

I cant find an issue for this and I also cant seem to reproduce the weird axis ticks. I will test nevertheless with jaspDescriptives

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates convertGgplotToPlotly() so Plotly axes initially keep ggplot2’s explicit tick specification (tickmode array), while switching to Plotly auto ticks only during pan/zoom interactions and restoring the original ticks on axis reset. It also adjusts the rangeframe Plotly-side logic and updates tests/snapshots affected by the rendering changes.

Changes:

  • Add an adaptive_ggplotly_ticks() helper that injects JS to toggle axis tick behavior between ggplot-style ticks and Plotly auto ticks based on relayout events.
  • Rework the rangeframe JS so it aligns rangeframe extents to the rendered ticks (including after resets that restore ggplot tick arrays).
  • Update PlotPieChart tests and refresh vdiffr snapshots impacted by the plotting changes.

Reviewed changes

Copilot reviewed 3 out of 16 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/testthat/test-PlotPieChart.R Updates the non-polar test path and makes the “many slices” input deterministic; adds a macOS skip for that case.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-sequential.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-points-main.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-points-legend.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-evidence-main.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-bf-main.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-arrow-plain.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotRobustnessSequential/plotrobustnesssequential-arrow-parse.svg Updated snapshot after rendering changes.
tests/testthat/_snaps/PlotPieChart/plotpiechart-polar-no-ticks-no-legend.svg Updated snapshot after PlotPieChart output changes.
tests/testthat/_snaps/PlotPieChart/plotpiechart-polar-default.svg Updated snapshot after PlotPieChart output changes.
R/PlotPieChart.R Adjusts documentation and deprecation warning text around polarAxis.
R/convertGgplotToPlotly.R Adds adaptive tick JS injection and updates rangeframe JS to use rendered tick extents.
man/plotPieChart.Rd Keeps generated Rd text consistent with the updated roxygen docs.
Files not reviewed (1)
  • man/plotPieChart.Rd: Generated file

Comment thread R/convertGgplotToPlotly.R
Comment on lines +539 to +542
function approximatelyEqual(a, b) {
const scale = Math.max(1, Math.abs(a), Math.abs(b));
return Math.abs(a - b) <= 1e-9 * scale;
}
Comment thread R/convertGgplotToPlotly.R
Comment on lines +121 to +125
for (ax in xaxes)
plotlyplotje <- adaptive_ggplotly_ticks(plotlyplotje, ax)

if (grepl("l", sides))
for (ax in yaxes)
plotlyplotje$x$layout[[ax]]$tickmode <- "auto"
for (ax in yaxes)
plotlyplotje <- adaptive_ggplotly_ticks(plotlyplotje, ax)
Comment on lines +26 to +28
# set.seed(1)
# value_many <- rpois(25, 10)
# hardcoded output of rpois above after test failures on macos only
@vandenman vandenman merged commit aebeafe into jasp-stats:master Jul 2, 2026
11 checks passed
@vandenman vandenman deleted the fix/plotly_axes branch July 2, 2026 07:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants