[英]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.