我有 geom_line 图(使用空气质量数据作为可重复的例子)显示温度变化,每月一条线,然后以这种方式 gganimate:
library("tidyverse")
library("gganimate")
data("airquality")
ggplot(airquality, aes(Day, Temp, color = Month)) +
geom_line(size = 2) +
geom_dl(aes(label = Month), method = list(dl.trans(x = x + 0.1, y = y + 0.25), "last.points", fontface = "bold")) +
transition_time(Month) +
labs(title = 'Month is {frame_time}') +
shadow_mark(aes(color = Month),size=1, alpha=0.7, past=T, future=F) +
geom_path(aes(color = Month), size = 1)
渲染此动画:
我的主要问题是实现相同但显示月份名称而不是数字(允许我在标签和标题中放置月份名称)并摆脱连接每行开头和结尾的直线。我试过这个(到目前为止没有成功):
aq <- airquality %>%
dplyr::mutate(Month = month.name[Month])
ggplot(aq, aes(Day, Temp, color = Month)) +
geom_line( size = 1) +
geom_dl(aes(label = Month), method = list(dl.trans(x = x + 0.1, y = y + 0.25), "last.points", fontface = "bold")) +
transition_time(Month) +
labs(title = month.name['{frame_time}']) +
shadow_mark(size = 1, colour = 'grey') +
geom_path(aes(group = Month), size = 1)
Error: time data must either be integer, numeric, POSIXct, Date, difftime, orhms
In addition: Warning messages:
1: In min(cl[cl != 0]) : no non-missing arguments to min; returning Inf
2: In min(cl[cl != 0]) : no non-missing arguments to min; returning Inf
3: In min(cl[cl != 0]) : no non-missing arguments to min; returning Inf