![](/img/trans.png)
[英]R: Naming a map with elements from a column and naming its file with elements from another column in a dataframe with a forloop
[英]Naming a column in R
我對代碼很陌生,因此此解決方案可能很簡單; 但是,我無法通過搜索找到合適的答案。
我正在使用quantmod
包。
我剛剛使用getFX
變量下載了數據。 我已將其分配給我的全球環境。
我想將其繪制在ggplot
但是出現問題。 使用繪圖功能可以正常工作。 但是,當我嘗試通過str()
功能弄清楚列名是什么時,我只得到一列標題。 日期字段為空白,結構顯示POSIXct[1:1]
。 如何為該日期列ggplot
以便可以在ggplot
中進行ggplot
?
我嘗試了以下方法,但是沒有運氣
JPYchart <- getFX("USD/JPY", from="2013-05-05", header=FALSE)
我的印象是標題將命名我的列v1,v2等,因為它們未命名,但是它們仍然保持空白。
這對我有用
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.