0

I am fairly new to code so this solution may be simple; however, I could not find the appropriate answer through my searches.

I am using the quantmod package.

I have just downloaded data using the getFX variable. I have assigned it to my global environment.

I want to plot it in ggplot but I am having an issue. It works fine using the plot feature. However, when I try to figure out what the column names are through the str() feature I am only given one column with a title. The date field is blank and the structure says POSIXct[1:1]. How can I title this date column so I can plot it in ggplot?

I have tried the following but have had no luck

JPYchart <- getFX("USD/JPY", from="2013-05-05", header=FALSE)

I was under the impression that header would name my colums v1,v2, etc since they were unnamed however they continue to remain blank.

Uwe
  • 41,420
  • 11
  • 90
  • 134
Justin
  • 53
  • 1
  • 1
  • 4
  • For others who might like to help, they probably don't know `getFX()` is from `quantmod`. Furthermore, that singular line, when run with `quantmod` loaded produces an error. Please review [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and adjust your question accordingly. – hrbrmstr Jul 13 '16 at 01:13
  • I don't believe so, I tried it several times even adding other currency pairs and the code was successful. Try this one: getFX("EUR/USD", from="2012-05-05", header=FALSE) thanks – Justin Jul 13 '16 at 01:18

1 Answers1

1

This works for me

library(quantmod)
library(ggplot2)

# getFX returns USDJPY xts object
getFX("USD/JPY", from="2013-05-05", header=FALSE)

str(USDJPY)
# An ‘xts’ object on 2013-05-05/2016-07-12 containing:
#  Data: num [1:1165, 1] 99 99.2 99.1 98.9 99.1 ...
# - attr(*, "dimnames")=List of 2
#  ..$ : NULL
#  ..$ : chr "USD.JPY"
#  Indexed by objects of class: [Date] TZ: UTC
#  xts Attributes:  
# List of 2
# $ src    : chr "oanda"
# $ updated: POSIXct[1:1], format: "2016-07-12 19:20:01"

# convert USDJPY to a data.frame
df <- as.data.frame(USDJPY)
df$date <- as.Date(rownames(df))

str(df)
#    'data.frame':  1165 obs. of  2 variables:
# $ USD.JPY: num  99 99.2 99.1 98.9 99.1 ...
# $ date   : Date, format: "2013-05-05" "2013-05-06" "2013-05-07" "2013-05-08" ...

# plot with ggplot
ggplot(data = df, aes(x = date, y = df$USD.JPY)) +
  geom_line()

example output

Ben Fasoli
  • 526
  • 3
  • 7
  • Thanks Ben! I truly appreciate the time and assistance. I needed to convert it into a data frame it appears. I will have to look into them more so I can resolve this issue on my own the next time! – Justin Jul 13 '16 at 01:55