Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I pause the video and get the current bases? #136

Open
vapniks opened this issue Feb 10, 2025 · 1 comment
Open

How can I pause the video and get the current bases? #136

vapniks opened this issue Feb 10, 2025 · 1 comment

Comments

@vapniks
Copy link

vapniks commented Feb 10, 2025

I want to be able to pause the video and obtain the bases displayed at that point.
Can I do this? If not then this is a feature request. This seems like an important feature that has been overlooked.

@dicook
Copy link
Member

dicook commented Feb 10, 2025

Hi @vapniks there are several ways to manage this:

(1) with the tourr package it is a bit clunky to do, but can be managed with save_history() and followed by render_gif() to get the individual projections: see some code below as an example. This is my usual approach. But it might only give you close to the projection you especially wanted. The lack of interactivity directly in R is the hurdle.

(2) using the detourr package all of this could be possible, but requires some changes that could be requested.

(3) Using langevitour the information is available from the drop down menu, but it doesn't have all the tour paths available in the tourr package.

Image

Code examples for (1):

library(tourr)
library(mulgar)
library(ggplot2)
library(colorspace)
data("penguins_sub")
set.seed(1148)
p_t_grand <- save_history(penguins_sub[, 1:4], 
                    max_bases = 50)
render_gif(penguins_sub[, 1:4], 
           planned_tour(p_t_grand), 
           display_xy(col=penguins_sub$species), 
           gif_file = "NAME",
           frames=1000,
           width=400,
           height=400
           )
set.seed(1209)
p_t_guided <- save_history(
  penguins_sub[, 1:4], 
  guided_tour(lda_pp(penguins_sub$species)),
  max_bases = 50)
render_gif(penguins_sub[, 1:4], 
           planned_tour(p_t_guided), 
           display_xy(col=penguins_sub$species), 
           gif_file = "NAME",
           frames=1000,
           width=400,
           height=400
           )
best_proj <- matrix(p_t_guided[,,5], ncol=2)
p_t_radial <- save_history(
  penguins_sub[, 1:4], 
  radial_tour(best_proj, 3),
  max_bases = 3)
render_gif(penguins_sub[, 1:4], 
           planned_tour(p_t_radial), 
           display_xy(col=penguins_sub$species), 
           gif_file = "gifs/penguins_cl_radial.gif",
           frames=1000,
           width=400,
           height=400
           )

The Preview app on a Mac allows extracting the image associated with any projection.

And the tourr::render_proj() set up the data to recontruct any tour projection with base graphics or ggplot2. Here is how I use it with ggplots:

prj <- matrix(p_t_guided[,,1], ncol=2)
gd1 <- render_proj(penguins_sub[, 1:4], prj)
gd1$data_prj$species <- penguins_sub$species
gd1_plt <- plot_tour_projection(gd1)

where the plot_tour_projection() function is:

plot_tour_projection <- function(d) {
  plt <- ggplot() +
  geom_path(data=d$circle, aes(x=c1, y=c2)) +
  geom_segment(data=d$axes, 
               aes(x=x1, y=y1, xend=x2, yend=y2)) +
  geom_text(data=d$axes, aes(x=x2, y=y2,
                             label=rownames(d$axes))) +
  geom_point(data=d$data_prj, 
             aes(x=P1, y=P2, 
                 colour=species)) +
    xlim(-1,1) + ylim(-1, 1) +
    scale_color_discrete_divergingx(palette="Zissou 1") +
    theme_minimal() +
    theme(aspect.ratio=1,
       legend.position = "none",
       axis.text=element_blank(),
       axis.title=element_blank(),
       axis.ticks=element_blank(),
       panel.grid=element_blank(),
       panel.background = element_rect(fill=NA,
                                       colour="black"))
  plt
}

tourr doesn't depend on ggplot2 which is why the plot function is not a part of the package.

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

No branches or pull requests

2 participants