2sb*_*2sb 8 plot datetime r ggplot2
我有一个数据框,其中一列为日期/时间(内部存储为数字),其他列为数字/整数,我想根据日期/时间绘制数据.
使用以下内容填充数据框中的日期/时间.
as.POSIXct(strptime(time, '%H:%M:%S %p %m/%d/%Y',tz='GMT'))
Run Code Online (Sandbox Code Playgroud)
class(table$time)是numeric.
dateTime1以及dateTime2地方dateTime1和dateTime2在一定的格式给定的日期.Pau*_*tra 11
你也可以ggplot2更具体地使用geom_point或geom_line(注意我使用来自@plannapus的示例数据):
require(ggplot2)
theme_set(theme_bw()) # Change the theme to my preference
ggplot(aes(x = time, y = variable), data = data) + geom_point()
Run Code Online (Sandbox Code Playgroud)

或使用线几何:
ggplot(aes(x = time, y = variable), data = data) + geom_line()
Run Code Online (Sandbox Code Playgroud)

ggplot2 自动识别x轴的数据类型是日期,并相应地绘制轴.
这是一些虚拟数据:
data <- structure(list(time = structure(c(1338361200, 1338390000, 1338445800, 1338476400, 1338532200, 1338562800, 1338618600, 1338647400, 1338791400, 1338822000), class = c("POSIXct", "POSIXt"), tzone = ""), variable = c(168L, 193L, 193L, 201L, 206L, 200L, 218L, 205L, 211L, 230L)), .Names = c("time", "variable"), row.names = c(NA, -10L), class = "data.frame")
data
time variable
1 2012-05-30 09:00:00 168
2 2012-05-30 17:00:00 193
3 2012-05-31 08:30:00 193
4 2012-05-31 17:00:00 201
5 2012-06-01 08:30:00 206
6 2012-06-01 17:00:00 200
7 2012-06-02 08:30:00 218
8 2012-06-02 16:30:00 205
9 2012-06-04 08:30:00 211
10 2012-06-04 17:00:00 230
Run Code Online (Sandbox Code Playgroud)
要在轴上显示日期和时间,您可以使用以下功能axis.POSIXct:
plot(data, xaxt="n")
axis.POSIXct(side=1, at=cut(data$time, "days"), format="%m/%d")
Run Code Online (Sandbox Code Playgroud)
您可以控制滴答的位置at(对于常规函数,axis除了此处将提供POSIXct类的对象)并控制它们的显示方式format.
就子集化而言,只要您的dateTime1和dateTime2对象也是POSIXct对象,您就可以像执行任何其他类型的子集一样进行.
dateTime1 <- strptime("00:00 05/31/2012", format="%H:%M %m/%d/%Y")
dateTime2 <- strptime("3 Jun 2012 05-30", format="%d %b %Y %H-%M")
data[data$time < dateTime2 & data$time > dateTime1, ]
time variable
3 2012-05-31 08:30:00 193
4 2012-05-31 17:00:00 201
5 2012-06-01 08:30:00 206
6 2012-06-01 17:00:00 200
7 2012-06-02 08:30:00 218
8 2012-06-02 16:30:00 205
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
39380 次 |
| 最近记录: |