动物园情节中的奇怪虚线

时间:2013-01-26 20:28:53

标签: r plot statistics time-series zoo

我有以下非常简单的R脚本,它使用zoo来显示时间轴上的每日数字:

# Small script to plot daily number of added users to database
library(zoo)

data <- read.csv("search_history.csv", header=FALSE)
# last line will be cut because it might be incomplete
zoodata <- data[1:(length(data$V2)-1), ]
series <- zoo(zoodata$V2, zoodata$V1)

par(mar=c(7, 6, 4, 2), 
    lab=c(5, 6, 5), 
    mgp = c(4, 1, 0))

plot(series,
     main="Number of users added to database over time", 
     xlab="Date", 
     ylab="Number of users",
     las=2,
     lwd=2,
     col="red",
     cex.axis=0.7)

search_history.csv的内容:

"2012-12-27","458","4728"
"2012-12-28","239","6766"
"2012-12-29","193","8189"
"2012-12-30","148","7698"
"2012-12-31","137","7370"
"2013-01-01","119","6324"
"2013-01-02","122","7016"
"2013-01-03","115","7986"
"2013-01-04","112","8222"
"2013-01-05","112","6828"
"2013-01-06","124","7318"
"2013-01-07","121","8228"
"2013-01-08","120","8158"
...

我想要显示第一个(V1)和第二个列(V2)。我基本上有两个问题: 第一个 ,显而易见的是y-Position~50和~450的虚线。如何删除它们,为什么它们甚至包括在内?

第二 问题是x-Axis中包含2013-01-26。如您所见,我删除了包含此数据的数据集的最后一行(如业余爱好者,也许有更好的方法可以执行此操作)。因此情节不应包括最后日期。我不明白为什么它甚至知道这个日期,因为它需要zoodata作为输入,而不是datamy plot

2 个答案:

答案 0 :(得分:3)

您可以使用read.zoo

## double quotes were removed but they could have been left in
series <- read.zoo(text = '
2012-12-27,458,4728
2012-12-28,239,6766
2012-12-29,193,8189
2012-12-30,148,7698
2012-12-31,137,7370
2013-01-01,119,6324
2013-01-02,122,7016
2013-01-03,115,7986
2013-01-04,112,8222
2013-01-05,112,6828
2013-01-06,124,7318
2013-01-07,121,8228
2013-01-08,120,8158', sep =',')

然后使用你的情节说明,

plot(series,
     main="Number of users added to database over time", 
     xlab="Date", 
     ylab="Number of users",
     las=2,
     lwd=2,
     col="red",
     cex.axis=0.7)

你可以直观地比较2 Times系列...... enter image description here

答案 1 :(得分:1)

两件事:您的字符串被读作因子,并且您使用字符向量而不是按日期为zoo对象编制索引。

如果您在stringsAsFactors=FALSE来电中加入read.csv,并为zoo对象提供Date索引,那么它看起来会更像您期望的那样。

library(zoo)    
data <- read.csv(text='"2012-12-27","458","4728"
                 "2012-12-28","239","6766"
                 "2012-12-29","193","8189"
                 "2012-12-30","148","7698"
                 "2012-12-31","137","7370"
                 "2013-01-01","119","6324"
                 "2013-01-02","122","7016"
                 "2013-01-03","115","7986"
                 "2013-01-04","112","8222"
                 "2013-01-05","112","6828"
                 "2013-01-06","124","7318"
                 "2013-01-07","121","8228"
                 "2013-01-08","120","8158"', header=FALSE, 
                 stringsAsFactors=FALSE)

zoodata <- data[1:(length(data$V2)-1), ]
series <- zoo(zoodata$V2, as.Date(zoodata$V1))

par(mar=c(7, 6, 4, 2), 
    lab=c(5, 6, 5), 
    mgp = c(4, 1, 0))

plot(series,
     main="Number of users added to database over time", 
     xlab="Date", 
     ylab="Number of users",
     las=2,
     lwd=2,
     col="red",
     cex.axis=0.7)

产生:

enter image description here