Cannot create a BarChartRace with ggplot

Viewed 45

I have a dataframe that has daily data about Covid19 (such as: total_cases,total_deaths) in European countries (there are 49 countries in total). You can see a preview here and 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 or (this video) 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:

this

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!

1 Answers

I found that this code shows the top 10 countries based on their total_cases 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)

The result is here

Related