0

So I have been trying to plot the following data frame :

head(MonthlyAveragePriceHAIRPEPE)

  month   AveragePrice pct.chg

1 2016-09        3.17   NA    
2 2016-10        0.792  -0.750
3 2016-11        0.225  -0.715
4 2016-12        0.179  -0.204
5 2017-01        0.445   1.48 
6 2017-02        3.36    6.55 

The problem is that, when I plot with the following line of code plot(MonthlyAveragePriceHAIRPEPE$month, MonthlyAveragePriceHAIRPEPE$AveragePrice), I get the following :

Error in plot.window(...) : need finite 'xlim' values. 
In addition : Warning messages : 
1: In min(x) : no non-missing arguments to min; returning Inf
2: In max(x) : no non-missing arguments to max; returning -Inf

I don't know how to solve those problems... I think it comes from the class of the month column, which is "character", but when I try to pass it to "factor", all I get are NA's... The class of the Average price is numeric and I get no problem plotting it alone. I think the problem really concerns the "month" column and its class...

The infos of the df are the following :

dput(head(MonthlyAveragePriceHAIRPEPE, 10))

structure(list(month = c("2016-09", "2016-10", "2016-11", "2016-12", 
"2017-01", "2017-02", "2017-03", "2017-04", "2017-05", "2017-06"
), AveragePrice = c(3.16968709677419, 0.791904347826087, 0.225412279295455, 
0.179445766423358, 0.444554531722054, 3.35761658783784, 5.6894554715794, 
16.6639257580906, 53.1994216287425, 66.4208618873239), pct.chg = c(NA, 
-0.750163242096669, -0.71535415872605, -0.203921955874672, 1.4773754242451, 
6.55276652974736, 0.694492305103592, 1.9289139956068, 2.19249031716991, 
0.24852601501664)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))

Many thanks in advance !

NewCommer
  • 37
  • 6
  • Try `plot(as.Date(paste0(df$month,'-1')), df$AveragePrice)` but you will need more robust way to change month from character to a valid R Date type, so R can find the `min(x)` and `max(x)` as shown in the Error message. – A. Suliman Dec 29 '18 at 21:13
  • Many thanks, it works ! Just, how can I change durably the month from character to a valid R Data type ? – NewCommer Dec 30 '18 at 12:03
  • See https://stackoverflow.com/questions/6242955/converting-year-and-month-yyyy-mm-format-to-a-date – A. Suliman Dec 30 '18 at 14:16
  • I used to try this but gave up because i have the following error : "Error in textConnection(text) : argument 'text' incorrect" – NewCommer Dec 30 '18 at 14:20

1 Answers1

0
library(zoo)
MonthlyAveragePriceHAIRPEPE$month_ymon <- as.yearmon(MonthlyAveragePriceHAIRPEPE$month)
plot(MonthlyAveragePriceHAIRPEPE$month_ymon, MonthlyAveragePriceHAIRPEPE$AveragePrice)

#OR
library(lubridate)
MonthlyAveragePriceHAIRPEPE$month_parse <- parse_date_time(MonthlyAveragePriceHAIRPEPE$month, 'ym')
plot(MonthlyAveragePriceHAIRPEPE$month_parse, MonthlyAveragePriceHAIRPEPE$AveragePrice)

#OR
MonthlyAveragePriceHAIRPEPE$month_base <- as.Date(paste0(MonthlyAveragePriceHAIRPEPE$month,'-1'))
A. Suliman
  • 12,923
  • 5
  • 24
  • 37