Search code examples
rggplot2facetggplotly

ggplotly facet_wrap don't display some subplots in R


I am trying to make a ggplot with facet_wrap and for some reason some subplots don't display and appear blank some of the time.

library(plotly)
peak_season <- drop_na(read_csv(url("https://www.dropbox.com/s/wb8qmrcul197ypp/peak_season.csv?raw=1")))
    
ggplotly(
  ggplot(data = peak_season, aes(x = overall, frame = age)) +
    geom_bar(position = "identity") + facet_wrap(~league_name_clean)
)
# Need to add position = "identity" due to a well known bug in ggplotly and geom_bar.

Image of missing plots

The data is reachable to make the plot, as shown in this image. For Spain Primera Division there is definitely data for players in age 20.

ggplotly(
  ggplot(data = peak_season %>% filter(league_name_clean == "Spain Primera Division"), aes(x = overall, frame = age)) +
    geom_bar(position = "identity") + facet_wrap(~league_name_clean)
    )
# Just added a filter

Only the plot of one league

I made it work with gganimate, but I want my users to be able to have control and explore the data shown.

library(gganimate)    
ggplot(data = peak_season, aes(x = overall)) +
      geom_histogram(binwidth = 1) + facet_wrap(~league_name_clean) +
      transition_states(age, state_length = 1) +
      view_follow(fixed_y = TRUE) +
      labs(title = "Player peak age by league",
           y = "No. of players",
           subtitle = "Age: {closest_state}") 

gganimate

I tried using RStudio Cloud to roll out a bug in my computer but it happens the same.

Any ideas why this happens?

Thanks,


Solution

  • For anyone that might find this in the future, I think ggplotly isn't powerful enough to manage calculating so many plots + the frequencies for each overall - age - league, so I had to calculate it beforehand.

    t <- peak_season %>%
      select(overall, age, league_name_clean) %>%
      table() %>%
      as_tibble
    
    ggplotly(
      ggplot(data = t, aes(x = Overall, y = Count, frame = age)) +
        scale_x_discrete(breaks = seq(40, 100, by = 5)) +
        geom_col(position = "identity") + facet_wrap(~league_name_clean)
      )
    

    working ggplotly