the dataset is
year | ID | top_1 | top_2 | top_3 |
---|---|---|---|---|
1952 | 25 | 7012 | 6647 | 6419 |
1953 | 28 | 8850 | 7165 | 6470 |
1954 | 31 | 8188 | 7678 | 7246 |
1955 | 34 | 8586 | 8485 | 7525 |
1956 | 37 | 8850 | 8516 | 8163 |
1957 | 40 | 7525 | 6501 | NA |
1958 | 43 | 8188 | 6165 | NA |
1959 | 46 | 6707 | 6187 | 5630 |
I want to get something like this
I am not sure what to put in aes()
argument
p <- ggplot( data = x , aes(year, y = ?? )
geom_bar( stat = "identity" )
transition_states(year)
labs(title = "abc")
coord_cartesian(ylim = c(5500 , 9000))
xlab("")
ylab("hgt")
animate(p, nframes = 450, fps=16)
CodePudding user response:
First you need to reshape your data using melt
. After that, you can set transition_states
to show different years of the barplot. To show the year in the title you can use {next_state}
. You can use the following code:
library(tidyverse)
library(gganimate)
library(reshape)
df_melted <- melt(df, id = c("ID", "year"))
p <- df_melted %>%
ggplot(aes(x = variable, y = value, fill = variable))
geom_bar(stat = "identity" )
transition_states(
year,
transition_length = 2,
state_length = 1
)
ease_aes('sine-in-out')
labs(title = "Heighest 3 peaks in Year: {next_state}")
xlab("")
ylab("hgt")
p
Output: