Cannot create a BarChartRace with ggplot

Hello everyone,

I have a dataframe that has daily data about Covid19 (such as: total_cases,total_deaths) in some European countries (there are 49 countries in total). You can have the whole dataframe here. I want to create a Bar Chart Race for the variable total_cases for all the European countries with ggplot. So, I followed the steps from this link and I wrote the below code:

library(ggplot2)
g1 = ggplot(data = data.europe,
aes(x = as.Date(date),y = total_cases,group = location,
color = location)) + geom_line(size = 0.5) + 
labs(y = "Total Cases", x = "Date") + 
theme(legend.position = "bottom",legend.box = "vertical",
legend.title = element_blank(),
legend.text = element_text(size = 10))

Then I wrote the below code in order to create the dynamic plot

g1_star = ggplot(data = data.europe,
aes(x = as.Date(date),y = total_cases,group = location,
color = location)) + geom_line(aes(group = as.Date(date)),linetype=1) + 
labs(y= "Total Cases", x = "Date") + 
theme(legend.position = "bottom",legend.box = "vertical",
legend.title = element_blank(),
legend.text = element_text(size = 10)) + 
transition_reveal(as.Date(date))

#We wil create the an animation
library(gifski)
library(gganimate)
animate(g1_star,height= 538,width = 866)
data_star = data.europe %>% group_by(as.Date(date))

However when I wrote these lines:

g1_star_anim = ggplot(data_star,aes(x = as.Date(date),
                                y = total_cases,
                                group = location,
                                fill = location,
                                color = location)) + 
geom_tile(aes(height = total_cases,width = 0.9), alpha = 0.8,color = NA) + 
geom_text(aes(y = 0, label = paste(location, " ")), vjust = 0.2, hjust = 1) + 
scale_y_continuous(labels = scales::comma) + theme(axis.line=element_blank())

anim1 = g1_star_anim + transition_states(as.Date(date), transition_length = 4, 
state_length = 1) + 
view_follow(fixed_x = TRUE) + 
labs(title = 'Total_cases per year')

The result is:
Uipgu

which isn't expected.

What should I change? Or which code should I write? Can anyone help me because I have been searching for a very long time?

Thanks in advance!

I'm not familiar with gganimate. But in your reference, it does not use date as the xaxis in the staticplot. Perhaps you need to change your x in your aes()

I wrote the below code and I got the result I wanted

library(gganimate)
library(hrbrthemes)
library(tidyverse)

data.europe.not.na.star = data.europe.not.na %>%
  group_by(as.Date(date)) %>%
  arrange(-total_cases) %>%
  mutate(rank = row_number()) %>%
  filter(rank<=10)

col = c("cadetblue1","aquamarine","chocolate1","gray13","blue3",
        "darkgoldenrod2","darkolivegreen1","darkorchid1","lightcoral","deeppink",
        "greenyellow","mediumvioletred","midnightblue","olivedrab1","mediumaquamarine",
        "red","seagreen1")

p = data.europe.not.na.star %>%
  ggplot(aes(x = -rank,y = total_cases, group = location)) +
  geom_tile(aes(y = total_cases / 2, height = total_cases,fill = location), width = 0.9) +
  geom_text(aes(label = location), hjust = "right", colour = "gold", fontface = "bold", nudge_y = -100000) +
  geom_text(aes(label = scales::comma(total_cases)), hjust = "left", nudge_y = 100000, colour = "grey30") +
  coord_flip(clip="off") +
  scale_fill_manual(name = 'location', values = col) +
  scale_x_discrete("") +
  scale_y_continuous("",labels=scales::comma) +
  hrbrthemes::theme_ipsum(plot_title_size = 32, subtitle_size = 24, caption_size = 20, base_size = 20) +
  theme(panel.grid.major.y=element_blank(),
        panel.grid.minor.x=element_blank(),
        plot.margin = margin(1,1,1,2,"cm"),
        axis.text.y=element_blank()) +
  # gganimate code to transition by year:
  transition_time(as.Date(date)) +
  ease_aes('cubic-in-out') +
  labs(title='Bar Char Race of Total Cases in Europe(Top 10)',
       subtitle='Total Cases in {round(frame_time,0)}')

animate(p, nframes = 750, fps = 25, end_pause = 50, width = 1200, height = 900)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.