1

The ggiraph package extends ggplot2 to add interactivity. One feature is the ability to zoom in on a plot, for example using the following code:

library(ggplot2)
library(ggiraph)

data(mtcars)

mtcars$model <- rownames(mtcars)

gg <- ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_point_interactive(aes(color = as.factor(cyl),
                             tooltip = model))

girafe(ggobj = gg) %>%
    girafe_options(opts_zoom(max = 5),
                   opts_tooltip(use_fill = TRUE))

When previewing the chart is is possible to zoom in by clicking the magnifying glass and scrolling. As the user zooms, the points enlarge, but the tooltip stays the same size.

Is it possible to adjust the geom sizes (e.g., point radius or line thickness) when zooming in?

There are probably lots of reasons this would be useful, but I'm specifically thinking about overplotting - I have a dense dataset but zooming in doesn't help because the points still cover one another.

I'm using ggiraph because it is a simple extension to ggplot2 but if there is a comparable package I would be open to other solutions.

Andrew Jackson
  • 823
  • 1
  • 11
  • 23

1 Answers1

0

First of all, Your code is not reproducible as model is not available so I assume that you have assigned the row names of mtcars to the vector model.

I don't think that You can do this in ggiraph but it's straightforward in plotly: You may generate a ggplot object and simply convert it to a plotly object using plotly::ggplotly().

For the mtcars example in Your question, the following does the trick:

library(plotly)

data(mtcars)

# generate ggplot object
gg <- ggplot(mtcars) + 
  geom_point(aes(x = mpg, y = disp, col  = as.factor(cyl), text = rownames(mtcars))) 

# convert to plotly object
ggplotly(gg, tooltip = "text")

The result looks like a ggplot2 but is interactive and does not suffer from the zooming issue. You may play with it in the viewer, include it in output formats that support HTML widgets or view it in the browser.

enter image description here

As the above is just sceenshot I uploaded the output to my account on plot.ly. You can find it here and play around with it.

I hope this helps!

Martin C. Arnold
  • 9,483
  • 1
  • 14
  • 22