charToDate(x) 中的 Ggplot 错误:字符串在 R 中不是标准的明确格式
Posted
技术标签:
【中文标题】charToDate(x) 中的 Ggplot 错误:字符串在 R 中不是标准的明确格式【英文标题】:Ggplot Error in charToDate(x) : character string is not in a standard unambiguous format in R 【发布时间】:2021-12-30 20:05:34 【问题描述】:我在 for 循环中使用 Ggplot 绘制五个不同的时间序列图,每个图中有两条线。
我写了以下代码(对不起,代码太长了):
country_names <- c("Norway", "Spain", "India", "Australia", "United states")
M <- length(country_names)
for (m in 1:M)
print(ggplot() +
geom_line(data = as.data.frame(gdp_cntrs[[m]][13:length(gdp_cntrs[[m]])]),
aes(x = as.Date(as.yearqtr(time(gdp_cntrs[[m]]))[13:length(gdp_cntrs[[m]])]),
y = gdp_cntrs[[m]][13:length(gdp_cntrs[[m]])], colour = "GDP %")) +
geom_line(data = as.data.frame(forcasts_ar[[m]]), aes(x =
as.Date(as.yearqtr(time(gdp_cntrs[[m]]))[13:length(gdp_cntrs[[m]])]),
y = forcasts_ar[[m]], colour = "AR-1 model")) +
scale_colour_manual(values = c("blue", "black")) +
ylab("Changes %") +
ggtitle(paste("AR-1 model in", country_names[m])) +
labs(y = "Changes %",
x = "Time",
colour = "Series:") +
theme_minimal() +
theme(legend.position = c(1.15, 0.6)) +
scale_x_yearmon(format="%Y-%m", n=6) +
theme(plot.margin = unit(c(1, 5, 1, 1), "cm")))
但是,当我使用scale_x_yearmon(format="%Y-%m", n=6)
我收到以下错误:
Error in charToDate(x) :
character string is not in a standard unambiguous format
没有scale_x_yearmon(format="%Y-%m", n=6)
,代码运行流畅。
我不明白这个错误,因为 aes(x = ) 显然是一个日期变量。 为澄清起见(我每 m 都检查一次):
> for (m in 1:M)
x <- as.Date(as.yearqtr(time(gdp_cntrs[[m]]))[13:length(gdp_cntrs[[m]])])
str(x)
Date[1:59], format: "2007-01-01" "2007-04-01" "2007-07-01" "2007-10-01" "2008-01-01" ...
Date[1:59], format: "2007-01-01" "2007-04-01" "2007-07-01" "2007-10-01" "2008-01-01" ...
Date[1:59], format: "2007-01-01" "2007-04-01" "2007-07-01" "2007-10-01" "2008-01-01" ...
Date[1:59], format: "2007-01-01" "2007-04-01" "2007-07-01" "2007-10-01" "2008-01-01" ...
Date[1:59], format: "2007-01-01" "2007-04-01" "2007-07-01" "2007-10-01" "2008-01-01"
如果有人能帮我解决这个问题,我将不胜感激?
【问题讨论】:
请定义gdp_cntrs
和forcasts_ar
。谢谢。
> str(gdp_cntrs[[1]]) 时间序列 [1:71, 1] 从 2004 年到 2022 年:1.3 0.9 1.1 1.5 1 -1.2 1.4 0.3 2.5 0.2 ... - attr( *, "dimnames")=List of 2 ..$ : NULL ..$ : chr "...1" ```` > str(forecasts_ar[[1]]) num [1:59] 0.908 0.53 0.77 1.046 0.947 ... 所以,gdp_cntr 是一个时间序列列表,预测是数字
【参考方案1】:
我发现如果使用as.yearmon()
而不是as.Date()
,则不会出现错误,并且循环将打印启用scale_x_yearmon(format="%Y-%m", n=6)
选项的图。
所以,我的解决方案是将as.Date()
替换为as.yearmon()
for (m in 1:M)
print(ggplot() +
geom_line(data = as.data.frame(gdp_cntrs[[m]][13:length(gdp_cntrs[[m]])]),
aes(x = as.yearmon(as.yearqtr(time(gdp_cntrs[[m]]))[13:length(gdp_cntrs[[m]])]),
y = gdp_cntrs[[m]][13:length(gdp_cntrs[[m]])], colour = "GDP %")) +
geom_line(data = as.data.frame(forcasts_ar[[m]]), aes(x =
as.yearmon(as.yearqtr(time(gdp_cntrs[[m]]))[13:length(gdp_cntrs[[m]])]),
y = forcasts_ar[[m]], colour = "AR-1 model")) +
scale_colour_manual(values = c("blue", "black")) +
ylab("Changes %") +
ggtitle(paste("AR-1 model in", country_names[m])) +
labs(y = "Changes %",
x = "Time",
colour = "Series:") +
theme_minimal() +
theme(legend.position = c(1.15, 0.6)) +
scale_x_yearmon(format="%Y-%m", n=6) +
theme(plot.margin = unit(c(1, 5, 1, 1), "cm")))
【讨论】:
以上是关于charToDate(x) 中的 Ggplot 错误:字符串在 R 中不是标准的明确格式的主要内容,如果未能解决你的问题,请参考以下文章