【发布时间】:2012-07-12 14:06:28
【问题描述】:
我是 ggplot2 的新手,它很棒,但我在一件事上遇到了困难。
我已经绘制了许多跨越一年的时间序列。 X 轴源自 Date 类的变量。我已经对情节进行了刻面,以便在具有独立 y 轴的列中有 7 个时间序列。该图的重点是比较每个刻面与顶部刻面的相关性。
我想做的最后一件事是将文本(每个方面与第一个方面之间的估计 pearson 相关性)添加到每个方面右上角的图中。
这被证明是非常困难的,因为geom_text()需要每个文本位的 x 和 y 坐标。当 X 轴是日期并且 Y 轴对于每个方面都不同时,如何指定坐标?以下是我目前拥有的一些示例数据和代码,因此您可以重现我目前拥有的内容:
library(ggplot2)
date <- rep(as.Date(1:365,origin='2011-1-1'),7)
location <- factor(rep(1:7,365))
product <- rep(letters[1:7], each=365)
value <- c(sample(1:10, size=365, replace=T),sample(1:3, size=365, replace=T),
sample(10:100, size=365, replace=T), sample(1:50, size=365, replace=T),
sample(1:20, size=365, replace=T),sample(50:100, size=365, replace=T),
sample(1:100, size=365, replace=T))
dat<-data.frame(date,location,product,value)
qplot(date, value, data=dat, geom="line", color=location, group=location,
main='Time Series Comparison', xlab='Month (2011)',ylab='Value') +
facet_grid(product ~ ., scale = "free_y")
【问题讨论】:
标签: r text ggplot2 time-series