ggplot具有“美学必须为长度1或与数据(1)相同:x,y,color”错误

时间:2019-01-10 19:56:11

标签: r for-loop ggplot2

我有一个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()) 
}

任何建议将不胜感激!

0 个答案:

没有答案