0

I tried to use view_follow() with no success... I want to display the chart with a smaller scale and change the axis-x according the data...

How can I adjust the axis-x dynamically only if the values overflow a preset value or the chart?

library(tidyverse)
library(gganimate)
library(gapminder)
theme_set(theme_classic())

gdp <- read.csv("https://raw.github.com/datasets/gdp/master/data/gdp.csv")
words <- scan(
  text="world income only total dividend asia euro america africa oecd",
  what= character())
pattern <- paste0("(",words,")",collapse="|")
gdp  <- subset(gdp, !grepl(pattern, Country.Name , ignore.case = TRUE))
colnames(gdp) <- gsub("Country.Name", "country", colnames(gdp))
colnames(gdp) <- gsub("Country.Code", "code", colnames(gdp))
colnames(gdp) <- gsub("Value", "value", colnames(gdp))
colnames(gdp) <- gsub("Year", "year", colnames(gdp))

gdp$value <- round(gdp$value/1e9)

gap <- gdp %>%
  group_by(year) %>%
  # The * 1 makes it possible to have non-integer ranks while sliding
  mutate(rank = min_rank(-value) * 1,
         Value_rel = value/value[rank==1],
         Value_lbl = paste0(" ",value)) %>%
  filter(rank <=10) %>%
  ungroup()

p <- ggplot(gap, aes(rank, group = country, fill = as.factor(country), color = as.factor(country))) +
     geom_tile(aes(y = value/2, height = value, width = 0.9), alpha = 0.8, color = NA) +
     geom_text(aes(y = 0, label = paste(country, " ")), vjust = 0.2, hjust = 1) +
     geom_text(aes(y=value,label = Value_lbl, hjust=0)) +
     coord_flip(clip = "off", expand = FALSE) +
     scale_y_continuous(labels = scales::comma) +
     scale_x_reverse() +
     guides(color = FALSE, fill = FALSE) +

  labs(title='{closest_state}', x = "", y = "GDP in billion USD",
       caption = "Sources: World Bank | Plot generated by Nitish K. Mishra @nitishimtech") +
  theme(plot.title = element_text(hjust = 0, size = 32),
        axis.ticks.y = element_blank(),  # These relate to the axes post-flip
        axis.text.y  = element_blank(),  # These relate to the axes post-flip
        plot.margin = margin(1,1,1,4, "cm")) +

  transition_states(year, transition_length = 4, state_length = 1) +
  ease_aes('cubic-in-out') 


animate(p, 200, fps = 10, duration = 10, width = 600, height = 400, renderer = gifski_renderer("plot.gif"))
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Bnknews
  • 11
  • 5
  • Can you please make a [mcve] of your problem? At the least, you need to include your data, but your more likely to get a solution if you can pare down your code to the minimum amount needed to reproduce your problem. [How to make a great R reproducible example](https://stackoverflow.com/q/5963269/8366499) – divibisan Mar 29 '19 at 21:50
  • 1
    Hi @divibisan, if you install the packages: tidyverse, gganimate, gapminder and their dependencies... you will able to execute the code, my source is a csv (https://raw.github.com/datasets/gdp/master/data/gdp.csv) if you run the code it will save a gif with the chart... – Bnknews Mar 29 '19 at 21:55
  • 1
    @Bnknews The point isn't about required packages, it's this: *we are often hesitant to download data from a link provided by a stranger over the internet*. Granted, sometimes someone will be willing to jump through such hoops to help, but your chances are better if you make it easier for people to reproduce your problem **from the question itself**. – Z.Lin Mar 30 '19 at 03:39

0 Answers0