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

时间:2017-04-24 14:39:12

标签: r plot ggplot2 facet

我想用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")

1 个答案:

答案 0 :(得分:-1)

这应该适合你。

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