来自单个数据帧的多个或多个时间序列图输出 [英] Several or multiple timeseries plot outputs from a single data frame
问题描述
我已经为这个问题苦苦挣扎了一段时间,如果有人能帮助我,我将不胜感激.
I have been struggling with this problem for a while now and anyone who can help me out I would greatly appreciate it.
首先,我正在处理包含多个时间序列的单个数据框中的时间序列数据.太多,无法单独输出到图表中.我已经尝试通过 ddply() 传递 qplot() 但是 r 告诉我它 qplot 不是一个函数,因此它不会工作.
First off, I am working with time series data in a single data frame containing multiple time series. Too many to output individually into graphs. I have tried passing qplot() through ddply() however r tells me it qplot is not a function and therefore it will not work.
我的数据结构是这样的……
the structure of my data is like this...
goodlocs <-
Loc Year dir
Artesia 1983 1490
Artesia 1984 1575
Artesia 1986 1567
Artesia 1987 1630
Artesia 1990 1680
Bogota 1983 1525
Bogota 1984 1610
Bogota 1985 1602
Bogota 1986 1665
Bogota 1990 1715
Carlsbad 1983 1560
Carlsbad 1985 1645
Carlsbad 1986 1637
Carlsbad 1987 1700
Carlsbad 1990 1750
Carlsbad 1992 1595
Datil 1987 1680
Datil 1990 1672
Datil 1991 1735
Datil 1992 1785
我有大约 250 个位置 (Locs),希望能够在如下图所示的图表上查看每个站点的数据,这样我就可以直观地检查我的所有数据.
I have about 250 Locations(Locs) and would like to be able to go over each stations data on a graph like the following one so I can inspect all of my data visually.
Artesia <- goodlocs[goodlocs$Loc == "Artesia",]
qplot(YEAR, dir, data = Artesia, geom = c("point", "line"), xlab = "Year",
ylab = "DIR", main = "Artesia DIR Over Record Period") +
geom_smooth(method=lm)
我知道 Par() 应该有助于做到这一点,但我一生都无法弄清楚.非常感谢任何帮助.
I understand that Par() is supposed to help do this but I can not figure it out for the life of me. Any help is greatly appreciated.
谢谢,
-齐亚
编辑-
正如 Arun 指出的那样,我正在尝试保存一个 .pdf,其中包含我的 goodlocs df 的 250 个不同图表,按Loc"分割,并带有用于数据审查的点和线几何......
as Arun pointed out, I am trying to save a .pdf of 250 different graphs of my goodlocs df split by "Loc", with point and line geometry for data review....
我还尝试通过 qplot 将我的 df 的 ddply 作为数据传递,但它也不起作用,我并没有真正期待它,但我不得不尝试.
I also tried passing a ddply of my df through qplot as the data but it did not work either, I was not really expecting it to but i had to try.
推荐答案
这个怎么样?
require(ggplot2)
require(plyr)
require(gridExtra)
pl <- dlply(df, .(Loc), function(dat) {
ggplot(data = dat, aes(x = Year, y = dir)) + geom_line() +
geom_point() + xlab("x-label") + ylab("y-label") +
geom_smooth(method = "lm")
})
ml <- do.call(marrangeGrob, c(pl, list(nrow = 2, ncol = 2)))
ggsave("my_plots.pdf", ml, height = 7, width = 13, units = "in")
<小时>
思路:首先按Loc
分割数据,并为每个子集创建图.拆分部分是使用 plyr
函数 dlply
完成的,该函数基本上以 data.frame
作为输入并提供 list
作为输出.绘图元素存储在与子集对应的列表的每个元素中.然后,我们使用 gridExtra
包的 marrangeGrob
函数来排列多个绘图(其中还有非常有用的 nrow
和 ncol
参数来设置参数).然后,您可以使用 ggplot2
中的 ggsave
保存它.
The idea: First split the data by Loc
and create the plot for each subset. The splitting part is done using plyr
function dlply
that basically takes a data.frame
as input and provides a list
as output. The plot element is stored in each element of the list corresponding to the subset. Then, we use gridExtra
package's marrangeGrob
function to arrange multiple plots (which also has the very useful nrow
and ncol
arguments to set the argument). Then, you can save it using ggsave
from ggplot2
.
我会留给您可能需要的任何其他调整.
I'll leave you to any additional tweaks you may require.
这篇关于来自单个数据帧的多个或多个时间序列图输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!