[R] lattice question
Bert Gunter
bgunter@4567 @end|ng |rom gm@||@com
Fri Aug 12 23:29:14 CEST 2022
As a private coda -- as it is unlikely to be of general interest --
note that it is easy to do this without resorting to the layering
paradigm through the use of the appropriate panel function and
lattice's argument passing protocol (although I stand in awe of Felix
Andrews's clever code!). Here's a version that uses R's underlying
with() function to control the nonstandard evaluation. I've added a
key and y axis label just to be cute:
with(mydf,xyplot(short ~ date, group = name,
type = c("l","g"),
y2 =long,
ylim = extendrange(c(short,long)),
auto.key = list(lines=TRUE, points = FALSE, space = "right"),
ylab = "Name",
panel= function(x,y, y2,...){
panel.superpose(x,y,...)
panel.superpose(x,y2,...)
}))
On Fri, Aug 12, 2022 at 12:35 PM Naresh Gurbuxani
<naresh_gurbuxani using hotmail.com> wrote:
>
> This is the solution I was looking for. Thanks to Deepayan and Bert for sticking with me.
>
> Naresh
>
> Sent from my iPhone
>
> On Aug 12, 2022, at 8:02 AM, Deepayan Sarkar <deepayan.sarkar using gmail.com> wrote:
>
>
>
>
> On Thu, Aug 11, 2022 at 9:03 PM Naresh Gurbuxani <naresh_gurbuxani using hotmail.com> wrote:
>>
>> Bert,
>>
>> Thanks for providing this solution. It produces the desired graph.
>>
>> To see how I want to distinguish groups, you should look at original data (mydf). There are two groups (Aa and Bb), each with two time series (long and short). Long is always positive. Short is always negative. Therefore, there is no need to distinguish between long and short. I only need to distinguish between Aa and Bb.
>>
>> I agree that more than six lines in the graph will make it cluttered. In fact the above exercise is to avoid clutter in the key. No need to show Aa.long and Aa.short, because long and short are obvious.
>
>
> In that case, this alternative approach may be conceptually simpler:
>
> library(latticeExtra)
>
> r <- with(mydf, extendrange(range(long, short)))
>
> plong <- xyplot(long ~ date, groups = name, data = mydf,
> type = "l", grid = TRUE,
> ylim = r)
>
> pshort <- xyplot(short ~ date, groups = name, data = mydf, type = "l")
>
> plong + pshort
>
> The first plot here is the "main" one, so you need to make sure that its 'ylim' is big enough for all the data.
>
> -Deepayan
>
>
>>
>>
>> Thanks,
>> Naresh
>>
>> Sent from my iPhone
>>
>> > On Aug 10, 2022, at 10:02 PM, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>> >
>> > It is unclear to me how you wish to define and distinguish groups.
>> > Assuming you wish to have separate lines for the interaction as
>> > Deepayan showed, but want the colors (or line types or both) to differ
>> > only by by the "name" factor, then is this what you want?
>> >
>> > trellis.par.set(superpose.line = list(lty = 1:6)) ## or other line
>> > types you create
>> > u.names <- unique(mydf.long$name)
>> > xyplot(X ~ date, groups = interaction(name,G), data = mydf.long,
>> > type = c("l","g"),
>> > col.line = trellis.par.get("superpose.line")$col[
>> > seq_along(u.names)],
>> > lty = trellis.par.get("superpose.line")$lty[
>> > seq_along(u.names)]
>> > )
>> >
>> > Notes:
>> > 1. If this is not what you want, I give up. Others may have better insight.
>> > 2. If this is what you want, Deepayan may be able to provide you a
>> > nicer way to do it.
>> > 3. If you have more different names than 6 or 7, then you may have to
>> > add more line types or colors to the superpose.line settings. Though I
>> > would think the plot would be pretty much a mess, if so.
>> >
>> > Bert
>> >
>> >
>> >> On Wed, Aug 10, 2022 at 4:57 PM Naresh Gurbuxani
>> >> <naresh_gurbuxani using hotmail.com> wrote:
>> >>
>> >> Actually I meant types (e.g., lty = 1:2). But colors would also work. But I do not want to specify these in call to xyplot().
>> >>
>> >> In my actual problem, there are more than two groups. Moreover, the number of groups changes from case to case. In my set up, I use trellis.par.set() to set line styles, colors, pch, etc.
>> >>
>> >> I would the call to xyplot() to automatically use the set options.
>> >>
>> >> Thanks,
>> >> Naresh
>> >>
>> >> Sent from my iPhone
>> >>
>> >>>> On Aug 10, 2022, at 5:40 PM, Bert Gunter <bgunter.4567 using gmail.com> wrote:
>> >>>
>> >>> I assume you mean two line colors, not types.
>> >>> Like this?
>> >>>
>> >>> xyplot(X ~ date, groups = interaction(name,G), data = mydf.long,
>> >>> type = c("l", "g"), col.line = c("blue","red"))
>> >>>
>> >>>
>> >>> Cheers,
>> >>> Bert
>> >>>
>> >>>> On Wed, Aug 10, 2022 at 2:12 PM Naresh Gurbuxani
>> >>>> <naresh_gurbuxani using hotmail.com> wrote:
>> >>>>
>> >>>> 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 using gmail.com> wrote:
>> >>>>>
>> >>>>> On Wed, Aug 10, 2022 at 4:53 PM Naresh Gurbuxani
>> >>>>> <naresh_gurbuxani using 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 using 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.
>> >>>> ______________________________________________
>> >>>> R-help using 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.
More information about the R-help
mailing list