thomasp85 / ggforce

Accelerating ggplot2
https://ggforce.data-imaginist.com
Other
913 stars 105 forks source link

gganimate + ggforce (Parallel Set) #235

Open chalg opened 4 years ago

chalg commented 4 years ago

I'm not sure if anyone has tried to animate the ggforce geom_parallel_sets with gganimate? I tried the below, but ran into an error. It would be nice to get this working as from memory it is not possible to animate sankey or alluvial plots via the ggalluvial either. I'm not sure if this is a bug or syntax error as I haven't seen any examples online.

# Required libraries
require("tidyverse")
#> Loading required package: tidyverse
#> Warning: package 'dplyr' was built under R version 3.6.1
require("lubridate")
#> Loading required package: lubridate
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
require("scales")
#> Loading required package: scales
#> 
#> Attaching package: 'scales'
#> The following object is masked from 'package:purrr':
#> 
#>     discard
#> The following object is masked from 'package:readr':
#> 
#>     col_factor
require("gganimate")
#> Loading required package: gganimate
require("ggforce")
#> Loading required package: ggforce
#> Warning: package 'ggforce' was built under R version 3.6.1

# Read in tibble
top_africa_gen <- tibble::tribble(
  ~country,    ~generation, ~year,   ~value,
  "Algeria", "Fossil fuels",  2016,    70663,
  "Algeria", "Fossil fuels",  2017,  73741.1,
  "Algeria", "Fossil fuels",  2018,  77076.9,
  "Algeria",      "Nuclear",  2016,        0,
  "Algeria",      "Nuclear",  2017,        0,
  "Algeria",      "Nuclear",  2018,        0,
  "Algeria",   "Renewables",  2016,      334,
  "Algeria",   "Renewables",  2017,    345.7,
  "Algeria",   "Renewables",  2018,    357.9,
  "Egypt", "Fossil fuels",  2016,   178641,
  "Egypt", "Fossil fuels",  2017, 180692.1,
  "Egypt", "Fossil fuels",  2018,   182833,
  "Egypt",      "Nuclear",  2016,        0,
  "Egypt",      "Nuclear",  2017,        0,
  "Egypt",      "Nuclear",  2018,        0,
  "Egypt",   "Renewables",  2016,  15716.8,
  "Egypt",   "Renewables",  2017,  15925.5,
  "Egypt",   "Renewables",  2018,    16137,
  "Libya", "Fossil fuels",  2016,    36422,
  "Libya", "Fossil fuels",  2017,  37404.5,
  "Libya", "Fossil fuels",  2018,  38435.2,
  "Libya",      "Nuclear",  2016,        0,
  "Libya",      "Nuclear",  2017,        0,
  "Libya",      "Nuclear",  2018,        0,
  "Libya",   "Renewables",  2016,        8,
  "Libya",   "Renewables",  2017,      8.3,
  "Libya",   "Renewables",  2018,      8.5,
  "Morocco", "Fossil fuels",  2016,    25776,
  "Morocco", "Fossil fuels",  2017,    26745,
  "Morocco", "Fossil fuels",  2018,  27293.9,
  "Morocco",      "Nuclear",  2016,        0,
  "Morocco",      "Nuclear",  2017,        0,
  "Morocco",      "Nuclear",  2018,        0,
  "Morocco",   "Renewables",  2016,     5066,
  "Morocco",   "Renewables",  2017,   5143.5,
  "Morocco",   "Renewables",  2018,   5204.5,
  "Nigeria", "Fossil fuels",  2016,    25311,
  "Nigeria", "Fossil fuels",  2017,  25748.1,
  "Nigeria", "Fossil fuels",  2018,  26204.9,
  "Nigeria",      "Nuclear",  2016,        0,
  "Nigeria",      "Nuclear",  2017,        0,
  "Nigeria",      "Nuclear",  2018,        0,
  "Nigeria",   "Renewables",  2016,   5667.4,
  "Nigeria",   "Renewables",  2017,   6019.9,
  "Nigeria",   "Renewables",  2018,   6392.2,
  "South Africa", "Fossil fuels",  2016,   226667,
  "South Africa", "Fossil fuels",  2017,   231630,
  "South Africa", "Fossil fuels",  2018,   236718,
  "South Africa",      "Nuclear",  2016,    15026,
  "South Africa",      "Nuclear",  2017,  15216.8,
  "South Africa",      "Nuclear",  2018,    15410,
  "South Africa",   "Renewables",  2016,    11054,
  "South Africa",   "Renewables",  2017,  11412.4,
  "South Africa",   "Renewables",  2018,  11715.7
)

