-1

I have a data-frame which looks as follows,

head(elnino)
YEAR..MONTH         NINO.3      NINO.3.4    rainfall
1  1950   Jan       -1.28         -1.34      5.8
2  1950   Feb       -1.10         -1.25     17.8
3  1950   Mar       -0.92         -1.16     22.4
4  1950   Apr       -0.75         -1.01      8.0
5  1950   May       -0.47         -0.75     44.0
6  1950   Jun       -0.49         -0.74    146.8

Now the First column ie, YEAR..MONTH is of class FACTOR. I want to convert it to class yearmon. But i am getting NA's as shown below,

as.yearmon(elnino[,1], "%Y-%m")
NA

Moreover ggplot doesn't support yearmon class while plotting.

Therefore can someone please tell me how to handle date of this type?

The data is as follows,

dput(head(elnino))
structure(list(YEAR..MONTH = c("1950   Jan", "1950   Feb", "1950   Mar", 
"1950   Apr", "1950   May", "1950   Jun"), NINO.3.ANOM = c(-1.28, 
-1.1, -0.92, -0.75, -0.47, -0.49), NINO.3.4.ANOM = c(-1.34, -1.25, 
-1.16, -1.01, -0.75, -0.74), rainfall = c(5.8, 17.8, 22.4, 8, 
44, 146.8)), .Names = c("YEAR..MONTH", "NINO.3.ANOM", "NINO.3.4.ANOM", 
"rainfall"), row.names = c(NA, 6L), class = "data.frame")

Moreover the error i get while plotting yearmon class objects with ggplot is as follows,

qplot(YEAR..MONTH,rainfall,data = elnino,geom="line")
Don't know how to automatically pick scale for object of type yearmon. Defaulting to continuous
Error: Discrete value supplied to continuous scale
alistaire
  • 42,459
  • 4
  • 77
  • 117
AKS
  • 11
  • 5
  • Please paste the output of `dput(head(elnino))` into your question instead of the data sample you provided, so that we'll have a sample of your data with the exact same structure as your actual data. Also, please show all the code that's relevant to your problem (including the ggplot code) and any error messages you're getting. – eipi10 Jul 15 '16 at 16:31
  • 1
    The immediate answer is that the format you are passing to `as.yearmon` does not appear to match what is found in the data.frame. However, without the actual data, it is hard to do more than say that. Without knowing what you are trying to plot, I have no way of suggesting a useful workaround/approach. – Mark Peterson Jul 15 '16 at 16:33
  • 1
    try `as.yearmon(elnino[,1], "%Y %b")` The %b refers to an abbreviated month and your column looks to be in the format of year, three spaces, abbreviated month. That bit in quotes refers to the format the data you are converting is currently in, not the way you want it to look after the transformation. – Austin Jul 15 '16 at 16:43
  • @Austin That worked thanks a lot. But qplot() still doesn't support the yearmon class while plotting. How do i tackle that issue? – AKS Jul 15 '16 at 16:49

2 Answers2

3

An easy alternative is to just use the Date class set to the first of the month instead of zoo::yearmon:

elnino$month <- as.Date(paste(elnino$YEAR..MONTH, '01'), '%Y %b %d')

qplot(month, rainfall, data = elnino, geom = "line")

plot with mon labels

The line will be the same, and ggplot knows how to format the axis nicely without more work. If you'd like to add on the year, use scale_x_date:

qplot(month,rainfall,data = elnino,geom="line") + scale_x_date(date_labels = '%b %Y')

plot with yearmon labels

alistaire
  • 42,459
  • 4
  • 77
  • 117
2

As predicted, the format supplied to as.yearmon did not match your data. It has three spaces between year and month, which is given as the abbreviation, not the number. This should work:

as.yearmon(as.character(elnino[,1]), "%Y   %b")

For the plotting, is there a reason you have to use yearmon? Why can't you use a more common Date format or continue to treat the values as factors (depending on your needs).

Mark Peterson
  • 9,370
  • 2
  • 25
  • 48