Home > Blockchain >  How do I make a line chart and showing title with Date using gganimate?
How do I make a line chart and showing title with Date using gganimate?

Time:01-18

I have this dataset:

str(combined_data)
Output:
tibble [48 × 3] (S3: tbl_df/tbl/data.frame)
 $ hour        : chr [1:48] "2022/02/08 00:00:00" "2022/02/08 01:00:00" "2022/02/08 02:00:00" "2022/02/08 03:00:00" ...
 $ total_identy: num [1:48] 660512 691496 713455 719447 721466 ...
 $ statu       : chr [1:48] "home" "home" "home" "home" ...

statu: home and work

dput(combined_data)
    
structure(list(hour = structure(c(1644267600, 1644271200, 1644274800, 
1644278400, 1644282000, 1644285600, 1644289200, 1644292800, 1644296400, 
1644300000, 1644303600, 1644307200, 1644310800, 1644314400, 1644318000, 
1644321600, 1644325200, 1644328800, 1644332400, 1644336000, 1644339600, 
1644343200, 1644346800, 1644350400, 1644267600, 1644271200, 1644274800, 
1644278400, 1644282000, 1644285600, 1644289200, 1644292800, 1644296400, 
1644300000, 1644303600, 1644307200, 1644310800, 1644314400, 1644318000, 
1644321600, 1644325200, 1644328800, 1644332400, 1644336000, 1644339600, 
1644343200, 1644346800, 1644350400), class = c("POSIXct", "POSIXt"
), tzone = ""), total_identy = c(660512, 691496, 713455, 719447, 
721466, 721698, 710649, 680721, 597052, 487964, 370763, 246387, 
125803, 45555, 6002, 0, 13056, 64378, 143761, 215388, 272781, 
347771, 462434, 568251, 140767, 109783, 87824, 81832, 79813, 
79581, 90630, 120558, 204227, 313315, 430516, 554892, 675476, 
755724, 795277, 801279, 788223, 736901, 657518, 585891, 528498, 
453508, 338845, 233028), statu = c("home", "home", "home", "home", 
"home", "home", "home", "home", "home", "home", "home", "home", 
"home", "home", "home", "home", "home", "home", "home", "home", 
"home", "home", "home", "home", "work", "work", "work", "work", 
"work", "work", "work", "work", "work", "work", "work", "work", 
"work", "work", "work", "work", "work", "work", "work", "work", 
"work", "work", "work", "work")), row.names = c(NA, -48L), class = c("tbl_df", 
"tbl", "data.frame"))

I want to show how the hourly total_identy changes according to work and home on a line graph. At the same time, I want to show the time in the header. Also, by adding shadow, I want the line graphs that it showed at first to appear dimmed later on. I converted the clock data to POSIXct format. But when I write labs(title = paste0("Hour:", format({frame_time}) with labs) I get an error. It's probably because of the format of the hour variable. I wrote the following code:

combined_data$hour <- as.POSIXct(combined_data$hour, format = "%Y/%m/%d %H:%M:%S")

# Plot

combined_data %>%
  ggplot(aes(x = hour, y = total_identy, group = statu, color = statu, linetype="solid"))  
  geom_line(alpha = 0.8, lineend = "round")  
  geom_point()  
  labs(title = "Hour: {frame_time}")  
  theme(plot.background = element_rect(fill = "black"),
        panel.background = element_rect(fill = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "none",
        plot.title = element_text(color = "white"))  
  gganimate::transition_reveal(hour)

output:

enter image description here

How can I solve it?

Thank you.

CodePudding user response:

You could use frame_along instead of frame_time like this:

library(ggplot2)
library(dplyr)
library(gganimate)
combined_data %>%
  ggplot(aes(x = hour, y = total_identy, group = statu, color = statu, linetype="solid"))  
  geom_line(alpha = 0.8, lineend = "round")  
  geom_point()  
  theme(plot.background = element_rect(fill = "black"),
        panel.background = element_rect(fill = "black"),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.line = element_blank(),
        axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        legend.position = "none",
        plot.title = element_text(color = "white"))  
  gganimate::transition_reveal(hour)  
  labs(title = "Hour: {(frame_along)}") 

Created on 2023-01-18 with reprex v2.0.2

  • Related