africa_gen_parallel <- gather_set_data(top_africa_gen, 1:2)
head(africa_gen_parallel)
#> # A tibble: 6 x 7
#>   country generation    year  value    id x       y      
#>   <chr>   <chr>        <dbl>  <dbl> <int> <chr>   <chr>  
#> 1 Algeria Fossil fuels  2016 70663      1 country Algeria
#> 2 Algeria Fossil fuels  2017 73741.     2 country Algeria
#> 3 Algeria Fossil fuels  2018 77077.     3 country Algeria
#> 4 Algeria Nuclear       2016     0      4 country Algeria
#> 5 Algeria Nuclear       2017     0      5 country Algeria
#> 6 Algeria Nuclear       2018     0      6 country Algeria

# animate attempt
p <- africa_gen_parallel %>%
  ggplot(aes(x, id = id, split = y, value = value)) +
  geom_parallel_sets(aes(fill = generation), alpha = 0.3, axis.width = 0.4) +
  geom_parallel_sets_axes(axis.width = 0.5) +
  geom_parallel_sets_labels(colour = 'white', angle = 0, size = 3.25) +
  scale_y_continuous(labels = scales::comma) +
  theme_minimal() +
  theme(axis.text.x = element_text(size = 9, angle = 00),
        strip.text.x = element_text(size = 10.5, colour = "darkgreen"),
        axis.title = element_text(size = 10), legend.position = "none",
        plot.caption = element_text(color = "gray50", face = "italic"),
        legend.title = element_blank()) +
  labs(x = NULL, y = "Electricity Generation (GWh)",
       title = "African Electricity Generation 2011-2018 (Top 6 countries)",
       caption = "Source: @GrantChalmers | https://africa-energy-portal.org/database") +
  #------gganimate part (not working)
  labs(title = "{frame_time}") +
  transition_states(as_factor(year),
                    transition_length = 3,
                    state_length = 1)

# Render animation
animate(p, nframes = 30)
#> Warning: Computation failed in `stat_parallel_sets()`:
#> id must keep grouping across data
#> Warning: Computation failed in `stat_parallel_sets_axes()`:
#> id must keep grouping across data

#> Warning: Computation failed in `stat_parallel_sets_axes()`:
#> id must keep grouping across data
#> Error in mapply(FUN = f, ..., SIMPLIFY = FALSE): zero-length inputs cannot be mixed with those of non-zero length

Created on 2019-10-25 by the reprex package (v0.3.0)

hongyuanjia commented 4 years ago

Same problem here. Really wish to combine ggforce with gganimate to plot a dynamic parallel sets.

hongyuanjia commented 4 years ago

As @thomasp85 said in in https://github.com/thomasp85/gganimate/issues/327

In general, any geom that doesn’t work with gganimate should get fixed at the geom side — I don’t want to special case any geoms in the gganimate code

Probably you can open an issue in the ggforce repo.

chalg commented 4 years ago

Thanks for finding that out, @thomasp85 asked me to raise it here, but perhaps he forgot about the other comment. I'll open an issue in the ggforce repo.

thomasp85 commented 4 years ago

No, this is the place - I just haven't got time to look at it yet

rdatasculptor commented 4 years ago

I don't mean to be impatient, but any news on this issue?

thomasp85 commented 4 years ago

No - my priorities have been elsewhere. I will circle back to gganimate sometime this year, but PR's are welcome in the meantime

jessexknight commented 2 years ago

polite ping... any update? Thanks,