我有一个for循环来创建一系列图形,但始终收到上面指出的错误(“美学必须为长度1或与数据(1)相同:x,y,颜色”)。当我运行不带循环的代码时,它可以正常运行。
tmp.df <- melt(data,
id.vars = c("Period", "source", "Bsl_Cat", "scenario"),
measure.vars = c("tot.loss", "freq"),
na.rm = TRUE)
for(i in seq_along(UoM)) {
ggplot() +
geom_line(data = subset(tmp.df,
source == source1 &
scenario == scenario &
tmp.df$Bsl_Cat == "CPBP" &
variable == "freq"),
aes(y = value, x = Period, color = source1)) +
geom_line(data = subset(tmp.df,
source == source2 &
scenario == scenario &
tmp.df$Bsl_Cat == "CPBP" &
variable == "freq"),
aes(y = value, x = Period, color = source2)) +
labs(x="Date", y = "Frequency per Quarter") +
scale_color_manual(labels = c("USAA", "ABA"), values = mycolors2) +
ggtitle(paste(UoM[i])) +
theme(plot.title = element_text(face = "plain")) +
theme(legend.direction = "horizontal",
legend.position = "bottom",
legend.title = element_blank())
}
任何建议将不胜感激!