开发者

Plotting Data from XML that has Timestamps on the X-axis

开发者 https://www.devze.com 2023-01-06 23:40 出处:网络
I got help parsing the following XML file on this site: <?xml version = \"1.0\"?> <Company >

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>

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))

However the resulting plot is not what I expect. It returns t开发者_高级运维he 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?


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)) 

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")

before you can plot via

plot(timeStampParsed, as.numeric(Price))

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

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")


try ggplot2 package from hadley – it´s hilarious.

melt your date with id date and then plot with qplot:

test = melt(test.df,id="timestamps")
qplot(timestamp,yvalue,data=test.df,geom="line")

etc.

HTH

EDIT:

To influence the scale use:

qplot(...) +  scale_y_continuous(limits = c(-0.25,0.25))

Also make sure to check ggplot2 documentation (you´ll find it in the link above) – it simply two-steps every other R documentation on plotting out there.

0

精彩评论

暂无评论...
验证码 换一张
取 消