kar*_*cow 8 r time-series scatter-plot ggplot2
数据是一系列日期和时间.
date time
2010-01-01 09:04:43
2010-01-01 10:53:59
2010-01-01 10:57:18
2010-01-01 10:59:30
2010-01-01 11:00:44
…
Run Code Online (Sandbox Code Playgroud)
我的目标是用水平轴(x)上的日期和垂直轴(y)上的时间来表示散点图.我想如果同一天的时间不止一次,我也可以添加颜色强度.
创建日期直方图非常容易.
mydata <- read.table("mydata.txt", header=TRUE, sep=" ")
mydatahist <- hist(as.Date(mydata$day), breaks = "weeks", freq=TRUE, plot=FALSE)
barplot(mydatahist$counts, border=NA, col="#ccaaaa")
Run Code Online (Sandbox Code Playgroud)
任何帮助,RTFM URI打包或提示都是受欢迎的.
And*_*rie 15
该ggplot2包非常容易处理日期和时间.
创建一些日期和时间数据:
dates <- as.POSIXct(as.Date("2011/01/01") + sample(0:365, 100, replace=TRUE))
times <- as.POSIXct(runif(100, 0, 24*60*60), origin="2011/01/01")
df <- data.frame(
dates = dates,
times = times
)
Run Code Online (Sandbox Code Playgroud)
然后得到一些ggplot2魔法. ggplot将自动处理日期,但要正确格式化时间轴使用scale_y_datetime():
library(ggplot2)
library(scales)
ggplot(df, aes(x=dates, y=times)) +
geom_point() +
scale_y_datetime(breaks=date_breaks("4 hour"), labels=date_format("%H:%M")) +
theme(axis.text.x=element_text(angle=90))
Run Code Online (Sandbox Code Playgroud)

关于问题的最后部分,按周分组等:要实现这一点,您可能需要将数据预先汇总到您想要的存储桶中.您可以使用可能的用途plyr,然后将结果数据传递给ggplot.
| 归档时间: |
|
| 查看次数: |
11854 次 |
| 最近记录: |