问题描述
我知道这个问题可能是陈词滥调,但我很难做到.
我的数据集格式如下:
日期访问11/1/2010 69653711/2/2010 71874811/3/2010 79935511/4/2010 80580011/5/2010 70126211/6/2010 53157911/7/2010 69006811/8/2010 75694711/9/2010 7187572010 年 11 月 10 日 70176811/11/2010 82011311/12/2010 645259我想创建一个时间序列图,x 轴代表时间 &y 轴视图.另外,我想用日期标记 x 轴.我使用的代码如下:
dm$newday = as.POSIXct(strptime(dm$Day, format="%Y-%m-%d"))plot(as.Date(dm$day),dm$visits)axis.Date(1,Day,at=seq(as.Date("2010/10/30"), as.Date("2011/01/29"),by="days"))
1) 由于时间是日期,所以一定要使用 Date"
类,而不是 POSIXct"
或 POSIXlt"
.请参阅 R 新闻 4/1 以获取建议,并在最后的注释中定义 Lines
的地方尝试此操作.这里没有使用任何包.
dm
text = Lines
的使用只是为了保持示例自包含,实际上它会被替换为类似 "myfile.dat"
的内容.(上图后续)
2) 由于这是一个时间序列,您可能希望使用时间序列表示法提供稍微简单的代码:
图书馆(动物园)z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")绘图(z,xaxt =n")轴(1,dm$Date,格式(dm$Date,%b %d"),cex.axis = .7)
根据您希望绘图的样子,在第一种情况下仅使用
plot(Visits ~ Date, dm)
或 plot(z)
可能就足够了> 在第二种情况下完全禁止 axis
命令.也可以使用 xyplot.zoo
库(格子)xyplot(z)
或 autoplot.zoo:
库(ggplot2)自动绘图(z)
注意:
行 <- "日期访问11/1/2010 69653711/2/2010 71874811/3/2010 79935511/4/2010 80580011/5/2010 70126211/6/2010 53157911/7/2010 69006811/8/2010 75694711/9/2010 7187572010 年 11 月 10 日 70176811/11/2010 8201132010 年 11 月 12 日 645259"
I know that this question might be a cliche, but I'm having hard time doing it.
I've data set in the following format:
Date Visits
11/1/2010 696537
11/2/2010 718748
11/3/2010 799355
11/4/2010 805800
11/5/2010 701262
11/6/2010 531579
11/7/2010 690068
11/8/2010 756947
11/9/2010 718757
11/10/2010 701768
11/11/2010 820113
11/12/2010 645259
I want to create a time-series plot, with x-axis representing time & y-axis vists. Also, I want to mark the x-axis with date. The code I was using is the following:
dm$newday = as.POSIXct(strptime(dm$Day, format="%Y-%m-%d"))
plot(as.Date(dm$day),dm$visits)
axis.Date(1,Day,at=seq(as.Date("2010/10/30"), as.Date("2011/01/29"),by="days"))
解决方案
1) Since the times are dates be sure to use
"Date"
class, not "POSIXct"
or "POSIXlt"
. See R News 4/1 for advice and try this where Lines
is defined in the Note at the end. No packages are used here.
dm <- read.table(text = Lines, header = TRUE)
dm$Date <- as.Date(dm$Date, "%m/%d/%Y")
plot(Visits ~ Date, dm, xaxt = "n", type = "l")
axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7)
The use of
text = Lines
is just to keep the example self-contained and in reality it would be replaced with something like "myfile.dat"
. (continued after image)
2) Since this is a time series you may wish to use a time series representation giving slightly simpler code:
library(zoo)
z <- read.zoo(text = Lines, header = TRUE, format = "%m/%d/%Y")
plot(z, xaxt = "n")
axis(1, dm$Date, format(dm$Date, "%b %d"), cex.axis = .7)
Depending on what you want the plot to look like it may be sufficient just to use
plot(Visits ~ Date, dm)
in the first case or plot(z)
in the second case suppressing the axis
command entirely. It could also be done using xyplot.zoo
library(lattice)
xyplot(z)
or autoplot.zoo:
library(ggplot2)
autoplot(z)
Note:
Lines <- "Date Visits
11/1/2010 696537
11/2/2010 718748
11/3/2010 799355
11/4/2010 805800
11/5/2010 701262
11/6/2010 531579
11/7/2010 690068
11/8/2010 756947
11/9/2010 718757
11/10/2010 701768
11/11/2010 820113
11/12/2010 645259"
这篇关于在 x 轴上绘制带有日期标签的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!