2017-04-24 96 views
0

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

我想使用此代码facett我的数据:

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") 
+0

我不能跟的r 3.3.2版本(2016年10月31日)'和'GGPLOT2 2.2.0'重现你的错误。你在什么版本上? –

+0

R版本3.3.2(2016-10-31)和ggplot v2.1.0。我使用Microsoft R客户端 – Kaja

+3

您的示例数据仅包含一个ID,因此无法检查facet_warp失败的位置。顺便说一句:你不必在facet_wrap中添加res [facet_wrap(〜ITEMGROUPID)' – timfaber

回答

-2

这应该为你做的伎俩。

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