Reputation: 53
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.
Upvotes: 0
Views: 243
Reputation: 526
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()
Upvotes: 1