从 X 轴上有时间戳的 XML 中绘制数据 [英] Plotting Data from XML that has Timestamps on the X-axis

查看:28
本文介绍了从 X 轴上有时间戳的 XML 中绘制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解析此站点上的以下 XML 文件时得到了帮助:

I got help parsing the following XML file on this site:

<?xml version = "1.0"?>
    <Company >
   <shareprice>
    <timeStamp> 12:00:00.01</timeStamp>
    <Price>  25.02</Price>
   </shareprice>

   <shareprice>
    <timeStamp> 12:00:00.02</timeStamp>
    <Price>  15</Price>
   </shareprice>



   <shareprice>
    <timeStamp> 12:00:00.025</timeStamp>
    <Price>  15.02</Price>
    </shareprice>



    <shareprice>
    <timeStamp> 12:00:00.031</timeStamp>
    <Price>  18.25</Price>
</shareprice>



  <shareprice>
    <timeStamp> 12:00:00.039</timeStamp>
    <Price>  18.54</Price>
  </shareprice>



   <shareprice>
    <timeStamp> 12:00:00.050</timeStamp>
    <Price> 16.52</Price>
  </shareprice>


    <shareprice>
    <timeStamp> 12:00:01.01</timeStamp>
    <Price>  17.50</Price>
    </shareprice>
</Company>

我在 R 中使用以下代码尝试绘制数据以获取 Y 轴上的股价和 x 轴上的时间戳:

I am using the following code in R to try and plot the data to get the share price on the Y axis and the timestamp on the x axis:

library (XML)
test.df <- xmlToDataFrame("c:/Users/user/Desktop/shares.xml")
test.df
attach(test.df)
mean(as.numeric(Price))
sd (as.numeric(Price)) 
plot(timeStamp,as.numeric(Price))

然而,结果图并不是我所期望的.它返回 x 轴上的时间戳,但 y 轴编号为 1 - 7.我应该做些什么来更改 R 或 XML 文件本身中的数据集?

However the resulting plot is not what I expect. It returns the Time stamps on the x axis but the y axis is numbered from 1 - 7. Is there something I should be doing to alter the data set either in R or the XML file itself?

推荐答案

您需要将 x 轴数据实际转换为时间对象.结合你的

You need to actually turn the x-axis data into time objects. Combine your

library (XML)
test.df <- xmlToDataFrame("c:/Users/user/Desktop/shares.xml")
test.df
attach(test.df)
mean(as.numeric(Price))
sd (as.numeric(Price)) 

上周我在 this SO question(你需要 as.character() 因为你的数据可能作为因素进来)

with what I showed you last week in this SO question (and you need as.character() as your data probably came in as factors)

timeStampParsed <- strptime(as.character(timeStamp), "%H:%M:%OS")

在你可以通过绘图之前

plot(timeStampParsed, as.numeric(Price))

对于 ggplot2 也是如此:您首先需要将数据转换为日期类型.

Likewise for ggplot2: You first need to get your data into a date type.

最后,如果您希望那里的实际日期与今天的推算默认值不同,则需要将其添加到 timeStamp 文本中,例如

Lastly, if you want an actual day in there that is different from the imputed default of today, you need to prepend it to the timeStamp text as for example in

timeStampParsed <- strptime(paste("2010-07-01"), as.character(timeStamp), 
                            "%Y-%m%-%d %H:%M:%OS")

这篇关于从 X 轴上有时间戳的 XML 中绘制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