繁体   English   中英

动物园情节中的奇怪虚线

[英]Strange dashed lines in zoo plot

我有以下非常简单的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作为输入,而不是data 我的情节

你可以使用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系列...... 在此输入图像描述

两件事:您的字符串被读作因子,并且您通过字符向量而不是日期来索引zoo对象。

如果在read.csv调用中包含stringsAsFactors=FALSE并为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)

哪个产生:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM