R语言 如何使用gganimate制作折线图并显示标题和日期?

qq24tv8q  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(209)

我有这个数据集:

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" ...

状态:家庭和工作

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"))

我想在一个折线图上显示每小时的total_identy是如何根据工作和家庭而变化的。同时,我想在标题中显示时间。另外,通过添加shadow,我希望它最初显示的线图稍后变暗。我将时钟数据转换为POSIX格式。但当我用labs编写labs(title = paste0("Hour:", format({frame_time})时,我得到一个错误。它'这可能是因为hour变量的格式。我写了下面的代码:

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)

输出:

我该怎么解决呢?
谢谢你。

nhhxz33t

nhhxz33t1#

您可以使用frame_along代替frame_time,如下所示:

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)}")

创建于2023年1月18日,使用reprex v2.0.2

相关问题