On Wed, Aug 10, 2022 at 4:53 PM Naresh Gurbuxani <naresh_gurbuxani at hotmail.com> wrote:> > > I want to merge two panels into one. Is it possible to do this? > > Thanks, > Naresh > > library(lattice) > mydf <- data.frame(date = rep(seq.Date(as.Date("2022-08-01"), by = 1, > length.out = 10), 2), name = c(rep("Aa", 10), rep("Bb", 10)), > long = runif(20, 2, 10), short = runif(20, -10, 0)) > > # This plots data in two panels. I want all four lines in one panel. > xyplot(long + short ~ date, groups = name, data = mydf, type = c("l", > "g"))The "extended" formula API (with +) is really only meant as an alternative to reshape() for simple cases. In your case, you probably want something like mydf.long = reshape(mydf, direction = "long", varying = list(c("long", "short")), v.names = "X", timevar = "G", times = c("long", "short")) xyplot(X ~ date, groups = interaction(name, G), data = mydf.long, type = c("l", "g")) -Deepayan> # This does not work > # No error in R session > # Graph window says: "Error using packet 1 > # argument 'subscripts' is missing, with no default" > xyplot(long ~ date, data = mydf, groups = name, type = c("l", "g"), > panel = function(x, y, ..., subscripts) { > panel.xyplot(x, y, ...) > panel.xyplot(mydf$date[subscripts], mydf$short[subscripts], ...)}) > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On Wed, Aug 10, 2022 at 8:38 AM Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote:> > On Wed, Aug 10, 2022 at 4:53 PM Naresh Gurbuxani > <naresh_gurbuxani at hotmail.com> wrote: > > > > > > I want to merge two panels into one. Is it possible to do this? > > > > Thanks, > > Naresh > > > > library(lattice) > > mydf <- data.frame(date = rep(seq.Date(as.Date("2022-08-01"), by = 1, > > length.out = 10), 2), name = c(rep("Aa", 10), rep("Bb", 10)), > > long = runif(20, 2, 10), short = runif(20, -10, 0)) > > > > # This plots data in two panels. I want all four lines in one panel. > > xyplot(long + short ~ date, groups = name, data = mydf, type = c("l", > > "g")) > > The "extended" formula API (with +) is really only meant as an > alternative to reshape() for simple cases. In your case, you probably > want something like > > mydf.long = reshape(mydf, direction = "long", varying = list(c("long", > "short")), v.names = "X", timevar = "G", times = c("long", "short")) > > xyplot(X ~ date, groups = interaction(name, G), data = mydf.long, type > = c("l", "g")) >(In the interest of learning- - ) Why? Regards
Deepayan, Thanks for providing a solution. While this is close to my goal, I want one more change. The line type (lty) should be the same for long and short. The line type should only change according to ?name? group. So the the graph will have two line types (not four as in your solution). Is it possible? Thanks, Narrsh Sent from my iPhone> On Aug 10, 2022, at 9:37 AM, Deepayan Sarkar <deepayan.sarkar at gmail.com> wrote: > > ?On Wed, Aug 10, 2022 at 4:53 PM Naresh Gurbuxani > <naresh_gurbuxani at hotmail.com> wrote: >> >> >> I want to merge two panels into one. Is it possible to do this? >> >> Thanks, >> Naresh >> >> library(lattice) >> mydf <- data.frame(date = rep(seq.Date(as.Date("2022-08-01"), by = 1, >> length.out = 10), 2), name = c(rep("Aa", 10), rep("Bb", 10)), >> long = runif(20, 2, 10), short = runif(20, -10, 0)) >> >> # This plots data in two panels. I want all four lines in one panel. >> xyplot(long + short ~ date, groups = name, data = mydf, type = c("l", >> "g")) > > The "extended" formula API (with +) is really only meant as an > alternative to reshape() for simple cases. In your case, you probably > want something like > > mydf.long = reshape(mydf, direction = "long", varying = list(c("long", > "short")), v.names = "X", timevar = "G", times = c("long", "short")) > > xyplot(X ~ date, groups = interaction(name, G), data = mydf.long, type > = c("l", "g")) > > -Deepayan > >> # This does not work >> # No error in R session >> # Graph window says: "Error using packet 1 >> # argument 'subscripts' is missing, with no default" >> xyplot(long ~ date, data = mydf, groups = name, type = c("l", "g"), >> panel = function(x, y, ..., subscripts) { >> panel.xyplot(x, y, ...) >> panel.xyplot(mydf$date[subscripts], mydf$short[subscripts], ...)}) >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code.