0

I have the following subset of data (which is imported from a csv file):

treas <- read.csv(file = 'treas.csv', header = TRUE, stringsAsFactors = FALSE)

      DATES    X2YR X3YR X5YR X7YR X10YR X30YR
1  6/3/2014    0.41 0.85 1.65 2.18  2.60  3.43
2  6/4/2014    0.41 0.85 1.65 2.20  2.61  3.45
3  6/5/2014    0.40 0.82 1.63 2.17  2.59  3.44
4  6/6/2014    0.41 0.86 1.66 2.19  2.60  3.44
5  6/9/2014    0.43 0.88 1.69 2.22  2.62  3.45
6 6/10/2014    0.45 0.93 1.71 2.24  2.64  3.47

When I call head() on the treas_yields dataframe, I see:

  X2YR X3YR X5YR X7YR X10YR X30YR
1 0.41 0.85 1.65 2.18  2.60  3.43
2 0.41 0.85 1.65 2.20  2.61  3.45
3 0.40 0.82 1.63 2.17  2.59  3.44
4 0.41 0.86 1.66 2.19  2.60  3.44
5 0.43 0.88 1.69 2.22  2.62  3.45
6 0.45 0.93 1.71 2.24  2.64  3.47

Now, I create a few columns to the right of this frame (essentially, just creating differences of pre-existing values):

To do so, I use:

treas_yields$2s30s = (treas_yields$X30YR - treas_yields$X2YR) * 100
treas_yields$2s10s = (treas_yields$X10YR - treas_yields$X2YR) * 100

Calling head(treas_yields) again, we see:

      DATES X2YR X3YR X5YR X7YR X10YR X30YR X2s30s X2s10s
1  6/3/2014 0.41 0.85 1.65 2.18  2.60  3.43    302    219
2  6/4/2014 0.41 0.85 1.65 2.20  2.61  3.45    304    220
3  6/5/2014 0.40 0.82 1.63 2.17  2.59  3.44    304    219
4  6/6/2014 0.41 0.86 1.66 2.19  2.60  3.44    303    219
5  6/9/2014 0.43 0.88 1.69 2.22  2.62  3.45    302    219
6 6/10/2014 0.45 0.93 1.71 2.24  2.64  3.47    302    219

Now, I want to plot the time series of X2s30s and X2s10s columns. To do so, I use:

ggplot(treas_yields, aes(x = DATES)) +
  geom_line(aes(y = X2s30s), color = 'red') +
  geom_line(aes(y = X2s10s), color = 'blue')

However, I'm seeing the following error message:

geom_path: Each group consists of only one observation. Do you need to adjust the group
aesthetic?
geom_path: Each group consists of only one observation. Do you need to adjust the group
aesthetic?

The data appears to be numerical when I call str(treas_yields):

'data.frame':   1251 obs. of  9 variables:
 $ DATES : chr  "6/3/2014" "6/4/2014" "6/5/2014" "6/6/2014" ...
 $ X2YR  : num  0.41 0.41 0.4 0.41 0.43 0.45 0.44 0.42 0.45 0.49 ...
 $ X3YR  : num  0.85 0.85 0.82 0.86 0.88 0.93 0.91 0.88 0.93 0.95 ...
 $ X5YR  : num  1.65 1.65 1.63 1.66 1.69 1.71 1.7 1.66 1.7 1.71 ...
 $ X7YR  : num  2.18 2.2 2.17 2.19 2.22 2.24 2.23 2.17 2.21 2.21 ...
 $ X10YR : num  2.6 2.61 2.59 2.6 2.62 2.64 2.65 2.58 2.6 2.61 ...
 $ X30YR : num  3.43 3.45 3.44 3.44 3.45 3.47 3.47 3.41 3.41 3.4 ...
 $ X2s30s: num  302 304 304 303 302 302 303 299 296 291 ...
 $ X2s10s: num  219 220 219 219 219 219 221 216 215 212 ...

Any idea what is causing these errors? Thanks!

EDIT: this issue was caused by the dates not being formatting correctly. After importing the csv file, it was necessary to convert the dates from characters to actual dates (as suggested by @aosmith in the comments below).

The revised code is:

library(ggplot2)
library (lubridate)

# import data
treas <- read.csv(file = 'treas.csv', header = TRUE, stringsAsFactors = FALSE)

# convert dates (necessary for plotting later)
treas_yields$DATES = mdy(treas_yields$DATES)

# add columns
treas_yields$2s30s = (treas_yields$X30YR - treas_yields$X2YR) * 100
treas_yields$2s10s = (treas_yields$X10YR - treas_yields$X2YR) * 100

# plot newly added columns
ggplot(treas_yields, aes(x = DATES)) +
  geom_line(aes(y = X2s30s), color = 'red') +
  geom_line(aes(y = X2s10s), color = 'blue')
equanimity
  • 2,371
  • 3
  • 29
  • 53
  • 3
    I think this has to do with your `x` variable. You likely want to convert your dates from characters to actual dates prior to plotting. – aosmith Jun 06 '19 at 15:07
  • That's exactly right. Converting the dates from character to dates resolves the issue. I will edit the original post. Thank you! – equanimity Jun 06 '19 at 15:20

0 Answers0