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

Using {frame_time} in the title for year shows non-integers #295

Closed
jzadra opened this issue Feb 18, 2019 · 7 comments
Closed

Using {frame_time} in the title for year shows non-integers #295

jzadra opened this issue Feb 18, 2019 · 7 comments

Comments

@jzadra
Copy link

jzadra commented Feb 18, 2019

When I use transition_time(year) and put "year: {year}" in the title, I get titles like "2017.285183", "2017.486" etc, building up through non-integers.

@jzadra
Copy link
Author

jzadra commented Feb 19, 2019

I figured this out. If the numeric value being used, ie year, is type double, you will get this behavior. If it is type integer, it works correctly.

If this isn't intended, might be good to treat variables that could be integer as integer.

Example:

library(gapminder)
library(gganimate)
#> Loading required package: ggplot2
library(tidyverse)

gapminder %>% 
  mutate(year = as.numeric(year)) %>% 
  ggplot(aes(gdpPercap, lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')

Created on 2019-02-19 by the reprex package (v0.2.1)

@UNFPAmaldives
Copy link

The solution, I read elsewhere, is to change your year variable to integer, and then, instead of transition_time, use transition_states, e.g.:

labs(title = 'Year: {closest_state}', x = 'GDP per capita', y = 'life expectancy (years)') +
transition_states(year, transition_length = 1, state_length = 1) +
ease_aes('linear')  

@thomasp85
Copy link
Owner

No need for that — just put ”Year: {as.integer(frame_time)}” as your title

@KucherP
Copy link

KucherP commented Apr 23, 2019

But, if I need to show not year (like 2012, 2013 etc), but period (2012-1, 2012-2 etc). How to do that?

@thomasp85
Copy link
Owner

However you’d usually do this with R code. You can basically stick whatever code inside {} that returns something coercive to a character

@Deleetdk
Copy link

Deleetdk commented Jun 8, 2019

Simple solution is to use "Year: {format(frame_time, '%Y-%m')}" which gives you stuff like 2012-1, 2012-2.

@highclef93
Copy link

Hello, I think I have a similar problem, I want to add evolving date and time(minutes) to the title of my ggplot animate,
I'm new to R, I need help please

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

6 participants