ggplot2错误“剧情中没有图层”

时间:2013-03-13 14:55:39

标签: r ggplot2

我已经看到问题已经问过了......并解决了将stat = "identity"添加到geom_bar的问题。 但在我的情况下,这并没有解决任何问题(我仍然得到消息“情节中没有图层”)。

我得到了一个简单的data.frame(data3),包含2个因子(MonthNB和StationNAME)和一个数字变量(Ptot):

   MonthNB StationNAME      Ptot
   1     stationA  21.70625
   2     stationA  16.19375
   3     stationA  16.64688
   4     stationA  27.37813
   5     stationA  38.26774
   6     stationA  52.91250
   7     stationA  69.36875
   8     stationA  43.18125
   9     stationA  33.24688
  10     stationA  35.74839
  11     stationA  36.01333
  12     stationA  30.24194
   1    stationB  25.14242
   2    stationB  18.62121
   3    stationB  22.11818
   4    stationB  32.70909
   5    stationB  33.83750
   6    stationB  63.65937
   7    stationB  69.05312
   8    stationB  50.70606
   9    stationB  46.96364
  10    stationB  50.28710
  11    stationB  46.81935
  12    stationB  39.88750

我尝试使用:

绘制Ptot = f(MonthNB)
d <- ggplot(data=data3, aes(x=MonthNB, y=Ptot, colour=StationNAME))
d + geom_line()
d

2 个答案:

答案 0 :(得分:31)

错误消息是由于您没有将d+geom_line()保存为对象。

#Save ggplot() as object
d <- ggplot(data=data3, aes(x=MonthNB, y=Ptot, colour=StationNAME))

#Add to d geom_line() - this makes the plot to appear on the screen but not saved.
d + geom_line()

将图层保存到对象

d<-d+geom_line()
#No error message
d

答案 1 :(得分:3)

错误是因为未添加geom_line()或geom_point()选项。您可以直接绘制它而不将其保存为添加此选项的对象。