至少一层必须包含用于分面的所有变量

我想用 ggplot2 绘制我的数据。我的数据结构如下所示:

 str(res)
'data.frame':   1161 obs. of  4 variables:
 $ gesamt      : num  66.6 32.9 52.5 23.9 17.7 ...
 $ ITEMGROUPID : chr  "1011113" "1011113" "1011113" "1011113" ...
 $ Salesname   : Factor w/ 2 levels "Hornbach Baumarkt CZ spol. s r.o.",..: 2 2 2 2 2 2 2 2 2 2 ...
 $ deliverydate: POSIXct, format: "2014-01-07" "2014-01-31" "2014-02-10" "2014-03-06" ...

我想用这段代码来分面我的数据:

ggplot(data = res, aes(x = res$deliverydate, y = res$gesamt, colour = res$Salesname)) +
geom_point() +
facet_wrap(~res$ITEMGROUPID)

但我收到此错误:

Error in layout_base(data, vars, drop = drop) : 
  At least one layer must contain all variables used for facetting

我应该怎么做才能解决这个问题?

一个可重现的例子:

> dput(head(res))
structure(list(gesamt = c(66.6, 32.86, 52.54, 23.89, 17.74, 45.05
), ITEMGROUPID = c("1011113", "1011113", "1011113", "1011113", 
"1011113", "1011113"), Salesname = structure(c(2L, 2L, 2L, 2L, 
2L, 2L), .Label = c("Hornbach Baumarkt CZ spol. s r.o.", "Possling GmbH & Co. KG"
), class = "factor"), deliverydate = structure(c(1389049200, 
1391122800, 1391986800, 1394060400, 1394751600, 1395874800), class = c("POSIXct", 
"POSIXt"), tzone = "")), .Names = c("gesamt", "ITEMGROUPID", 
"Salesname", "deliverydate"), row.names = c(NA, 6L), class = "data.frame")
stack overflow At least one layer must contain all variables used for facetting
原文答案

答案:

作者头像

这应该为您解决问题。

ggplot(data = res, aes(x = deliverydate, y = gesamt, colour = Salesname)) +
geom_point() + facet_wrap(~ITEMGROUPID)