0

I am just learning R and working with the iclaims data set from the bsts library. The data frame is of the form

           claims
2004-01-04  2.536
2004-01-11  0.882
2004-01-18 -0.077
2004-01-25  0.135
2004-02-01  0.373
2004-02-08 -0.437
...        ...

The short-term goal is to plot this data using ggplot2. However, I need to set the x-axis to the column containing the date, which has no header. I have been referring to this as the key column, but many Google searches seem to indicate that this the wrong term.

So this question really has three parts:

  1. What is the column containing the dates called?

  2. How do you extract this column? Output should read along the lines of

    2004-01-04 2004-01-11 2004-01-18 2004-01-25 2004-02-01 2004-02-08 ...

  3. When using ggplot2, how do you assign this column to x in the aes() argument?

user5249203
  • 4,436
  • 1
  • 19
  • 45
littlebenlittle
  • 833
  • 2
  • 9
  • 18
  • 1
    I don't have this library installed, but those look like row names, which you can access with `row.names()`. For `ggplot` or many other operations, make a column in your data frame that contains these rownames, either by assigning a new column, or with `tibble::rownames_to_column`. – camille Aug 02 '18 at 16:43
  • duplicate Q. https://stackoverflow.com/questions/23246070/how-to-use-names-and-rownames-of-a-dataframe-for-the-aes-of-ggplot – user5249203 Aug 02 '18 at 17:24

1 Answers1

0

From what you have posted it looks as if that is the row names "column" which is not an actual column. you can access it with row.names().

However to use it as a column you can convert it with:

data.tables::setDT(df, keep.rownames = TRUE)[]

Then it should have a name that you can use to plot.

divibisan
  • 11,659
  • 11
  • 40
  • 58
Adam Wheeler
  • 393
  • 1
  • 11