Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
726 views
in Technique[技术] by (71.8m points)

r - Several or multiple timeseries plot outputs from a single data frame

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...