2010-07-14 83 views
0

这是哈德利指出的一个后续问题,除非我用时间戳解决问题,否则我生成的图形将不正确。考虑到这一点,我正在努力解决我在代码中遇到的问题。到目前为止,我已经从我以前的问题已被回答停止使用attach()函数赞成使用dataSet.df $ variableName 我有问题从strptime时间戳中绘制图形。我将附上我正在使用的所有代码和从中解析数据集的XML文件(这在以前的问题中也得到了回答)。R的时间戳,Qplot和strptime

<?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代码我目前如下:

library(ggplot2) 
library (XML) 
test.df <- xmlToDataFrame("c:/Users/user/Desktop/shares.xml") 
test.df 
timeStampParsed <- strptime(as.character(test.df$timeStamp), "%H:%M:%OS") 
test.df$Price <- as.numeric(as.character(test.df$Price)) 
summary (test.df) 
mean(test.df$Price) 
sd (test.df$Price) 
mean(timeStampParsed) 
par(mfrow=c(1,2)) 
plot(timeStampParsed, test.df$Price) 
qplot(timeStampParsed,Price,data=test.df,geom=c("point","line"), 
     scale_y_continuous(limits = c(10,26))) 

绘图命令产生一个图形,但它是不是很美观的。在qplot命令返回以下错误消息:

Error in sprintf(gettext(fmt, domain = domain), ...) : 
invalid type of argument[1]: 'symbol' 

在获得这一权利的兴趣(和削减问题被问)有没有教程/网站,我可以使用吗?再次非常感谢您的帮助。

回答

2

仍然使我在我以前的两个答案中纠正的代码中出现一些错误。因此,让我们再试试这个,更明确:

library(ggplot2) 
library (XML) 
df <- xmlToDataFrame("/tmp/anthony.xml") # assign to df, shorter to type 
df 
sapply(df, class)   # shows everything is a factor 
summary(df)    # summary for factor: counts ! 
df$timeStamp <- strptime(as.character(test.df$timeStamp), "%H:%M:%OS") 
df$Price <- as.numeric(as.character(test.df$Price)) 
sapply(df, class)   # shows both columns converted 
options("digits.secs"=3) # make sure we show sub-seconds 
summary (df)    # real summary 
with(df, plot(timeStamp, Price)) # with is an elegant alternative to attach() 

我也qplot()得到一个错误,但你可能只是太少了您的数据的范围的。所以我们试试这个:

R> set.seed(42)    # fix random number generator 
R> df$timeStamp <- df[1,"timeStamp"] + cumsum(runif(7)*60) 
R> summary(df)    # new timestamps spanning larger range 
    timeStamp       Price  
Min. :2010-07-14 12:00:54.90 Min. :15.0 
1st Qu.:2010-07-14 12:01:59.71 1st Qu.:15.8 
Median :2010-07-14 12:02:58.12 Median :17.5 
Mean :2010-07-14 12:02:55.54 Mean :18.0 
3rd Qu.:2010-07-14 12:03:52.20 3rd Qu.:18.4 
Max. :2010-07-14 12:04:51.96 Max. :25.0 
R> qplot(timeStamp,Price, data=df, geom=c("point","line"), 
+ scale_y_continuous(limits = c(10,26))) 
R> 

现在qplot()的作品。总之,您使用的数据并未满足您所使用的qplot函数的某些最低要求 - 例如,时间轴跨度超过一秒。

一般而言,您可能需要从开始R(附带该程序)或其他介绍文本。你先跳先进材料(日期时间数据类型,从XML读取,因素......)并被烧毁。首先迈出第一步。

+0

Dirk感谢您的帮助。我将XML文件更改为不止一秒,这似乎满足qplot函数的最低要求。 – 2010-07-14 18:30:04

+0

我还看到使用strptime()修正时间戳的优势,因为哈德利是正确的,即在不修正时间戳创建错误图的情况下执行图,因为x轴未缩放以表示时间线。 @Dirk我会看看程序附带的R介绍,它只是我没有选择,只能从我做的地方开始。在回复R脚本自动化的同时,您也回答了我的下一个问题之一。我使用了BATCH方法,因为我没有在该答案中找到脚本方法。有没有控制输出文件类型(创建图形的.pdf文件)的方法? – 2010-07-14 21:34:42