单个数据帧的多个或多个时间序列图输出

编程入门 行业动态 更新时间:2024-10-20 09:21:18
本文介绍了单个数据帧的多个或多个时间序列图输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 你好

我已经在这个问题上苦苦挣扎了一段时间了,任何可以帮助我的人都会很感激.

首先,我正在包含多个时间序列的单个数据帧中处理时间序列数据.太多,无法单独输出到图形中.我尝试过通过ddply()传递qplot(),但是r告诉我qplot不是函数,因此它将不起作用.

我的数据结构是这样的...

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),并且希望能够遍历下图所示的每个测站数据,以便我可以目视检查所有数据.

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()应该可以做到这一点,但我无法终生解决.任何帮助,我们将不胜感激.

谢谢

-Zia

编辑-

正如Arun所指出的那样,我正在尝试保存一个由.loc分割的我的goodlocs df的250个不同图形的.pdf文件,并带有点和线的几何图形以供数据查看.

我还尝试将df的ddply作为数据通过qplot传递,但是它也不起作用,我并不是很期待,但是我必须尝试.

解决方案

如何?

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保存它.

我会让您进行其他可能需要的调整.

Hello,

I have been struggling with this problem for a while now and anyone who can help me out I would greatly appreciate it.

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

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)

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.

Thanks,

-Zia

edit -

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....

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.

解决方案

How about this?

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")

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.

更多推荐

单个数据帧的多个或多个时间序列图输出

本文发布于:2023-07-18 04:44:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141131.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   时间   数据   序列图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!