问题描述
install.packages('RCurl')
require(RCurl)
install.packages('zoo')
require(zoo)
x = getURL("https://raw.githubusercontent.com/RInterested/DATASETS/gh-pages/interest%20in%20soccer%20in%20the%20US.csv")
soccer <- read.csv(textConnection(x), header=T)
s <- zoo(soccer)
plot.ts(s$soccer_US)
结果
但我需要 x 轴作为日期.
but I need the x-axis to be the dates.
我知道有很多关于同一主题的帖子,尽管其中很多——包括我可能发布的一些——都包含 xaxt=n"
,这似乎是一个非常绕道而行.
I know there are a good number of posts on the same topic, although many of them - including some I might have posted - entail xaxt="n"
, which seems to be a very circuitous way to go about it.
在基本的 plot()
中是否有更简单的方法来保留 x 轴的日期结构?
Is there a simpler way of preserving the date structure of the x axis in basic plot()
?
推荐答案
在问题的代码中,zoo(...) 命令使用不正确.请参阅 ?zoo 以获取有关它的信息.
In the code in the question the zoo(...) command is used incorrectly. See ?zoo to get information on it.
为此,使用 yearmon 类将文件读入动物园对象 z,然后使用 plot 或 autoplot.
To do this read the file into a zoo object, z, using yearmon class for the times and then just use plot or autoplot.
library(zoo)
u <- "https://raw.githubusercontent.com/RInterested/DATASETS/gh-pages/interest%20in%20soccer%20in%20the%20US.csv"
z <- read.csv.zoo(u, FUN = as.yearmon)
plot(z)
或将最后一行替换为:
library(ggplot2)
autoplot(z) + scale_x_yearmon()
注意
文件的前几行如下所示:
Note
The first few lines of the file look like this:
L <- readLines(u)
writeLines(head(L))
给予:
date,count
2004-01,25
2004-02,29
2004-03,33
2004-04,36
2004-05,37
这篇关于在基本 R plot() 中显示时间序列日期结构的自动方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!