你如何在R中绘制时间序列?

时间:2016-03-15 00:44:27

标签: r

这是我的任务:

  

从在线资源中读取您选择的任何股票的时间序列数据,在价格范围内绘制交易量(“交易量”)(“高” - “低”)。

这就是我所拥有的:

                DrawShapes shapes = new DrawShapes();
                add(shapes);
            }
            @Override
            public void mousePressed(MouseEvent event){
                statusbar.setText("You pressed down the mouse");
            }

            @Override
            public void mouseReleased(MouseEvent event){
                statusbar.setText("You released the button");
            }
            @Override
            public void mouseEntered(MouseEvent event){
                statusbar.setText("You entered the area");
                mousepanel.setBackground(Color.RED);
            }
            @Override
            public void mouseExited(MouseEvent event){
                statusbar.setText("The mouse has left the window");
                mousepanel.setBackground(Color.WHITE);
            }
            //These are mouse motion events
            @Override
            public void mouseDragged(MouseEvent event){
                statusbar.setText("You are dragging the mouse");
            }
            @Override
            public void mouseMoved(MouseEvent event){
                statusbar.setText("You are moving the mouse");
            }
        }
    }

我得到的结果是:

enter image description here

这看起来是正确的方法,但我不确定。

我读过How to make a great R reproducible example,我认为我需要包含一个向量或矩阵才能使其可重现。但是,我不知道如何针对我的具体问题做这件事。

这是包含股票信息的CSV文件:

#Set the working directory
setwd("C:/Examples")

mydata<- read.csv("C:/Examples/table.csv")
attach(mydata)

plot(Volume, High - Low)

我不知道如何让这更清楚。我的代码是对的吗?如果不是为什么?

1 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情:

阅读数据框

mydata <- read.table(text="Date,Open,High,Low,Close,Volume,Adj Close
2/9/2016,26.639999,27.690001,26.51,26.82,13919100,26.82
2/8/2016,27.610001,27.969999,26.48,27.049999,24473600,27.049999
2/5/2016,29.059999,29.139999,27.73,27.969999,16077500,27.969999
2/4/2016,27.91,29.23,27.709999,29.15,28517000,29.15
2/3/2016,28.450001,28.610001,26.57,27.68,55527600,27.68
2/2/2016,29.32,30.23,28.129999,29.059999,34022500,29.059999
2/1/2016,29.27,29.790001,28.790001,29.57,12865800,29.57
1/29/2016,29.1,29.51,28.51,29.51,18718300,29.51
1/28/2016,30.59,30.629999,28.6,28.75,15420500,28.75
1/27/2016,29.9,30.530001,29.450001,29.690001,13269900,29.690001
1/26/2016,29.76,30.190001,29.620001,29.98,11422600,29.98
1/25/2016,29.959999,30.389999,29.66,29.780001,23095500,29.780001", header=T, sep=',')

制作一个不同于HighLow

的新列
mydata$`High-Low` <- mydata$High-mydata$Low

制作一个Time Serie,其中包含2个选定列,按日期排序

myts <- ts(mydata[order(strptime(mydata$Date, format="%m/%d/%Y")),c('Volume', 'High-Low')])

绘制

plot(myts)

enter image description here