Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 error "no layers in plot"

Tags:

r

ggplot2

I have seen the question already asked... and solved adding stat = "identity" to geom_bar. But in my case, this does not solve anything (I still get the message "no layers in plot").

I got a simple data.frame (data3) with 2 factors (MonthNB and StationNAME) and one numerical variable (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

I tried to plot Ptot=f(MonthNB) using:

d <- ggplot(data=data3, aes(x=MonthNB, y=Ptot, colour=StationNAME))
d + geom_line()
d
like image 696
user2165907 Avatar asked Mar 13 '13 14:03

user2165907


Video Answer


2 Answers

The error message is due to fact that you didn't save d+geom_line() as an object.

#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()

To save layer to object

d<-d+geom_line()
#No error message
d
like image 179
Didzis Elferts Avatar answered Oct 03 '22 06:10

Didzis Elferts


the error was because the geom_line() or geom_point() option was not added. You can directly plot it without saving it as object on adding this option.

like image 20
Zaira Avatar answered Oct 03 '22 06:10

Zaira