Re: [R] Is there a sexy way ...?

2024-09-26 Thread Deepayan Sarkar
> unsplit(x, f)
 [1]  7  2  6 13  5  9  1 14 15  4  8 12 10 11  3

Is more general (works if the subgroups are imbalanced), and hopefully more
sexy as well :-)

Best,
-Deepayan


On Fri, 27 Sept 2024 at 10:11, Bert Gunter  wrote:

> ... And, in fact, I just realized that
>
> c(do.call(rbind, x))
>
> is even better.
>
> -- Bert
>
>
> On Thu, Sep 26, 2024 at 9:26 PM Bert Gunter 
> wrote:
>
> > Sorry, hit send by accident.
> > The 2-line version is:
> >
> > x <- do.call(rbind, x)
> > dim(x) <- NULL
> >
> > Cheers,
> > Bert
> >
> > On Thu, Sep 26, 2024 at 9:23 PM Bert Gunter 
> > wrote:
> >
> >> How about:
> >> as.vector(do.call(rbind,x))
> >>
> >> Cheers,
> >> Bert
> >>
> >>
> >>
> >>
> >> However, I much prefer a 2 line version:
> >>
> >> On Thu, Sep 26, 2024 at 8:56 PM Rolf Turner 
> >> wrote:
> >>
> >>>
> >>> I have (toy example):
> >>>
> >>> x <- list(`1` = c(7, 13, 1, 4, 10),
> >>>   `2` = c(2, 5,  14, 8, 11),
> >>>   `3` = c(6, 9, 15, 12, 3))
> >>> and
> >>>
> >>> f <- factor(rep(1:3,5))
> >>>
> >>> I want to create a vector v of length 15 such that the entries of v,
> >>> corresponding to level l of f are the entries of x[[l]].  I.e. I want
> >>> v to equal
> >>>
> >>> c(7, 2, 6, 13, 5, 9, 1, 14, 15, 4, 8, 12, 10, 11, 3)
> >>>
> >>> I can create v "easily enough", using say, a for-loop.  It seems to me,
> >>> though, that there should be sexier (single command) way of achieving
> >>> the desired result.  However I cannot devise one.
> >>>
> >>> Can anyone point me in the right direction?  Thanks.
> >>>
> >>> cheers,
> >>>
> >>> Rolf Turner
> >>>
> >>> --
> >>> Honorary Research Fellow
> >>> Department of Statistics
> >>> University of Auckland
> >>> Stats. Dep't. (secretaries) phone:
> >>>  +64-9-373-7599 ext. 89622
> >>> Home phone: +64-9-480-4619
> >>>
> >>> __
> >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide
> >>> https://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>>
> >>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] how to specify point symbols in the key on a lattice dotplot

2024-09-14 Thread Deepayan Sarkar
On Fri, 13 Sept 2024 at 23:36, Christopher W. Ryan
 wrote:
>
> For me, Bert's suggestion produces a plot with two black symbols above
> the plotting region, a circle and a triangle, both filled, and no text.
>
> This, in which I specify several features of the symbols in the key,
>
> dd %>%  dotplot( segment ~ transit_time, groups = impact,  data = .,
> pch = 16:17,
> col = 1:2,
> cex = 1.8,
> scales = list(cex = 1.4),
> key = list(text = list(c("impaction", "no impaction")),
> points = list(pch =16:17, col = 1:2) ))
>
> gets me what I want.
>
> When using key = (), is it necessary to specify all features of the
> plotting symbols and the text? I was under the impression that auto.key
> and/or simpleKey (which I'd also tried) had certain defaults, but one or
> more of those defaults could be changed by providing arguments, with all
> unspecified features remaining at their respective defaults.

Here is a very brief review:

- To use key = list(...) you need to be very verbose, specifying
everything you need. It will not "guess" anything. E.g.,

dotplot(1 ~ 1, key = list(text = list(c("one", "two")), points =
list(pch = 16:17, col = 3:4), lines = list(lwd = 2:3, lty = 2:3)))

This used to be the only option in the original Trellis implementation
in S-PLUS.

- simpleKey() produces a key with parameters taken from the current
settings, but you still need to explicitly specify the text labels,
and which components you want. E.g.,

dotplot(1 ~ 1, key = simpleKey(text = c("one", "two"), points = TRUE,
lines = TRUE))

- The result will change if you change the current settings _before_
calling simpleKey(). E.g.,

trellis.par.set(simpleTheme(pch = 16:17))
dotplot(1 ~ 1, key = simpleKey(text = c("one", "two"), points = TRUE,
lines = TRUE))

- auto.key goes one step further and tries to guess the text (and
which components should be included) from the context and the levels
of the 'groups' argument. E.g.,

dotplot(1:10 ~ 1:10, groups = gl(2, 5), auto.key = TRUE)

It also delays the call to simpleKey() to the point when the plot is
actually drawn, in case the settings have changed before then. One way
to do so is to attach a list of settings (as supplied to
trellis.par.set()) as the par.settings argument. Hence,

dotplot(1:10 ~ 1:10, groups = gl(2, 5), auto.key = TRUE, par.settings
= simpleTheme(pch = 3:4))

Hope this helps.

Best,
-Deepayan


> Thanks.
>
> --Chris Ryan
>
>
>
>
> On Fri, 13 Sep 2024 10:45:08 -0700, Bert Gunter wrote:
>
> >"Why does key = list(points = 16:17)  not work? "
> >
> >Because, from the "key" section of ?xyplot
> >" The contents of the key are determined by (possibly repeated)
> >components named "rectangles", "lines", "points" or "text". Each of
> >these must be **lists** with relevant graphical parameters (see later)
> >controlling their appearance."
> >
> >Ergo, try:
> >
> >dd |> dotplot( segment ~ transit_time, groups = impact,  data = .,
> >pch = 16:17,
> >cex = 1.8,
> >scales = list(cex = 1.4),
> >key = list(points = list(pch =16:17) ))
> >
> >Cheers,
> >Bert
> >
> >
> >On Fri, Sep 13, 2024 at 9:53 AM Christopher W. Ryan
> > wrote:
> >>
> >>
> >> dd %>% dotplot( segment ~ transit_time, groups = impact,  data = .,
> >>pch = 16:17,
> >>cex = 1.8,
> >>scales = list(cex = 1.4),
> >>key = list(points = 16:17) )
> >>
> >> produces a graph with no discernible key, but with an asterisk at the
> >> top, above the plotting region.
> >>
> >> Same result from
> >>
> >> dd %>% dotplot( segment ~ transit_time, groups = impact,  data = .,
> >>pch = 16:17,
> >>cex = 1.8,
> >>scales = list(cex = 1.4),
> >>key = list(points = 16:17),
> >>auto.key = TRUE )
> >>
> >>
> >>
> >>
> >> dd %>% dotplot( segment ~ transit_time, groups = impact,  data = .,
> >>scales = list(cex = 1.4),
> >>par.settings = simpleTheme(pch = 16:17, cex = 1.8),
> >>auto.key = TRUE)
> >>
> >> produces the desired result.
> >>
> >> Why does key = list(points = 16:17)  not work?  Below is a MWE:
> >>
> >> 
> >>
> >> library(lattice)
> >> library(dplyr)
> >> dd <- structure(list(impact = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
> >> 2L), levels = c("impaction", "no impaction"), class = "factor"),
> >> segment = structure(c(4L, 2L, 1L, 3L, 4L, 2L, 1L, 3L), levels =
> >> c("left", "right", "rectosigmoid", "total"), class = c("ordered",
> >> "factor" )), transit_time = c(70, 10, 20, 32, 42, 10, 12, 18)),
> >> class = "data.frame", row.names = c(NA, -8L))
> >>
> >> dd %>% dotplot( segment ~ transit_time, groups = impact,  data = .,
> >>pch = 16:17,
> >>cex = 1.8,
> >>scales = list(cex = 1.4),
> >>key = list(points 

Re: [R] how to specify point symbols in the key on a lattice dotplot

2024-09-13 Thread Deepayan Sarkar
On Fri, 13 Sept 2024 at 12:49, Christopher W. Ryan
 wrote:
>
> I am making a dotplot with lattice, as follows:
>
> dd %>% dotplot( segment ~ transit_time, groups = impact,  data = .,
>as.table = TRUE,
> pch = 16:17,
> cex = 1.8,
> scales = list(cex = 1.4),
>auto.key = TRUE)
>
> impact is a factor with two levels.
>
> They key shows 2 open circles, one of each color of my two
> plotting symbols, one for each group. I would like the
> symbols in the key to match the plotting characters in the graph: 16
> (filled circle) for one group and 17 (filled triangle) for the second
> group.  How would I do that? I have not had any success with supplying
> arguments to auto.key, simpleKey, or key. Guess I'm not understanding
> the syntax.

Specifying key = list(...) will work, but the shortcut is to add

par.settings = simpleTheme(pch = 16:17, cex = 1.8)

That way, you don't need to specify the parameters anywhere else.

-Deepayan

> Thanks.
>
> --Chris Ryan
>
> --
> Agency Statistical Consulting, LLC
> Helping those in public service get the most from their data.
> www.agencystatistical.com
>
> Public GnuPG email encryption key at
> https://keys.openpgp.org
> 9E53101D261BEC070CFF1A0DC8BC50E715A672A0
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] dotchart and dotplot(lattice) plot with two/three conditioning variables

2024-09-04 Thread Deepayan Sarkar
For lattice::dotplot(), you are close; this is more like the layout you
want:

dotplot(Year ~ ptile | reorder(Population, ptile, mean), testLong,
groups = c("0-50", "50-90", "90-100")[time],
layout = c(1, NA),
par.settings = simpleTheme(pch = 16), auto.key = TRUE)

dotchart() works better with tables, but unfortunately it doesn't seem to
handle more than two dimensions, so you can only get one group at a time:

xtabs(ptile ~ Year + Population, testLong, subset = time == 1) |>
dotchart(pch = 16)

This seems like something that should not be too difficult to improve.

Best,
-Deepayan


On Wed, 4 Sept 2024 at 18:46, Anupam Tyagi  wrote:

> Hello, I am trying to make a Cleaveland Dotplot with two, if possible
> three, variables on the vertical axis. I was able to do it in Stata
> with two variables, Year and Population (see graph at the link:
>
> https://drive.google.com/file/d/1SiIfmmqk6IFa_OI5i26Ux1ZxkN2oek-o/view?usp=sharing
> ). I hope the link to the graph works. I have never tried this before.
>
> I want to make a similar (possibly better) graph in R. I tried several
> ways to make it in R with dotchart() and dotplot(lattice). I have been
> only partially successful thus far. I would like Year, Population and
> popGroup on the vertical axis. If popGroup occupies too much space,
> then I would like a gap between the groups of Cities and Villages, so
> they can be seen as distinct "Populations". My code and a made-up data
> are below (in actual data I have 18 categories in "Population",
> instead of only six in the made-up data). How can I make this type of
> graph?
>
> # Only for 2004-05. How to plot 2011-12 on the same plot?
> dotchart(test$"X0_50"[test$"Year"=="2004-05"], labels=test$Population,
> xlab = "Income Share ",
>  main = "Income shares of percentiles of population", xlim = c(12,
> 50))
> points(test$"X50_90"[test$"Year"=="2004-05"], 1:6, pch = 2)
> points(test$"X90_100"[test$"Year"=="2004-05"], 1:6, pch = 16)
> legend(x = "topleft",
>legend = c("0-50%", "50-90%", "90-100%"),
>pch = c(1,2, 16)
> )
>
> # reorder so Year 2004-05 is plotted before Year 2011-12. This is not
> plotting correctly for
> # second and third variables. Gap between different Cities and
> Villages is quite a bit.
> test2 <- test[order(test$seqCode, test$Year, decreasing = T),]
>
> dotchart(test2$"X0_50", labels=test2$Year, xlab = "Income Share ",
>  main = "Income shares of percentiles of population", groups =
> as.factor(test2$Population), xlim = c(12, 50))
> points(test2$"X50_90", 1:12, pch = 2)
> points(test2$"X90_100", 1: 12, pch = 16)
>
>
> # use lattice library
> library(lattice)
> dotplot(reorder(Population, -seqCode) ~ test$"X0_50" + test$"X50_90" +
> test$"X90_100", data = test, auto.key = TRUE)
>
> testLong <- reshape(test, idvar = c("Population", "Year"), varying =
> list(5:7),
>v.names = "ptile", direction = "long")
>
> dotplot(reorder(Population, -seqCode) ~ ptile | Year, data = testLong,
> groups = time, auto.key = T)
>
> Dataframe is below using dput(). Dataframe is named "test" in my code.
>
> structure(list(seqCode = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
> 4L, 5L, 6L), popGroup = c("City", "City", "City", "Village",
> "Village", "Village", "City", "City", "City", "Village", "Village",
> "Village"), Population = c("Dallas", "Boston", "Chicago", "Kip",
> "Von", "Dan", "Dallas", "Boston", "Chicago", "Kip", "Von", "Dan"
> ), Year = c("2004-05", "2004-05", "2004-05", "2004-05", "2004-05",
> "2004-05", "2011-12", "2011-12", "2011-12", "2011-12", "2011-12",
> "2011-12"), X0_50 = c(15.47, 21.29, 18.04, 15.62, 18.89, 24.37,
> 17.43, 17.99, 18.04, 14.95, 16.33, 28.98), X50_90 = c(44.12,
> 43.25, 45.72, 46.15, 43.84, 46.24, 44.39, 44.08, 43.62, 42.89,
> 44.57, 47.14), X90_100 = c(40.42, 35.47, 36.24, 38.24, 37.27,
> 29.39, 38.18, 37.93, 38.34, 42.16, 39.11, 23.88)), class =
> "data.frame", row.names = c(NA,
> -12L))
>
> --
> Anupam.
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] lattice panel layout like cross-tabs, like a 2 x 2 table.

2024-08-30 Thread Deepayan Sarkar
You are thinking of latticeExtra::useOuterStrips.

Best,
-Deepayan

On Sat, 31 Aug 2024 at 02:53, Christopher W. Ryan via R-help <
r-help@r-project.org> wrote:

> Years ago, I recall creating lattice plots with two binary factors, call
> them f1 and f2, as in
>
> xyplot(y ~ x | f1 + f2, data = dd)
>
> and I made it so the rows had strips on the left with the levels of one
> factor, and the columns had strips on the top with the levels of the
> other factor.  Sort of like strip.left() but for just one of the factors.
>
> I can't remember or find how I did it, what options to set. Can anyone
> remind me?
>
> Thanks.
>
> --Chris Ryan
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] [External] Using the pipe, |>, syntax with "names<-"

2024-07-20 Thread Deepayan Sarkar
The main challenge in Bert's original problem is that `[` and `[<-` cannot
be called in a pipeline. The obvious solution is to define named versions,
e.g.:

elt <- `[`
`elt<-` <- `[<-`

Then,

> z <- data.frame(a = 1:3, b = letters[1:3])
> z |> names() |> elt(2)
[1] "b"
> z |> names() |> elt(2) <- "foo"
> z
  a foo
1 1   a
2 2   b
3 3   c

You could actually also do (using a similar function already defined in
methods)

z |> names() |> el(2) <- "bar"

Iris's _ trick is of course a nice alternative; and this example in ?pipeOp
already covers it:

# using the placeholder as the head of an extraction chain:
mtcars |> subset(cyl == 4) |> lm(formula = mpg ~ disp) |> _$coef[[2]]

While the replacement question is a nice exercise, I am not sure about the
value of emphasizing that you can use pipes to do complex assignments.
Doesn't that defeat the whole purpose of piping? For one thing, it will
necessarily terminate the pipe. Also, it will not work if the starting
value is not a variable. E.g.,

> data.frame(a = 1:3, b = letters[1:3]) |> names() |> _[2] <- "bar"
Error in names(data.frame(a = 1:3, b = letters[1:3]))[2] <- "bar" :
  target of assignment expands to non-language object

Duncan's rename() approach, which will just change the column name and
return the modified object, seems more useful as part of a pipeline.

Best,
-Deepayan

On Sun, 21 Jul 2024 at 04:46, Bert Gunter  wrote:

> I second Rich's excellent suggestion.
>
> As with all elegant solutions, Iris's clicked on the wee light bulb in
> my brain, and I realized that a slightly more verbose, but perhaps
> more enlightening, alternative may be:
>
> z |>  attr("names") |> _[2] <- "foo"
>
> However, I would add this as an example *only with* Iris's solution.
> Hers should be shown whether or not the above is.
>
> Cheers,
> Bert
>
> On Sat, Jul 20, 2024 at 3:35 PM Richard M. Heiberger 
> wrote:
> >
> > I think Iris's solution should be added to the help file: ?|>
> > there are no examples there now that show assignment or replacement
> using the "_"
> >
> > > On Jul 20, 2024, at 18:21, Duncan Murdoch 
> wrote:
> > >
> > > On 2024-07-20 6:02 p.m., Iris Simmons wrote:
> > >> z <- data.frame(a = 1:3, b = letters[1:3])
> > >> z |> names() |> _[2] <- "foo"
> > >> z
> > >
> > > That's a great suggestion!
> > >
> > > Duncan Murdoch
> > >
> > > __
> > > R-help@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@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] Output of tapply function as data frame

2024-03-27 Thread Deepayan Sarkar
For more complicated examples, the (relatively new) array2DF()
function is also useful:

> with(data, tapply(count, Date, mean)) |> array2DF()
Var1Value
1 2024-03-23 5.416667
2 2024-03-24 5.50
3 2024-03-25 6.00
4 2024-03-26 4.476190
5 2024-03-27 6.538462
6 2024-03-28 5.20

or

> tapply(data, ~ Date, with, mean(count)) |> array2DF(responseName = "count")
Datecount
1 2024-03-23 5.416667
2 2024-03-24 5.50
3 2024-03-25 6.00
4 2024-03-26 4.476190
5 2024-03-27 6.538462
6 2024-03-28 5.20

Best,
-Deepayan

On Wed, 27 Mar 2024 at 13:15, Rui Barradas  wrote:
>
> Às 04:30 de 27/03/2024, Ogbos Okike escreveu:
> > Warm greetings to you all.
> >
> > Using the tapply function below:
> > data<-read.table("FD1month",col.names = c("Dates","count"))
> > x=data$count
> >   f<-factor(data$Dates)
> > AB<- tapply(x,f,mean)
> >
> >
> > I made a simple calculation. The result, stored in AB, is of the form
> > below. But an effort to write AB to a file as a data frame fails. When I
> > use the write table, it only produces the count column and strip of the
> > first column (date).
> >
> > 2005-11-01 2005-12-01 2006-01-01 2006-02-01 2006-03-01 2006-04-01
> > 2006-05-01
> >   -4.106887  -4.259154  -5.836090  -4.756757  -4.118011  -4.487942
> >   -4.430705
> > 2006-06-01 2006-07-01 2006-08-01 2006-09-01 2006-10-01 2006-11-01
> > 2006-12-01
> >   -3.856727  -6.067103  -6.418767  -4.383031  -3.985805  -4.768196
> > -10.072579
> > 2007-01-01 2007-02-01 2007-03-01 2007-04-01 2007-05-01 2007-06-01
> > 2007-07-01
> >   -5.342338  -4.653128  -4.325094  -4.525373  -4.574783  -3.915600
> >   -4.127980
> > 2007-08-01 2007-09-01 2007-10-01 2007-11-01 2007-12-01 2008-01-01
> > 2008-02-01
> >   -3.952150  -4.033518  -4.532878  -4.522941  -4.485693  -3.922155
> >   -4.183578
> > 2008-03-01 2008-04-01 2008-05-01 2008-06-01 2008-07-01 2008-08-01
> > 2008-09-01
> >   -4.336969  -3.813306  -4.296579  -4.575095  -4.036036  -4.727994
> >   -4.347428
> > 2008-10-01 2008-11-01 2008-12-01
> >   -4.029918  -4.260326  -4.454224
> >
> > But the normal format I wish to display only appears on the terminal,
> > leading me to copy it and paste into a text file. That is, when I enter AB
> > on the terminal, it returns a format in the form:
> >
> > 008-02-01  -4.183578
> > 2008-03-01  -4.336969
> > 2008-04-01  -3.813306
> > 2008-05-01  -4.296579
> > 2008-06-01  -4.575095
> > 2008-07-01  -4.036036
> > 2008-08-01  -4.727994
> > 2008-09-01  -4.347428
> > 2008-10-01  -4.029918
> > 2008-11-01  -4.260326
> > 2008-12-01  -4.454224
> >
> > Now, my question: How do I write out two columns displayed by AB on the
> > terminal to a file?
> >
> > I have tried using AB<-data.frame(AB) but it doesn't work either.
> >
> > Many thanks for your time.
> > Ogbos
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
> Hello,
>
> The main trick is to pipe to as.data.frame. But the result will have one
> column only, you must assign the dates from the df's row names.
> I also include an aggregate solution.
>
>
>
> # create a test data set
> set.seed(2024)
> data <- data.frame(
>Date = sample(seq(Sys.Date() - 5, Sys.Date(), by = "1 days"), 100L,
> TRUE),
>count = sample(10L, 100L, TRUE)
> )
>
> # coerce tapply's result to class "data.frame"
> res <- with(data, tapply(count, Date, mean)) |> as.data.frame()
> # assign a dates column from the row names
> res$Date <- row.names(res)
> # cosmetics
> names(res)[2:1] <- names(data)
> # note that the row names are still tapply's names vector
> # and that the columns order is not Date/count. Both are fixed
> # after the calculations.
> res
> #>   count   Date
> #> 2024-03-22 5.416667 2024-03-22
> #> 2024-03-23 5.50 2024-03-23
> #> 2024-03-24 6.00 2024-03-24
> #> 2024-03-25 4.476190 2024-03-25
> #> 2024-03-26 6.538462 2024-03-26
> #> 2024-03-27 5.20 2024-03-27
>
> # fix the columns' order
> res <- res[2:1]
>
>
>
> # better all in one instruction
> aggregate(count ~ Date, data, mean)
> #> Datecount
> #> 1 2024-03-22 5.416667
> #> 2 2024-03-23 5.50
> #> 3 2024-03-24 6.00
> #> 4 2024-03-25 4.476190
> #> 5 2024-03-26 6.538462
> #> 6 2024-03-27 5.20
>
>
>
> Also,
> I'm glad to help as always but Ogbos, you have been an R-Help
> contributor for quite a while, please post data in dput format. Given
> the problem the output of the following is more than enough.
>
>
> dput(head(data, 20L))
>
>
> Hope this helps,
>
> Rui Barradas
>
>
> --
> Este e-mail foi analisado pelo software antivírus AVG para verificar a 
> presença de vírus.
> www.avg.com
>
> __
> R-help@r-project.org mailing list -- To

Re: [R] refresh.console() function?

2024-03-13 Thread Deepayan Sarkar
Perhaps you mean

https://search.r-project.org/R/refmans/utils/html/flush.console.html

Best,
Deepayan


On Wed, 13 Mar, 2024, 8:59 pm Christofer Bogaso, <
bogaso.christo...@gmail.com> wrote:

> Hi,
>
> I run a lengthy for loop and I want to display loop status for each
> step in my loop.
>
> I previously heard of a R function namely refresh.console() which
> would print the status within the loop as it progresses.
>
> However I see this
>
> > help.search("refresh.console")
>
> No vignettes or demos or help files found with alias or concept or
>
> title matching ‘refresh.console’ using regular expression matching.
>
> Could you please help me find the correct function name?
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] combine barchart and xyplot in lattice

2023-11-18 Thread Deepayan Sarkar
On Sat, 18 Nov 2023 at 06:44, Naresh Gurbuxani
 wrote:
>
> In below graph, I would like to add two vertical lines using
> panel.abline().  Is this possible?

I assume you want the 'v' variable in panel.abline() to be interpreted
in the context of your x-axis, which here represents a factor
variable. Unless two factor variables have the same levels, their
values don't really mean the same thing. So either you need to specify
the levels, or make the axis numeric:

# option 1 - factor
xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
   col = 2, data = vehicles,
   panel = function(x, y, ...) {
   panel.xyplot(x, y, ...)
   panel.abline(v = factor(c("9", "17"), levels = levels(x)),
lty = 2, col = "gray")
   })

# option 2 - numeric
xyplot(count ~ hour, type = "l", lwd = 2,
   col = 2, data = vehicles,
   panel = function(x, y, ...) {
   panel.xyplot(x, y, ...)
   panel.abline(v = c(9, 17), lty = 2, col = "gray")
   })

Best,
-Deepayan

>
> Thanks,
> Naresh
>
> mydf <- data.frame(hour = rep(6:20, 2),
> traffic = c(round(dnorm(6:20, 9, 3) * 1), round(dnorm(6:20, 17, 4) *
> 1)),
> direction = rep(c("inbound", "outbound"), c(15, 15)))
>
> vehicles <- data.frame(hour = 6:20,
> count = c(100, 120, 140, 125, 105, 80, 70, 75, 80, 100, 110, 120, 115,
> 110, 100))
>
> library(lattice)
> library(latticeExtra)
>
> # This works
> mybars <- barchart(traffic ~ as.factor(hour), groups = direction,
> stack = TRUE, horizontal = FALSE, data = mydf,
> auto.key = list(columns = 2, space = "bottom"), xlab = "hour")
>
> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
> col = 2, data = vehicles)
>
> mybars + mylines
>
> # This does not work.  Lines are not correctly placed.
> mylines <- xyplot(count ~ as.factor(hour), type = "l", lwd = 2,
> col = 2, data = vehicles, panel = function(x, y, ...) {
> panel.xyplot(x, y, ...)
> panel.abliine(v = as.factor(c(9, 17)), lty = 2, col = "gray")
> })
> __
> R-help@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@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.


Re: [R] make a lattice dotplot with symbol size proportional to a variable in the plotted dataframe

2023-11-08 Thread Deepayan Sarkar
On Wed, 8 Nov 2023 at 10:56, Christopher W. Ryan via R-help
 wrote:
>
> Very helpful, Deepayan, and educational. Thank you.
>
> What does NSE stand for?

Non-standard evaluation, used widely in formula-interface functions as
well as the tidyverse. with() in my example is a less nuanced version
of this. See

http://adv-r.had.co.nz/Computing-on-the-language.html

https://developer.r-project.org/nonstandard-eval.pdf

Best,
-Deepayan


> Thanks,
> Chris
>
> Deepayan Sarkar wrote:
> >
> > --Chris Ryan
>
> __
> R-help@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@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.


Re: [R] make a lattice dotplot with symbol size proportional to a variable in the plotted dataframe

2023-11-07 Thread Deepayan Sarkar
Handling NSE in these kinds of examples is a pain in lattice. I would
suggest using with() and dropping the data argument for simple examples,
e.g.,

dd |> mutate(new.proportion = las/total, new.bubble = total/100) |>
with(dotplot(agency ~ new.proportion, pch = 16, cex = new.bubble))

But if you care about multi-panel plots, you also need to be careful about
making sure that the 'cex' values get split properly. This is done
generally using the 'subscripts' argument provided to panel functions, so
something like this should be safer:

panel.bubble <- function(x, y, cex, ..., subscripts) {
panel.dotplot(x, y, cex = cex[subscripts], ...)
}

dd |> mutate(new.proportion = las/total, new.bubble = total/100) |>
with(dotplot(agency ~ new.proportion, pch = 16,
 cex = new.bubble, panel = panel.bubble))

Best,
-Deepayan

On Tue, 7 Nov 2023 at 11:03, Christopher Ryan via R-help <
r-help@r-project.org> wrote:

> Hello. My question is in the subject line. Using R 4.1.3 on Windows 10.
> Commented MWE below. Thanks.
>
> --Chris Ryan
>
>
>
> library(dplyr)
> library(lattice)
>
> ## fabricate a dataframe
> dd <- data.frame(agency = sample(LETTERS, size = 5),
> total = sample(100:200, size = 5),
> las = sample(20:40, size = 5))
> dd <- dd %>% mutate(proportion = las/total, bubble = total/100)
>
>
> ## attempt to make a dotplot with symbol size proportional
> ## to the variable named total
>
> dotplot(agency ~ proportion, pch = 16, cex = bubble, data = dd)
> ##  object 'bubble' not found
>
> dotplot(agency ~ proportion, pch = 16, cex = dd$bubble, data = dd)
> ## works
>
>
>
> ## also works in two commands
> external.bubble <- dd$bubble
> dotplot(agency ~ proportion, pch = 16, cex = external.bubble, data = dd)
>
>
>
> ## but how to chain it with pipes, dplyr-style,
> ## modifying the dataframe and then
> ## using the modified version in dotplot, all in one chain?
>
> dd %>% mutate(new.proportion = las/total, new.bubble = total/100) %>%
> dotplot(agency ~ new.proportion, pch = 16, cex = new.bubble, data = .)
> ## object 'new.bubble' not found
>
>
> dd %>% mutate(new.proportion = las/total, new.bubble = total/100) %>%
>  dotplot(agency ~ new.proportion, pch = 16, cex = .$new.bubble, data =
> .)
> ## the .$new.bubble syntax seems to work, but I've never
> ## used or seen that before, and it seems weird.
> ## Is there a "proper" syntax?
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] lattice densityplot with weights

2023-09-29 Thread Deepayan Sarkar
Yes, the following should (mostly) work:

lattice::densityplot(~ x, mydf, groups = name, weights = wt,
auto.key = list(space = "inside"), grid = TRUE)

I just realised that the 'subdensity' and 'warnWbw' arguments are not
passed through by densityplot(), so there is no way to get rid of the
corresponding warnings. But this should be OK otherwise.

On Fri, 29 Sept 2023 at 10:35, Bert Gunter  wrote:
>
> Unless I misunderstand...
>
> See ?panel.densityplot
>
> Lattice functions do their work through a host of panel functions,
> typically passing their ... arguments to the panel functions.
> panel.densityplot has a weights argument.

'weights' needs a bit more work because it has to do NSE like
'groups', but other than that, yes. The ?densityplot help page
documents the 'weight' argument.

Best,
-Deepayan


> Cheers,
> Bert
>
>
>
>
> On Fri, Sep 29, 2023 at 3:32 AM Naresh Gurbuxani <
> naresh_gurbux...@hotmail.com> wrote:
>
> >
> > density() function in R accepts weights as an input.  Using this
> > function, one can calculate density and plot it.  Is it possible to
> > combined these two operations in lattice densityplot()?
> >
> > mydf <- data.frame(name = "A", x = seq(-2.9, 2.9, by = 0.2), wt =
> > diff(pnorm(seq(-3, 3, by = 0.2
> > mydf <- rbind(mydf, data.frame(name = "B", x = mydf$x + 0.5, wt =
> > mydf$wt))
> > with(subset(mydf, name == "A"), density(x, weights = wt / sum(wt)) |>
> > plot(xlim = c(-3, 3.5), xlab = "", main = "Density Plots"))
> > with(subset(mydf, name == "B"), density(x, weights = wt / sum(wt)) |>
> > lines(lty = 2, col = 2))
> > grid()
> > legend("topright", legend = c("A", "B"), col = c(1, 2), lty = c(1, 2),
> > bty = "n")
> >
> > # I want to do something like this:
> > # densityplot(~ x, weights = wt, groups = name, data = mydf, type = c("l",
> > "g"))
> >
> > __
> > R-help@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.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Plotting factors in graph panel

2023-07-11 Thread Deepayan Sarkar
On Wed, 12 Jul 2023 at 08:57, Anupam Tyagi  wrote:

> Thanks.
> I made a graph in Stata that is close to what I want in R. Stata graph is
> attached. The main differences between Stata and R graphs that I was able
> to make, with ggplot or lattice, is that I have been able to scale y-axis
> of each sub-graph independently in Stata, but not in R. Also, x-axis labels
> are also complete in Stata but not in R. Is there a way to do this in R via
> base-R, ggplot or lattice?


Yes, of course. For lattice, add the argument

scales = list(y = "free")

For ggplot2, change

facet_wrap(~Measure)

to

facet_wrap(~Measure, scale = "free")

-Deepayan

I had initially thought that these types of
> panel graphs are common and should be easy to do, but that is not how this
> is turning out to be. Any help is welcome.
>

> On Fri, 7 Jul 2023 at 17:57, PIKAL Petr  wrote:
>
> > Hallo Anupam
> >
> > With
> >
> > ggplot change axis label size into Google
> >
> > the first answer I got was
> >
> > axis.text theme
> >
> > r - Change size of axes title and labels in ggplot2 - Stack Overflow
> > <
> https://stackoverflow.com/questions/14942681/change-size-of-axes-title-and-labels-in-ggplot2
> >
> >
> >
> >
> > so
> >
> >
> >
> > ggplot(TrialData4, aes(x=Income, y=Percent, group=Measure)) +
> geom_point()
> > +
> >   geom_line() + facet_wrap(~Measure) +
> > theme(axis.text=element_text(size=5))
> >
> >
> >
> > Should do the trick.
> >
> >
> >
> > S pozdravem | Best Regards
> >
> >
> > *RNDr. Petr PIKAL*Vedoucí Výzkumu a vývoje | Research Manager
> >
> >
> > *PRECHEZA a.s.*nábř. Dr. Edvarda Beneše 1170/24 | 750 02 Přerov | Czech
> > Republic
> > Tel: +420 581 252 256 | GSM: +420 724 008 364
> > petr.pi...@precheza.cz | *www.precheza.cz <https://www.precheza.cz/>*
> >
> > *Osobní údaje: *Informace o zpracování a ochraně osobních údajů
> > obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: *
> https://www.precheza.cz/zasady-ochrany-osobnich-udaju/
> > <https://www.precheza.cz/zasady-ochrany-osobnich-udaju/>* | Information
> > about processing and protection of business partner’s personal data are
> > available on website: *
> https://www.precheza.cz/en/personal-data-protection-principles/
> > <https://www.precheza.cz/en/personal-data-protection-principles/>*
> >
> > *Důvěrnost: *Tento e-mail a jakékoliv k němu připojené dokumenty jsou
> > důvěrné a podléhají tomuto právně závaznému prohlášení o vyloučení
> > odpovědnosti: *https://www.precheza.cz/01-dovetek/
> > <https://www.precheza.cz/01-dovetek/>* | This email and any documents
> > attached to it may be confidential and are subject to the legally binding
> > disclaimer: *https://www.precheza.cz/en/01-disclaimer/
> > <https://www.precheza.cz/en/01-disclaimer/>*
> >
> >
> >
> > *From:* Anupam Tyagi 
> > *Sent:* Friday, July 7, 2023 12:48 PM
> > *To:* PIKAL Petr 
> > *Cc:* r-help@r-project.org
> > *Subject:* Re: [R] Plotting factors in graph panel
> >
> >
> >
> > Thanks! You are correct, the graphs look very similar, except ggplot is
> > scaling the text font to make it more readable. Is there a way to scale
> > down the x-axis labels, so they are readable?
> >
> >
> >
> > On Fri, 7 Jul 2023 at 12:02, PIKAL Petr  wrote:
> >
> > Hallo Anupam
> >
> > I do not see much difference in ggplot or lattice, they seems to me
> > provide almost identical results when removing theme part from ggplot.
> >
> > library(ggplot2)
> > library(lattice)
> >
> > ggplot(TrialData4, aes(x=Income, y=Percent, group=Measure)) +
> geom_point()
> > +
> >   geom_line() + facet_wrap(~Measure)
> >
> > xyplot(Percent ~ Income | Measure, TrialData4,
> >type = "o", pch = 16, as.table = TRUE, grid = TRUE)
> >
> > So it is probably only matter of your preference which one do you choose.
> >
> > Cheers
> > Petr
> >
> >
> > > -Original Message-
> > > From: R-help  On Behalf Of Deepayan
> Sarkar
> > > Sent: Thursday, July 6, 2023 3:06 PM
> > > To: Anupam Tyagi 
> > > Cc: r-help@r-project.org
> > > Subject: Re: [R] Plotting factors in graph panel
> > >
> > > On Thu, 6 Jul 2023 at 15:21, Anupam Tyagi  wrote:
> > > >
> > > > Btw, I think "lattice" graphics will provide a better solution than
> > > > &q

Re: [R] Plotting factors in graph panel

2023-07-06 Thread Deepayan Sarkar
On Thu, 6 Jul 2023 at 15:21, Anupam Tyagi  wrote:
>
> Btw, I think "lattice" graphics will provide a better solution than
> "ggplot", because it puts appropriate (space saving) markers on the axes
> and does axes labels well. However, I cannot figure out how to do it in
> "lattice".

You will need to convert Income to a factor first. Alternatively, use
dotplot() instead of xyplot(), but that will sort the levels wrongly,
so better to make the factor first anyway.

TrialData4 <- within(TrialData4,
{
Income <- factor(Income, levels = c("$10", "$25", "$40", "$75", "> $75"))
})

xyplot(Percent ~ Income | Measure, TrialData4,
   type = "o", pch = 16, as.table = TRUE, grid = TRUE)

or

dotplot(Percent ~ Income | Measure, TrialData4,
type = "o", as.table = TRUE)

This is not really any different from the ggplot() version though.
Maybe you just don't like the effect of the '+ theme_classic()' part.

Best,
-Deepayan


> On Thu, 6 Jul 2023 at 15:11, Anupam Tyagi  wrote:
>
> > Hi John:
> >
> > Thanks! Below is the data using your suggestion. I used "ggplot" to make a
> > graph. I am not too happy with it. I am looking for something simpler and
> > cleaner. Plot is attached.
> >
> > I also tried "lattice" package, but nothing got plotted with "xyplot"
> > command, because it is looking for a numeric variable on x-axis.
> >
> > ggplot(TrialData4, aes(x=Income, y=Percent, group=Measure)) + geom_point()
> > +
> >   geom_line() + facet_wrap(~Measure) + theme_classic()
> >
> > > dput(TrialData4)structure(list(Income = c("$10", "$25", "$40", "$75", "> 
> > > $75",
> > "$10", "$25", "$40", "$75", "> $75", "$10", "$25", "$40", "$75",
> > "> $75", "$10", "$25", "$40", "$75", "> $75", "$10", "$25", "$40",
> > "$75", "> $75", "$10", "$25", "$40", "$75", "> $75", "$10", "$25",
> > "$40", "$75", "> $75", "$10", "$25", "$40", "$75", "> $75", "$10",
> > "$25", "$40", "$75", "> $75", "$10", "$25", "$40", "$75", "> $75",
> > "$10", "$25", "$40", "$75", "> $75", "$10", "$25", "$40", "$75",
> > "> $75", "$10", "$25", "$40", "$75", "> $75", "$10", "$25", "$40",
> > "$75", "> $75", "$10", "$25", "$40", "$75", "> $75", "$10", "$25",
> > "$40", "$75", "> $75", "$10", "$25", "$40", "$75", "> $75", "$10",
> > "$25", "$40", "$75", "> $75", "$10", "$25", "$40", "$75", "> $75",
> > "$10", "$25", "$40", "$75", "> $75", "$10", "$25", "$40", "$75",
> > "> $75", "$10", "$25", "$40", "$75", "> $75", "$10", "$25", "$40",
> > "$75", "> $75", "$10", "$25", "$40", "$75", "> $75", "$10", "$25",
> > "$40", "$75", "> $75", "$10", "$25", "$40", "$75", "> $75", "$10",
> > "$25", "$40", "$75", "> $75", "$10", "$25", "$40", "$75", "> $75"
> > ), Percent = c(3.052, 2.292, 2.244, 1.706, 1.297, 29.76, 28.79,
> > 29.51, 28.9, 31.67, 31.18, 32.64, 34.31, 35.65, 37.59, 36, 36.27,
> > 33.94, 33.74, 29.44, 46.54, 54.01, 59.1, 62.17, 67.67, 24.75,
> > 24.4, 25, 24.61, 24.02, 25.4, 18.7, 29, 11.48, 7.103, 3.052,
> > 2.292, 2.244, 1.706, 1.297, 29.76, 28.79, 29.51, 28.9, 31.67,
> > 31.18, 32.64, 34.31, 35.65, 37.59, 36, 36.27, 33.94, 33.74, 29.44,
> > 46.54, 54.01, 59.1, 62.17, 67.67, 24.75, 24.4, 25, 24.61, 24.02,
> > 25.4, 18.7, 29, 11.48, 7.103, 3.052, 2.292, 2.244, 1.706, 1.297,
> > 29.76, 28.79, 29.51, 28.9, 31.67, 31.18, 32.64, 34.31, 35.65,
> > 37.59, 36, 36.27, 33.94, 33.74, 29.44, 46.54, 54.01, 59.1, 62.17,
> > 67.67, 24.75, 24.4, 25, 24.61, 24.02, 25.4, 18.7, 29, 11.48,
> > 7.103, 3.052, 2.292, 2.244, 1.706, 1.297, 29.76, 28.79, 29.51,
> > 28.9, 31.67, 31.18, 32.64, 34.31, 35.65, 37.59, 36, 36.27, 33.94,
> > 33.74, 29.44, 46.54, 54.01, 59.1, 62.17, 67.67, 24.75, 24.4,
> > 25, 24.61, 24.02, 25.4, 18.7, 29, 11.48, 7.103), Measure = c("MF None",
> > "MF None", "MF None", "MF None", "MF None", "MF Equity", "MF Equity",
> > "MF Equity", "MF Equity", "MF Equity", "MF Debt", "MF Debt",
> > "MF Debt", "MF Debt", "MF Debt", "MF Hybrid", "MF Hybrid", "MF Hybrid",
> > "MF Hybrid", "MF Hybrid", "Bank None", "Bank None", "Bank None",
> > "Bank None", "Bank None", "Bank Current", "Bank Current", "Bank Current",
> > "Bank Current", "Bank Current", "Bank Savings", "Bank Savings",
> > "Bank Savings", "Bank Savings", "Bank Savings", "MF None 1",
> > "MF None 1", "MF None 1", "MF None 1", "MF None 1", "MF Equity 1",
> > "MF Equity 1", "MF Equity 1", "MF Equity 1", "MF Equity 1", "MF Debt 1",
> > "MF Debt 1", "MF Debt 1", "MF Debt 1", "MF Debt 1", "MF Hybrid 1",
> > "MF Hybrid 1", "MF Hybrid 1", "MF Hybrid 1", "MF Hybrid 1", "Bank None 1",
> > "Bank None 1", "Bank None 1", "Bank None 1", "Bank None 1", "Bank Current 
> > 1",
> > "Bank Current 1", "Bank Current 1", "Bank Current 1", "Bank Current 1",
> > "Bank Savings 1", "Bank Savings 1", "Bank Savings 1", "Bank Savings 1",
> > "Bank Savings 1", "MF None 2", "MF None 2", "MF None 2", "MF None 2",
> > "MF None 2", "MF Equity 2", "MF Equity 2", "MF Equity 2", "MF Equity 2",
> > "MF Equity 2", "MF Debt 2", "MF Debt 2", "MF Debt 2", "MF Debt 2",
> > "MF Debt 2", "MF Hybrid 2", "MF Hybrid 2", "MF Hybrid 2", "MF Hybrid 2"

Re: [R] plot level, velocity, acceleration with one x axis

2023-05-31 Thread Deepayan Sarkar
I think your proposal of modifying plot.ts() to allow 'log' to be
vectorized would be the most natural solution here.

For what it's worth, the details of the implementation and the fact
that you can supply a panel function allows an ugly hack:

pfun <- function(...) {
e <- parent.frame()
e$log <- ""
lines(...)
}
plot(DAX., log = "y", panel = pfun)

This would need to be modified to include a counter in more
complicated cases; we can do without because only the first panel has
a log scale in this example.

-Deepayan

On Thu, Jun 1, 2023 at 12:53 AM Spencer Graves
 wrote:
>
>
>
> On 5/31/23 2:12 PM, Viechtbauer, Wolfgang (NP) wrote:
> > How about using the same 'mar' for all plots, but adding an outer margin?
> >
> > DAX <- EuStockMarkets[, 'DAX']
> > DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX
> > colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> > head(DAX.)
> >
> > par(mfrow=c(3,1), mar=c(1,4.5,0,2), oma=c(3,0,1,0))
> >
> > plot(DAX.[, 1], log='y', ylab='DAX', axes=FALSE)
> > axis(2)
> > box(col='grey')
> >
> > plot(DAX.[, 2], ylab='vel (%)', axes=FALSE)
> > axis(2)
> > box(col='grey')
> >
> > plot(DAX.[, 3], ylab='accel (%)', axes=FALSE)
> > axis(2)
> > box(col='grey')
> > axis(1)
> >
> > Best,
> > Wolfgang
>
>
> That's exactly what I needed.
>
>
> Thanks, Spencer
> >
>
> >> -Original Message-
> >> From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Spencer 
> >> Graves
> >> Sent: Wednesday, 31 May, 2023 17:45
> >> To: Eric Berger
> >> Cc: r-help
> >> Subject: Re: [R] plot level, velocity, acceleration with one x axis
> >>
> >> On 5/31/23 9:20 AM, Eric Berger wrote:
> >>> I sent you an updated response to deal with the redundant copies of the 
> >>> x-axis.
> >>> Re-sending.
> >>>
> >> par(mfrow=c(3,1))
> >> plot(DAX.[, 1], log='y', ylab='DAX', xaxt="n")
> >> plot(DAX.[, 2], ylab='vel (%)', xaxt="n")
> >> plot(DAX.[, 3], ylab='accel (%)')
> >>
> >>I got that.  The primary problem with that is that most of the
> >> vertical space is reserved for axis labels, whether they are printed or
> >> not.  If I squeeze the vertical dimension of the plot, I get, "figure
> >> margins too large".  To control that, I need to set "mar" separately for
> >> each panel, and then the plot regions for each are not the same size.
> >> Using the "layout" function instead of "mfrow" is better, but I don't
> >> see now to make that work consistently without fixing the aspect ratio.
> >> There may be a way in the tidyverse, but I haven't found it yet.  The
> >> only solution I've found so far that makes sense to me is to modify the
> >> code for plot.ts to accept a vector for the log argument, with the
> >> constraint that length(lot) = either 1 or ncol(x) and returning
> >> invisibly an object that would make it feasible for a user to call
> >> axis(2, ...) once for each vertical axis to handle cases where someone
> >> wanted to a vertical scale different from linear and log.  I'd want to
> >> make sure that lines.ts also works with this, because I want to add fits
> >> and predictions.
> >>
> >>Comments?
> >>Thanks,
> >>Spencer Graves
> >>
> >> ** With either of the following plots, if I adjust the aspect ratio by
> >> enlarging or reducing the vertical dimension of the plot, the relative
> >> sizes of the plot regions change.
> >>
> >> DAX <- EuStockMarkets[, 'DAX']
> >> DAX. <- cbind(DAX, diff(log(DAX)), diff(diff(log(DAX
> >> colnames(DAX.) <- c("DAX", 'vel (%)', 'accel (%)')
> >> head(DAX.)
> >>
> >> plot(DAX., log='xy')
> >>
> >> op <- par(mfrow=c(3,1), mar=c(0, 4.1, 4.1, 2.1))
> >> plot(DAX.[, 1], log='y', ylab='DAX', axes=FALSE)
> >> axis(2)
> >> box(col='grey')
> >> par(mar=c(0, 4.1, 0, 2.1))
> >> plot(DAX.[, 2], ylab='vel (%)', axes=FALSE)
> >> axis(2)
> >> box(col='grey')
> >> par(mar=c(5.1, 4.1, 0, 2.1))
> >> plot(DAX.[, 3], ylab='accel (%)', axes=FALSE)
> >> axis(2)
> >> box(col='grey')
> >> axis(1)
> >> par(op)
>
> __
> R-help@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@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.


Re: [R] lattice xyplot: how to change the y-axis labels size using yscale.components.subticks on the right side and how to adapt the ticks number

2023-05-30 Thread Deepayan Sarkar
Thanks for the reproducible example. Could you explain what you want
in a bit more detail? Does the following do more or less what you
want, except that the labels are on the left instead of right?

gr <-
xyplot(value ~ tindexTOP | sensor
 , data = df_rhelp
 , ylab = " "
 , xlab = list("Time (s)", font=9)
 , main = "test yscale.components with axis"
 , subset = sensor %in% paste0("sensor",1:8)
 , layout = c(1,8)
 , between = list(y = 0.5),
 , scales = list(y = list(relation = "free", cex = 0.3, rot = 0)
   , x = list(relation = "same", axs = "i",
  rot = 45, cex = 0.3))
 , type = c("l","g")
 , yscale.components = my_yscale.components.subticks
   ## , xscale.components = xscale.components.subticks
 , strip = F
 , strip.left = strip.custom(
   par.strip.text = list(font=3, cex = 0.5,col="red",lines=3)
   )
 , par.strip.text = list(font=3, cex = 0.5,col="red",lines=2)
 , par.settings = list(plot.line  = list(col = "black")
 , strip.background=list(col="transparent")
 , layout.widths = list(axis.key.padding = 5)
   ))

If you want the same labels on the right as well, you will just need
to uncomment the line

   ans$right <- TRUE

in your my_yscale.components.subticks function.

-Deepayan

On Tue, May 30, 2023 at 12:39 AM Laurent Rhelp  wrote:
>
> Dear R-Help-list,
>
> I want to display many sensors on the same page so I have to adapt
> the size of the y-axis labels and I woul like to adapt the number of
> ticks according to the sensor. I use the yscale.components argument with
> the function yscale.components.subticks: see the code below.
>
> In order to change the size labels on the right I tried to add the line
> ans$left$labels$cex <- 0.3 in yscale.components.subticks but it doesn't
> work. I try to put cex = 0.3 in the scale argument for the y list but it
> doesn't work too.
>
> And since the numeric ranges of sensors are different I would like to
> adapt the ticks number for every sensor. But I didn't undestand how to
> pass the value of the n argument at the yscale.components function and
> how I can use this function according to the current panel.
>
> Thank you
>
> Best regards
>
> Laurent
>
>
> o<-->o
>
> The data:
>
> df_rhelp <- structure(list(tindexTOP = c(4.76837158203125e-07,
> 4.76837158203125e-07,
> 4.76837158203125e-07, 4.76837158203125e-07, 4.76837158203125e-07,
> 4.76837158203125e-07, 4.76837158203125e-07, 4.76837158203125e-07,
> 4.76837158203125e-07, 2.86102294921875e-06, 2.86102294921875e-06,
> 2.86102294921875e-06, 2.86102294921875e-06, 2.86102294921875e-06,
> 2.86102294921875e-06, 2.86102294921875e-06, 2.86102294921875e-06,
> 2.86102294921875e-06, 5.48362731933594e-06, 5.48362731933594e-06,
> 5.48362731933594e-06, 5.48362731933594e-06, 5.48362731933594e-06,
> 5.48362731933594e-06, 5.48362731933594e-06, 5.48362731933594e-06,
> 5.48362731933594e-06, 7.86781311035156e-06, 7.86781311035156e-06,
> 7.86781311035156e-06, 7.86781311035156e-06, 7.86781311035156e-06,
> 7.86781311035156e-06, 7.86781311035156e-06, 7.86781311035156e-06,
> 7.86781311035156e-06, 1.04904174804688e-05, 1.04904174804688e-05,
> 1.04904174804688e-05, 1.04904174804688e-05, 1.04904174804688e-05,
> 1.04904174804688e-05, 1.04904174804688e-05, 1.04904174804688e-05,
> 1.04904174804688e-05, 1.28746032714844e-05, 1.28746032714844e-05,
> 1.28746032714844e-05, 1.28746032714844e-05, 1.28746032714844e-05,
> 1.28746032714844e-05, 1.28746032714844e-05, 1.28746032714844e-05,
> 1.28746032714844e-05, 1.54972076416016e-05, 1.54972076416016e-05,
> 1.54972076416016e-05, 1.54972076416016e-05, 1.54972076416016e-05,
> 1.54972076416016e-05, 1.54972076416016e-05, 1.54972076416016e-05,
> 1.54972076416016e-05, 1.78813934326172e-05, 1.78813934326172e-05,
> 1.78813934326172e-05, 1.78813934326172e-05, 1.78813934326172e-05,
> 1.78813934326172e-05, 1.78813934326172e-05, 1.78813934326172e-05,
> 1.78813934326172e-05, 2.05039978027344e-05, 2.05039978027344e-05,
> 2.05039978027344e-05, 2.05039978027344e-05, 2.05039978027344e-05,
> 2.05039978027344e-05, 2.05039978027344e-05, 2.05039978027344e-05,
> 2.05039978027344e-05, 2.288818359375e-05, 2.288818359375e-05,
> 2.288818359375e-05, 2.288818359375e-05, 2.288818359375e-05,
> 2.288818359375e-05,
> 2.288818359375e-05, 2.288818359375e-05, 2.288818359375e-05,
> 2.55107879638672e-05,
> 2.55107879638672e-05, 2.55107879638672e-05, 2.55107879638672e-05,
> 2.55107879638672e-05, 2.55107879638672e-05, 2.55107879638672e-05,
> 2.55107879638672e-05, 2.55107879638672e-05, 2.78949737548828e-05
> ), sensor = structure(c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L,
> 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L

Re: [R] on lexical scoping....

2023-04-04 Thread Deepayan Sarkar
On Tue, Apr 4, 2023 at 7:26 PM akshay kulkarni 
wrote:

> Dear Members,
>  I have the following code typed at the
> console prompt:
>
> y   <-   x*10
>
> X has not been defined and the above code throws an object not found
> error. That is, the global environment does not contain x.


That is not the correct interpretation of the error. R will happily evaluate

y   <-   pi*10

even if the global environment does not contain pi. The "environments"
where R will look is given by

search()

If you manage to find a package that defines 'x' (and exports it),
attaching it will put the package on the search path, and then your call
will indeed no longer give an error.

-Deepayan

Why doesn't it look further in the environment stack, like that of
> packages? There are thousands of packages that contain the variable named
> x. Of course, that happens if the above code is in a function (or does it?).
>
> What concept of R is at work in this dichotomy?
>
> THanking you,
> Yours sincerely,
> AKSHAY M KULKARNI
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] latticeExtra: how to use doubleYScale when we want to keep the groups of every lattice graph

2023-02-13 Thread Deepayan Sarkar
On Mon, Feb 13, 2023 at 3:48 AM Laurent Rhelp  wrote:
>
> Dear R-Help-list,
>
>   I want to use the doubleYScale function from latticeExtra to overlap two
> lattice graphs (cf. code below). The overlapping works but I lose the
> groups of
> every lattice, there are only two colors. Reading the documentation,
> the arguments style1 and style2 give me the impression that it is not
> possible to keep the groups of every lattice.

I'm mostly guessing from looking at the examples, but is this what you want?

gr_Gx_Tx <- latticeExtra::doubleYScale(gr_Gx, gr_Tx, style1 = 0, style2 = 0)

Best,
-Deepayan

> Is there any possibility to keep the groups of every lattice graph with
> the doubleYScale function ?
>
> Or, might you give me some orientations to realize the graph keeping the
> groups using the lattice package ?
>
> Thank you
>
> Best regards
>
> Laurent
>
> #-o<-->o-
> library(lattice)
> library(latticeExtra)
> ##
> ## creation of the first lattice graph gr_Gx with a group of two elements
> ##
> lattice.par <- trellis.par.get()
> my.superpose.line  <- lattice.par$superpose.line
> my.superpose.line$col[1:2] <- c("black","red")
> gr_Gx <- xyplot( value ~ time | region
>, data = df.m
>, subset = sensor %in% c("G2","G4")
>, group = sensor
>, type ="l"
>, scales = list( y = list(log=10)
> , x = list(relation="free", axs =
> "i", abbreviate=TRUE))
>, yscale.components = yscale.components.log10ticks
>, par.settings = list(
>  superpose.line = my.superpose.line
>)
> )
> print(gr_Gx)
> ##
> ## creation of the second lattice graph gr_Tx with a group of five elements
> ##
> lattice.par <- trellis.par.get()
> my.superpose.line  <- lattice.par$superpose.line
> my.superpose.line$col[1:5] <- c(1,2,3,4,5)
> gr_Tx <-  xyplot( value ~ time | region
> , group = sensor
> , subset = sensor %in% c("T1","T2","T3","T4","T5")
> , data= df.m
> , type = "l"
> , scales = list( x= list( axs = "i", relation = "free"
>   , abbreviate = TRUE))
> , par.settings = list( superpose.line =
> my.superpose.line )
> )
> print(gr_Tx)
> ##
> ## overlap the two lattice graphs
> ## We lost the groups of every lattice
> ##
> gr_Gx_Tx <- latticeExtra::doubleYScale(gr_Gx, gr_Tx)
> print(gr_Gx_Tx)
>
> #o<-->o-
>
> The data :
>
> df.m <- structure(list(time = c(0.000250101089477539, 0.000250101089477539,
>   0.000250101089477539, 0.000250101089477539, 0.000250101089477539,
>   0.000250101089477539, 0.000250101089477539, 0.000500202178955078,
>   0.000500202178955078, 0.000500202178955078, 0.000500202178955078,
>   0.000500202178955078, 0.000500202178955078, 0.000500202178955078,
>   0.000750064849853516, 0.000750064849853516, 0.000750064849853516,
>   0.000750064849853516, 0.000750064849853516, 0.000750064849853516,
>   0.000750064849853516, 0.00100016593933105, 0.00100016593933105,
>   0.00100016593933105, 0.00100016593933105, 0.00100016593933105,
>   0.00100016593933105, 0.00100016593933105, 0.00125002861022949,
>   0.00125002861022949, 0.00125002861022949, 0.00125002861022949,
>   0.00125002861022949, 0.00125002861022949, 0.00125002861022949,
>   0.00150012969970703, 0.00150012969970703, 0.00150012969970703,
>   0.00150012969970703, 0.00150012969970703, 0.00150012969970703,
>   0.00150012969970703, 0.00175023078918457, 0.00175023078918457,
>   0.00175023078918457, 0.00175023078918457, 0.00175023078918457,
>   0.00175023078918457, 0.00175023078918457, 0.0029346008301,
>   0.0029346008301, 0.0029346008301, 0.0029346008301,
>   0.0029346008301, 0.0029346008301, 0.0029346008301,
>   0.00225019454956055, 0.00225019454956055, 0.00225019454956055,
>   0.00225019454956055, 0.00225019454956055, 0.00225019454956055,
>   0.00225019454956055, 0.00250005722045898, 0.00250005722045898,
>   0.00250005722045898, 0.00250005722045898, 0.00250005722045898,
>   0.00250005722045898, 0.00250005722045898, 0.00275015830993652,
>   0.00275015830993652, 0.00275015830993652, 0.00275015830993652,
>   0.00275015830993652, 0.00275015830993652, 0.00275015830993652,
>   0.0032098083496, 0.0032098083496, 0.0032098083496,
>   0.0032098083496, 0.0032098083496, 0.0032098083496,
>   0.0032098083496, 0.0032501220703125, 0.0032501220703125,
>   0.0032501220703125, 0.0032501220703125, 0.0032501220703125,
> 0.0032501220703125,
>   0.0032501220703125, 0.00350022315979004, 0.00350022315979004,
>   0.00350022315979004, 0.00350022315979004, 0.00350022315979004,
>   0.0035002231597

Re: [R] lines through points in lattice legend

2023-01-28 Thread Deepayan Sarkar
On Sat, Jan 28, 2023 at 2:49 PM Kenneth Knoblauch
 wrote:
>
> Hi,
>
> I'm struggling to find if there is a simple way to make the lines and points 
> overlap in a legend for a lattice plot using auto.key.  Here is a toy example 
> of what doesn't work (for me) as the lines and points are adjacent rather 
> than overlapping:
>
> library(lattice)
> d <- data.frame(x = 1:2, y = 1:4, f = factor(letters[1:2]))
>
> xyplot(y ~ x, d, groups = f, type = "b",
> pch = 21:22, fill = c("white", "black"), col = "black",
> par.settings = list(superpose.symbol =
> list(pch = 21:22, fill = c("white", "black"), col = "black"),
> superpose.line = list(col = "black")),
> auto.key = list(space = "right", points = TRUE, lines = TRUE))

Just adding a type = "b" should work (for the lines), so something like:

   auto.key = list(space = "right", type = "b",
   points = FALSE, lines = TRUE)

BTW, once you specify par.settings, you don't need to specify the
parameters again, so you can drop the second line.

Best,
-Deepayan

>
>
> I've seen a few examples on stack.overflow but I haven't been able to make 
> them work, or they seem more complicated then I would think it should be (but 
> I don't exclude that I'm fooling myself there).
>
> R version 4.2.2 Patched (2023-01-04 r83564)
> Platform: x86_64-apple-darwin17.0 (64-bit)
> Running under: macOS Catalina 10.15.7
>
> Matrix products: default
> BLAS:   
> /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
> LAPACK: 
> /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
>
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
>
> attached base packages:
> [1] stats graphics  grDevices utils datasets  methods   base
>
> other attached packages:
> [1] lattice_0.20-45
>
> loaded via a namespace (and not attached):
> [1] compiler_4.2.2 grid_4.2.2
>
>  Thanks for any enlightenment, in advance.
>
> Ken
>
>   ___
>   Kenneth Knoblauch
>  Inserm U1208
>  Stem-cell and Brain Research Institute
>  18 avenue du Doyen Lépine
>  69500 Bron
>  France
>  tel: +33 (0)4 72 91 34 77
>  fax: +33 (0)4 72 91 34 61
>  portable: +33 (0)6 84 10 64 10
>  https://sbri.fr/public-profile/63/single-member/
>
> __
> R-help@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@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.


Re: [R] Bug in R-Help Archives?

2023-01-26 Thread Deepayan Sarkar
>From looking at the headers in John Sorkin's mail, my guess is that he
just replied to the other thread rather than starting a fresh email,
and in his attempts to hide that, was outsmarted by Outlook.

This is based on references to domains such as yahoo.com,
dcn.davis.ca.us, and precheza.cz in the header, which were all
involved in the certification thread.

-Deepayan

On Fri, Jan 27, 2023 at 12:26 PM Rui Barradas  wrote:
>
> Às 06:39 de 27/01/2023, Rui Barradas escreveu:
> > Hello,
> >
> > When consulting the R-Help Archives today I've noticed that the thread
> >
> > Pipe operator
> >
> > started by John Sorkin, Tue Jan 3 17:48:30 CET 2023 is under another
> > thread,
> >
> > R Certification
> >
> > started by Mukesh Ghanshyamdas Lekhrajani.
> >
> > Isn't this a bug in the filing system?
> >
> > Thanks to the list maintainer  Martin Maechler and ETH Zurich for
> > organizing and hosting the list for all of us. It's an invaluable tool
> > that has served so many R users along the years and that surely gives a
> > lot of work organizing and eventual headaches. I hope this is not one of
> > them.
> >
> > Rui Barradas
> >
> > __
> > R-help@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.
>
> Maybe the attached screen capture makes it more clear.
>
> Rui Barradas
>
>
> __
> R-help@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@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.


Re: [R] lattice xyplot: trouble about the use of yscale.components to start the yscale from zero

2023-01-22 Thread Deepayan Sarkar
On Sun, Jan 22, 2023 at 6:48 PM Laurent Rhelp  wrote:

> Dear RHelp-list,
>
>   I want to choice my scale for every panel in a lattice graph according to
>   the ylim range of every panel, with 10 ticks and with a start from 0.
> Also I want to plot a grid
>according to the y ticks (I did that in the panel argument with the
> panel.abline function) .
>   So I decided to use the yscale.components argument and I write the
> script below.
>   Before using the pretty function I introduce the 0 value in my range but
> it does not work : the labels are not displayed and the panel.abline do
> nothing. I am not able to understand why.
>If I comment the line ylim <- c(0,max(lim)) the labels appear but of
> course they do not start from 0.
>

You have mistyped

  ans$left$ticks$at <- tick.at

as

  ans$left$tick$at <- tick.at

You might think that partial matching will help you, but partial matching
doesn't work for assignment (for obvious reasons). Consider

> foo <- list(ticks = list(at = 1:10))
> foo$tick$at <- "bar"
> str(foo)
List of 2
 $ ticks:List of 1
  ..$ at: int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ tick :List of 1
  ..$ at: chr "bar"

I don't know if there is a good argument for this assignment to work at all
(other than that NULL and an empty list are conceptually equivalent), but
it is what it is.

This is why I tend to set options(warnPartialMatchDollar = TRUE). In this
case it would give you an inaccurate warning, but at least it will give you
a warning.

As for the range of the scales, these are determined by 'ylim' and
'prepanel', so you would need to specify one of these.

As you are using scales$axs = "i", you can simply provide ylim = c(0, NA),
which forces the lower limit to 0, and lets the upper limit be decided as
usual.

For finer control, you can specify a prepanel function, e.g.,

prepanel = function(x, y, ...) list(ylim = c(0, max(y)))

Hope this helps,

Best,
-Deepayan


>
> Thank you for your help.
>
> Laurent
>
>
>
>   
> --o<--->o-
> library(lattice)
> library(zoo)
> ##
> ## Rq : mydata is a zoo object
> ##
> mydata <- structure(c(0.190991684047867, 0.186639474048368,
> 0.188562286982088,
>  0.187781290093149, 0.188242724296645, 0.190412570465429,
> 0.188922969182772,
>  0.194037520889193, 0.191973884842229, 0.197032138922638,
> 1.03204611806177,
>  1.02831610106898, 1.0280323738983, 1.03595907392095,
> 1.03316162925952,
>  1.04755124287765, 1.04403986225312, 1.05686325668364,
> 1.04672225664295,
>  1.05462971668107), dim = c(10L, 2L), dimnames = list(c("f1",
>   "f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1"),
> c("col1","col2"))
>   , index = c(1.27904891967773, 1.27909898757935, 1.27915000915527
>, 1.27920007705688, 1.2792489528656, 1.27929902076721,
> 1.27935004234314
>,1.27939891815186, 1.27944993972778, 1.2795762939), class =
> "zoo")
>
> options(digits=17)
>
> yscale.components.n.ticks <- function(lim,...){
>
>ans <- yscale.components.default(lim = lim,...)
>ylim <- lim
>## I want to start from 0
>ylim <- c(0,max(lim))
>tick.at <- pretty(ylim, n=10, min.n = 9)
> cat("lim yscale : ",ylim,"\n")
>
>mylabels_y <- formatC(   tick.at
>   , format = "f"
>   , digits=3
>   , width=9
>   ,flag=" ")
>
>print(cbind( mylabels_y = mylabels_y, tick.at_y = tick.at))
>
>ans$left$tick$at <- tick.at
>ans$left$labels$at <- tick.at
>ans$left$labels$labels <- mylabels_y
>ans
> }
>
>
> xscale.components.n.ticks <- function(lim, ... ){
>
>ans <- xscale.components.default(lim = lim,...)
>tick.at <- pretty(lim,20,9)
>mylabels_x <- formatC(   tick.at
>   , format = "f"
>   , digits=6
>   , width=9
>   , flag=" ")
>
>print(cbind( mylabels_x = mylabels_x, tick.at_x = tick.at))
>
>ans$bottom$tick$at <- tick.at
>ans$bottom$labels$at <- tick.at
>ans$bottom$labels$labels <- mylabels_x
>ans
> }
>
> # to see the x values
> time(mydata)
>
> gr <- xyplot(  mydata
> , main = "title"
> , layout = c(1,2)
> , pch = 20
> , panel = function(...) {
>
>   ylim <- current.panel.limits()$ylim
>   xlim <- current.panel.limits()$xlim
>
>   ## I create here the same ticks that those created
>   ##  in xscale.components and yscale.components
>  ylim <- c(0,max(ylim))
>  cat("ylim panel : ",ylim,"\n")
>  y.tick.at <- pretty(ylim,10,9)
>  print(cbind(y.tick.at.panel= y.tick.at))
>
>   x.tick.at <- pretty(xlim,20,9)
>
>  panel.abline( h

Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function

2022-12-11 Thread Deepayan Sarkar
On Sun, Dec 11, 2022 at 11:05 PM Laurent Rhelp  wrote:
>
> Excellent, it works.
>
> But, may you please explain me how xyplot knows that it has to apply
> panel.bwplot on every groups according to the groups factor ? Because
> there is only one panel. I introduced the groups argument in order to
> apply the bwplot function only on the values of every group.

xyplot() doesn't know that, but panel.bwplot() does. It chooses one of
the variables as the grouping variable (depending on
horizontal=TRUE|FALSE); see ?panel.bwplot.

Best,
-Deepayan

>
>
> Le 11/12/2022 à 17:48, Deepayan Sarkar a écrit :
> > On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp  wrote:
> >> I understand the idea but I did not succeed.
> >>
> >> Here is what I tried:
> >>
> >> ## 1.middles of classes calculation
> >>
> >> m <- tapply(DF$x, groups, mean)
> >>
> >> ## 2. create a new factor columns with the levels deduced from
> >> ## the values of the middles of the classes
> >> ##
> >>
> >> DF$m <- DF$groups
> >> levels(DF$m) <- as.character(m)
> >> DF$m <- as.numeric(levels(DF$m))[DF$m]
> >>
> >> ## 3. I verify with xyplot
> >>
> >> xyplot(   y ~ m
> >> , type = "p"
> >> , data=DF
> >> , scales = list(
> >>   y = list(log=T)
> >>   , x = list(log=T)
> >>
> >> )
> >> )
> >> ## 4. I use the panel.groups and panel.bwplot to display the boxplots
> >> without success
> >>
> >> xyplot(   y ~ m
> >> , groups = groups
> >> , type = "p"
> >> , data=DF
> >> , scales = list(
> >>   y = list(log=T)
> >>   , x = list(log=T)
> >>   , panel = panel.superpose
> >>   ,  panel.groups=function(x,y, group.number,...){
> >> panel.xyplot(x,y,...)
> >> panel.bwplot(x,y,...)
> >>   }
> >>   , horizontal = FALSE
> >>   , box.width = .0001
> >> )
> >> )
> > You have a typo here: x = list(log=T) should be followed by a
> > close-paren to complete scales.
> >
> > But I don't understand from your specification why you need groups and
> > panel.superpose etc. What is wrong with
> >
> > xyplot( y ~ m
> >   ## , groups = groups # why?
> >   , data=DF
> >   , scales = list(
> > y = list(log=T)
> >   , x = list(log=T)
> > )
> >   , panel = panel.bwplot
> >   , horizontal = FALSE
> >   , box.width = .0001
> > )
> >
> > ?
> >
> > And sorry for my wrong calculation of m earlier, but you can simplify
> > your version to
> >
> > m <- tapply(DF$x, groups, mean)
> > DF$m <- as.vector(m[DF$groups])
> >
> > Best,
> > -Deepayan
> >
> >>
> >> thx
> >>
> >>
> >>
> >> Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit :
> >>> Log-scales for the "factor" variable in bwplot() is not allowed.
> >>>
> >>> You could, however, use the panel function panel.bwplot() with
> >>> xyplot(num ~ num). The potential problem with that is the box widths,
> >>> which panel.bwplot() will not know how to compute.
> >>>
> >>> See if the following gives you a reasonable starting point:
> >>>
> >>> DF <- within(DF, m <- tapply(y, groups, mean))
> >>> xyplot(y ~ m, DF, scales = list(log = TRUE),
> >>>  panel = panel.bwplot, horizontal = FALSE,
> >>>  box.width = .0001)
> >>>
> >>> Best,
> >>> -Deepayan
> >>>
> >>> On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp  
> >>> wrote:
> >>>> Dear R-Help list,
> >>>>
> >>>>   I would like to use bwplot from the lattice package with a log 
> >>>> scale
> >>>> both on
> >>>> the x-axis and the y-axis but I do not know how to do that because I do
> >>>> not know
> >>>> how to change the factor x-axis in a numeric x-axis.
> >>>>
> >>>> Here is my example:
> >>>>
> >>>>
> >>>> library(l

Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function

2022-12-11 Thread Deepayan Sarkar
On Sun, Dec 11, 2022 at 2:33 PM Laurent Rhelp  wrote:
>
> I understand the idea but I did not succeed.
>
> Here is what I tried:
>
> ## 1.middles of classes calculation
>
> m <- tapply(DF$x, groups, mean)
>
> ## 2. create a new factor columns with the levels deduced from
> ## the values of the middles of the classes
> ##
>
> DF$m <- DF$groups
> levels(DF$m) <- as.character(m)
> DF$m <- as.numeric(levels(DF$m))[DF$m]
>
> ## 3. I verify with xyplot
>
> xyplot(   y ~ m
>, type = "p"
>, data=DF
>, scales = list(
>  y = list(log=T)
>  , x = list(log=T)
>
>)
> )
> ## 4. I use the panel.groups and panel.bwplot to display the boxplots
> without success
>
> xyplot(   y ~ m
>, groups = groups
>, type = "p"
>, data=DF
>, scales = list(
>  y = list(log=T)
>  , x = list(log=T)
>  , panel = panel.superpose
>  ,  panel.groups=function(x,y, group.number,...){
>panel.xyplot(x,y,...)
>panel.bwplot(x,y,...)
>  }
>  , horizontal = FALSE
>  , box.width = .0001
>)
> )

You have a typo here: x = list(log=T) should be followed by a
close-paren to complete scales.

But I don't understand from your specification why you need groups and
panel.superpose etc. What is wrong with

xyplot( y ~ m
 ## , groups = groups # why?
 , data=DF
 , scales = list(
   y = list(log=T)
 , x = list(log=T)
   )
 , panel = panel.bwplot
 , horizontal = FALSE
 , box.width = .0001
   )

?

And sorry for my wrong calculation of m earlier, but you can simplify
your version to

m <- tapply(DF$x, groups, mean)
DF$m <- as.vector(m[DF$groups])

Best,
-Deepayan

>
>
> thx
>
>
>
> Le 10/12/2022 à 17:02, Deepayan Sarkar a écrit :
> > Log-scales for the "factor" variable in bwplot() is not allowed.
> >
> > You could, however, use the panel function panel.bwplot() with
> > xyplot(num ~ num). The potential problem with that is the box widths,
> > which panel.bwplot() will not know how to compute.
> >
> > See if the following gives you a reasonable starting point:
> >
> > DF <- within(DF, m <- tapply(y, groups, mean))
> > xyplot(y ~ m, DF, scales = list(log = TRUE),
> > panel = panel.bwplot, horizontal = FALSE,
> > box.width = .0001)
> >
> > Best,
> > -Deepayan
> >
> > On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp  wrote:
> >> Dear R-Help list,
> >>
> >>  I would like to use bwplot from the lattice package with a log scale
> >> both on
> >> the x-axis and the y-axis but I do not know how to do that because I do
> >> not know
> >> how to change the factor x-axis in a numeric x-axis.
> >>
> >>Here is my example:
> >>
> >>
> >> library(lattice)
> >>
> >> # the mock data
> >> y <- runif(10,min=0, max=500)
> >> x <- seq(0,500,length=length(y))
> >> # I cut the x variable to create a factor variable in order to calculate
> >> the boxes
> >> groups <- cut(x,10,ordered_result = TRUE)
> >> # creating the dataframe for the lattice functions
> >> DF <- data.frame( x= x , y = y, groups = groups)
> >>
> >>
> >> ## ok for xyplot
> >> xyplot(   y ~ x
> >> , data=DF
> >> , scales = list(
> >>   y = list(log=T)
> >>   , x = list(log=T)
> >>
> >> )
> >> )
> >>
> >> ## ok for bwplot with the log scale for the y-axis
> >> bwplot( y ~ groups
> >> , data=DF
> >> , scales = list(
> >>   y = list(log=T)
> >>   # , x = list(log=T)
> >>
> >> )
> >> )
> >>
> >>
> >>
> >> ## Non ok for bwplot with the log scale for the x-axis
> >> bwplot( y ~ groups
> >>   , data=DF
> >>   , scales = list(
> >> y = list(log=T)
> >>   , x = list(log=T)
> >>
> >>   )
> >> )
> >> which gives an error because the x-axis is a factor, I would like to
> >> replace it
> >> for the display by the meddle of every class for example and put a log
> >> scale on the x-axis.

Re: [R] lattice: how to use a log scale on the x-axis with the bwplot function

2022-12-10 Thread Deepayan Sarkar
Log-scales for the "factor" variable in bwplot() is not allowed.

You could, however, use the panel function panel.bwplot() with
xyplot(num ~ num). The potential problem with that is the box widths,
which panel.bwplot() will not know how to compute.

See if the following gives you a reasonable starting point:

DF <- within(DF, m <- tapply(y, groups, mean))
xyplot(y ~ m, DF, scales = list(log = TRUE),
   panel = panel.bwplot, horizontal = FALSE,
   box.width = .0001)

Best,
-Deepayan

On Sat, Dec 10, 2022 at 7:46 PM Laurent Rhelp  wrote:
>
> Dear R-Help list,
>
> I would like to use bwplot from the lattice package with a log scale
> both on
> the x-axis and the y-axis but I do not know how to do that because I do
> not know
> how to change the factor x-axis in a numeric x-axis.
>
>   Here is my example:
>
>
> library(lattice)
>
> # the mock data
> y <- runif(10,min=0, max=500)
> x <- seq(0,500,length=length(y))
> # I cut the x variable to create a factor variable in order to calculate
> the boxes
> groups <- cut(x,10,ordered_result = TRUE)
> # creating the dataframe for the lattice functions
> DF <- data.frame( x= x , y = y, groups = groups)
>
>
> ## ok for xyplot
> xyplot(   y ~ x
>, data=DF
>, scales = list(
>  y = list(log=T)
>  , x = list(log=T)
>
>)
> )
>
> ## ok for bwplot with the log scale for the y-axis
> bwplot( y ~ groups
>, data=DF
>, scales = list(
>  y = list(log=T)
>  # , x = list(log=T)
>
>)
> )
>
>
>
> ## Non ok for bwplot with the log scale for the x-axis
> bwplot( y ~ groups
>  , data=DF
>  , scales = list(
>y = list(log=T)
>  , x = list(log=T)
>
>  )
> )
> which gives an error because the x-axis is a factor, I would like to
> replace it
> for the display by the meddle of every class for example and put a log
> scale on the x-axis.
>
> Thank you for your help
> Best regards
> Laurent
>
>
>
> --
> Cet e-mail a été vérifié par le logiciel antivirus d'Avast.
> www.avast.com
>
> __
> R-help@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@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.


Re: [R] pairwise.var.test

2022-11-02 Thread Deepayan Sarkar
On Mon, Oct 31, 2022 at 5:30 AM Thomas Subia via R-help
 wrote:
>
> Colleagues,
>
> Thank you all for the timely suggestions. That is appreciated.
>
> What I am really looking for a way to identify difference in group level 
> variance by using multiple comparison intervals. Minitab displays those 
> results in a graph.
>
> This method is described in:
> https://support.minitab.com/en-us/minitab/20/media/pdfs/translate/Multiple_Comparisons_Method_Test_for_Equal_Variances.pdf
>
> I was hoping that R had something similar.

Perhaps you are looking for something like the plot produced by

example(TukeyHSD)

For this you would need confidence intervals for each pairwise
comparison, not just the p-values. Once you have those, recreating the
plot should not be difficult, but I don't know if there is any package
that already does this for you. E.g., car::leveneTest() etc. are
designed for multiple groups and won't give you confidence intervals.

Best,
-Deepayan

>
> I tried a Google search on this but to no avail.
>
> Thomas Subia
>
>
>
>
>
>
> On Sunday, October 30, 2022 at 03:44:54 PM PDT, Rui Barradas 
>  wrote:
>
>
>
>
>
> Às 21:47 de 30/10/2022, Jim Lemon escreveu:
> > Hi Thomas,
> > I have assumed the format of your p-value matrix. This may require
> > some adjustment.
> >
> >A  B  CD  E  F
> > A 1  0.74640.01870.0865  0.0122  0.4693
> > B 0.74641  0.03580.1502  0.0173  0.3240
> > C 0.01870.035810.5131  0.7185  0.0050
> > D 0.08650.15020.51311  0.3240  0.0173
> > E 0.01220.01730.71850.3240  1  0.0029
> > F 0.46930.32400.00500.0173  0.0029  1
> >
> > pvar.mat<-as.matrix(read.table(text=
> >  "1  0.74640.01870.0865  0.0122  0.4693
> >  0.74641  0.03580.1502  0.0173  0.3240
> >  0.01870.035810.5131  0.7185  0.0050
> >  0.08650.15020.51311  0.3240  0.0173
> >  0.01220.01730.71850.3240  1  0.0029
> >  0.46930.32400.00500.0173  0.0029  1",
> >  stringsAsFactors=FALSE))
> > rownames(pvar.mat)<-colnames(pvar.mat)<-LETTERS[1:6]
> > pvar.col<-matrix(NA,nrow=6,ncol=6)
> > pvar.col[pvar.mat < 1]<-"red"
> > pvar.col[pvar.mat < 0.05]<-"orange"
> > pvar.col[pvar.mat < 0.01]<-"green"
> > library(plotrix)
> > par(mar=c(6,4,4,2))
> > color2D.matplot(pvar.mat,cellcolors=pvar.col,
> >  main="P-values for matrix",axes=FALSE)
> > axis(1,at=seq(0.5,5.5,by=1),labels=LETTERS[1:6])
> > axis(2,at=seq(0.5,5.5,by=1),labels=rev(LETTERS[1:6]))
> > color.legend(0,-1.3,2.5,-0.7,c("NA","NS","<0.05","<0.01"),
> >  rect.col=c(NA,"red","orange","green"))
> >
> > Jim
> >
> > On Mon, Oct 31, 2022 at 6:34 AM Thomas Subia via R-help
> >  wrote:
> >>
> >> Colleagues,
> >>
> >> The RVAideMemoire package has a pairwise variance test which one can use 
> >> to identify variance differences between group levels.
> >>
> >> Using the example from this package, 
> >> pairwise.var.test(InsectSprays$count,InsectSprays$spray), we get this 
> >> output:
> >>
> >>  Pairwise comparisons using F tests to compare two variances
> >>
> >> data:  InsectSprays$count and InsectSprays$spray
> >>
> >>A  BCDE
> >> B 0.7464-  -  -  -
> >> C 0.01870.0358-  -  -
> >> D 0.08650.15020.5131--
> >> E 0.01220.01730.71850.3240-
> >> F 0.46930.32400.00500.01730.0029
> >>
> >> P value adjustment method: fdr
> >>
> >> Is there a way to graph the pairwise variance differences so that users 
> >> can easily identify the statistically significant variance differences 
> >> between group levels?
> >>
> >> I can do this using Minitab but I'd prefer using R for this.
> >>
> >> Thomas Subia
> >>
> >> __
> >> R-help@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@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.
>
>
> Hello,
>
> With Jim's data creation code, here is a ggplot graph.
>
> First coerce to data.frame, then reshape to long format.
> Now bin the p-values with the cutpoints 0.01, 0.05 and 1. This is dne
> with ?findInterval.
>
> The colors are assigned in the plot code, based on the binned p.values
> above.
>
>
> library(ggplot2)
>
> pvar.mat |> as.data.frame() -> p

Re: [R] panel.rect and log scale in lattice plot

2022-03-23 Thread Deepayan Sarkar
On Wed, Mar 23, 2022 at 4:08 PM Ivan Krylov  wrote:
>
> On Wed, 23 Mar 2022 09:38:34 +
> "Garbade, Sven via R-help"  wrote:
>
> >cpl <- current.panel.limits()
>
> If you str() the return value of current.panel.limits() from the panel
> function with log-scaling enabled, you can see that it contains the
> logarithm of the y-values, as do the y values themselves. This is
> consistent with ?xyplot saying:
>
> >> Note that this is in reality a transformation of the data, not the
> >> axes. Other than the axis labeling, using this feature is no
> >> different than transforming the data in the formula; e.g.,
> >> ‘scales=list(x = list(log = 2))’ is equivalent to ‘y ~ log2(x)’.
>
> ...although it could be more explicit.
>
> If you take a logarithm of 10 and 500, lrect() should be able to
> produce a rectangle in the right place.

Right, "log scales" in lattice simply transform the data, unlike in
traditional graphics.

If it helps, I could change current.panel.limits() to return
information on whether the data were transformed. For now, a
roundabout way to detect this inside the panel function is to use
trellis.last.object()$y.scales$log etc.

Best,
-Deepayan

>
> --
> Best regards,
> Ivan
>
> __
> R-help@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@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.


Re: [R] Can browseURL open a lynx browser?

2022-03-20 Thread Deepayan Sarkar
On Sun, Mar 20, 2022 at 12:39 PM Ashim Kapoor  wrote:
>
> Dear all,
>
> I wish to do :
>
> browseURL("http://www.google.com","lynx";)
>
> But this is not opening the lynx browser. I am able to open lynx from
> the Debian command line and lynx is on the PATH.

I haven't used lynx in a while, but

browseURL("http://www.google.com";, browser = "elinks -remote")

works for me as long as an elinks session is already running.

Best,
-Deepayan

> When I do :
>
> browseURL("http://www.google.com";)
>
> it does open up the url in Firefox.
>
> When I do ?browseURL it says:
>
>   If ‘browser’ supports remote control and R knows how to
>   perform it, the URL is opened in any already-running browser
>   or a new one if necessary.  This mechanism currently is
>   available for browsers which support the ‘"-remote
>   openURL(...)"’ interface (which includes Mozilla and Opera),
>   Galeon, KDE konqueror (_via_ kfmclient) and the GNOME
>   interface to Mozilla. (Firefox has dropped support, but
>   defaults to using an already-running browser.)  Note that the
>   type of browser is determined from its name, so this
>   mechanism will only be used if the browser is installed under
>   its canonical name.
>
> How can I find out if the -remote openURL interface is supported by lynx?
>
> Thank you,
> Ashim
>
> __
> R-help@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@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.


Re: [R] how to plot numeric variable against several categories with lattice bwplot?

2022-01-17 Thread Deepayan Sarkar
On Mon, Jan 17, 2022 at 7:01 PM PIKAL Petr  wrote:
>
> Hi Luigi
>
> Not sure how to do it in lattice but in ggplot you could use for cycle
>
> for(i in 3:5) {
> print(ggplot(df, aes(x=df[,i], y=y))+geom_boxplot()+ xlab(names(df)[i]))
> }
>
> It is better to put plots to pdf file

Pretty much the same with lattice, e.g.,

flist <- sprintf("y ~ %s", setdiff(names(df), "y"))
plist <- lapply(flist, function(f) bwplot(as.formula(f), df))

print(plist)

You could then use latticeExtra::c.trellis() to combine them into a
single plot, but YMMV.

library(latticeExtra)
names(plist) <- flist
do.call(c, plist)

Best,
-Deepayan


>
> Cheers
> Petr
>
> > -Original Message-
> > From: R-help  On Behalf Of Luigi Marongiu
> > Sent: Monday, January 17, 2022 1:31 PM
> > To: Rolf Turner 
> > Cc: r-help 
> > Subject: Re: [R] how to plot numeric variable against several categories
> with
> > lattice bwplot?
> >
> > Yes, I would like to have a boxplot of y against each of the other
> variables, each
> > into a separate panel, without having to type the command for each pair (I
> have
> > 23 categories in the real data...).
> > Thank you for the solid line code (I thought it might have been a simple
> > parameter to add...)
> >
> > On Mon, Jan 17, 2022 at 4:22 AM Rolf Turner 
> > wrote:
> > >
> > >
> > > On Thu, 13 Jan 2022 20:38:04 +0100
> > > Luigi Marongiu  wrote:
> > >
> > > > Hello,
> > > > I have a numerical variable (x) and a series of categories. I would
> > > > like to make a box plot of x against each of the categories. How can
> > > > I arrange the data so that I can accomplish it with lattice?
> > > > At the moment I got this:
> > > > ```
> > > > df = data.frame(x = c(rep(1,5), rep(2,5), rep(3,5)), y = rnorm(15),
> > > > f1 = rep(letters[1:5],3),
> > > > f2 = rep(letters[7:9],5),
> > > > f3 = c(rep(LETTERS[10:12],4), LETTERS[10:12])) df$x = factor(df$x)
> > > > df$f1 = factor(df$f1)
> > > > df$f2 = factor(df$f2)
> > > > df$f3 = factor(df$f3)
> > > > library(lattice)
> > > > bwplot(y ~ x, data = df, auto.key = TRUE ) ``` Also, is it possible
> > > > to have the wishers in solid line?
> > > > Thank you
> > >
> > > It is not at all clear to me what your are trying to do.  If you
> > > really want "to make a box plot of x against each of the categories",
> > > you could just do:
> > >
> > > bwplot(x ~ f1, data = df, auto.key = TRUE ) bwplot(x ~ f2, data = df,
> > > auto.key = TRUE ) bwplot(x ~ f3, data = df, auto.key = TRUE )
> > >
> > > successively.  But perhaps you meant "y" rather than "x".  Please
> > > clarify what you wish to accomplish.
> > >
> > > As to displaying the *whiskers* as solid lines ... that is one hell of
> > > a good question!  I had to struggle:
> > >
> > > xxx <- trellis.par.get("box.umbrella") xxx$lty <- 1
> > > trellis.par.set(box.umbrella=xxx)
> > > junk <- rnorm(42)
> > > bwplot(junk)
> > >
> > > That gave me a boxplot "lying on its side"; I wanted one "standing up".
> > > After a great deal more struggle I did
> > >
> > > crud <- factor(rep(1,42),labels="")
> > > bwplot(junk ~ crud,ylab="")
> > >
> > > which gave me what I was after.
> > >
> > > Perhaps others who are more knowledgeable than I could chip in with
> > > better ideas.
> > >
> > > But first Luigi must clarify what he wants to obtain.
> > >
> > > cheers,
> > >
> > > Rolf Turner
> > >
> > > --
> > > Honorary Research Fellow
> > > Department of Statistics
> > > University of Auckland
> > > Phone: +64-9-373-7599 ext. 88276
> > >
> >
> >
> > --
> > Best regards,
> > Luigi
> >
> > __
> > R-help@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@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@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.


Re: [R] A technical question on methods for "+"

2021-12-02 Thread Deepayan Sarkar
On Fri, Dec 3, 2021 at 3:14 AM Bert Gunter  wrote:
>
> Those are both helpful comments. Would it not be useful to say
> something along these lines in ?class. ?
> The key point I missed is that there often(but not always) must be an
> *explicit* class **attribute** for method dispatch; and class() does
> not indicate whether its value is explicit or not (just recapitulating
> what you both said). Ops methods are such a case (for anything but
> classes inheriting from numeric?), and perhaps a mention there might
> be useful, too.

?Ops already says

 Note that a method will be used for one of these groups or one of
 its members _only_ if it corresponds to a '"class"' attribute, as
 the internal code dispatches on 'oldClass' and not on 'class'.
 This is for efficiency: having to dispatch on, say, 'Ops.integer'
 would be too slow.

Can you suggest some text to make this clearer?

FWIW, the following seems to work as you would want (at the cost of
global inefficiency):

`+` <- function(e1, e2) UseMethod("+")
`+.default` <- function(e1, e2 = 0) .Primitive("+")(e1, e2)

and then

"+.character" <- function(e1, e2) paste0(e1, e2)

> "jan" + "feb"
[1] "janfeb"
> Reduce("+", letters)
[1] "abcdefghijklmnopqrstuvwxyz"

-Deepayan

> Again, thanks for the help.
>
> Bert
>
> On Thu, Dec 2, 2021 at 12:40 PM Duncan Murdoch  
> wrote:
> >
> > The reason for this behaviour is that finding methods is a lot slower
> > than just evaluating the built-in function.  So R takes the time to
> > determine if there's an attribute named "class" attached, but doesn't go
> > searching further if there isn't one.
> >
> > Duncan Murdoch
> >
> > On 02/12/2021 3:10 p.m., Andrew Simmons wrote:
> > > This is because + dispatches on the class attribute, which a string like
> > > "test" has set to NULL, so it doesn't dispatch. You can add the class
> > > yourself like structure("test", class = "character") and that should work.
> > >
> > > I'm not sure where it's explained, but most primitive functions dispatch 
> > > on
> > > the class attribute, which is different from UseMethod which calls class()
> > > if the class attribute is NULL.
> > >
> > > I think if you want to define something like what you have written, you
> > > could write a function `%+%` use that instead
> > >
> > > On Thu, Dec 2, 2021, 14:32 Bert Gunter  wrote:
> > >
> > >> ... and probably a dumb one and almost certainly not of interest to
> > >> most R users. But anyway...
> > >>
> > >> ?"+" says:
> > >> "The unary and binary arithmetic operators are generic functions:
> > >> methods can be written for them individually or via the Ops group
> > >> generic function. "
> > >>
> > >> So:
> > >> "+.character" <- function(e1, e2) paste0(e1, e2)
> > >> ## but this doesn't 'work':
> > >>> "a" + "b"
> > >> Error in "a" + "b" : non-numeric argument to binary operator
> > >>
> > >> ## but explicitly invoking the method does 'work' :
> > >>> "+.character"('a','b')
> > >> [1] "ab"
> > >>
> > >> ##Note also:
> > >>> methods("+")
> > >> [1] +.character +.Date  +.IDate*+.POSIXt+.trellis*
> > >>
> > >> So what am I failing to understand?
> > >> Thanks.
> > >>
> > >> Bert Gunter
> > >>
> > >> __
> > >> R-help@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.
> > >>
> > >
> > >   [[alternative HTML version deleted]]
> > >
> > > __
> > > R-help@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@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@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.


Re: [R] [External Email] Re: how to make the far right section of a smoother line look different from the rest of that line?

2021-11-16 Thread Deepayan Sarkar
On Wed, Nov 17, 2021 at 1:04 AM Christopher W Ryan via R-help
 wrote:
>
> Thanks Bert, that looks promising.
>
> panel.smoother() is from latticeExtra
>
> https://rdrr.io/cran/latticeExtra/man/panel.smoother.html

I'm a bit unsure about your premise. If I understand correctly, the
data for the last week is incomplete and may change. When it does, the
loess smooth is potentially affected for the preceding weeks as well
(in your case, with span = 0.3, upto around 30% of the preceding
weeks). So wouldn't it be misleading to suggest that the smoother
lines are "tentative" only for the last week or two?

It makes more sense to me to just highlight the last data point as
(potentially) incomplete.

Best,
-Deepayan

> --Chris Ryan
>
> On Tue, Nov 16, 2021 at 2:08 PM Bert Gunter  wrote:
>
> > Where did you get panel.smoother()? There is no such panel function in
> > lattice.
> >
> > Is something like this what you want?
> >
> > x <- 1:100
> > y <- rnorm(100, mean =5)
> > end <- 91 # tentative smooth after this
> > xyplot(y ~x, cutoff = end, col1 = "black", col2 = "red"
> >, panel = function(x, y,  col1, col2, cutoff) {
> >   sqleft <- seq_len(cutoff)
> >   sqright <- seq.int(cutoff +1, length(x))
> >   col <- rep(c(col1,col2), times = c(cutoff, length(x) - cutoff))
> >   panel.points(x, y, col = col)
> >   ylo <- predict(loess(y ~ x))
> >   panel.lines(x[sqleft], ylo[sqleft], col = col1, lwd =2)
> >   panel.lines(x[sqright], ylo[sqright], col = col2, lwd = 2, lty =
> > "dotted")
> >})
> >
> > Notes:
> > 1. This works because of loess default to predict at given x's. Modify as
> > required if you change to another smoother or wish to use different points
> > at which to plot the smoother.
> > 2. This can almost certainly be done by creating a grouping variable to
> > separate the two plotting regimes and might be slicker and more robust with
> > that approach.
> >
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Tue, Nov 16, 2021 at 7:45 AM Christopher W Ryan via R-help <
> > r-help@r-project.org> wrote:
> >
> >> eclrs.3 %>%
> >> mutate(start.week = floor_date(realCollectionDate, unit = "week")) %>%
> >> group_by(start.week, k12) %>%
> >> summarise(n = n(), pctpos = 100 * mean(realResult))  %>%
> >> xyplot(pctpos ~ start.week | k12, col = "red", data = ., layout = c(1,2),
> >> ylab = "percent of test results positive", xlab = "specimen collection
> >> date",  strip = strip.custom(strip.names = c(TRUE, TRUE)),  sub = "The
> >> final week shown may not yet be complete so is likely inaccurate",   panel
> >> = function(...){
> >> panel.xyplot(..., type = "p", cex = 0.8)
> >> panel.smoother(..., span = 0.3, col.se = "red", alpha.se = 0.08, lwd
> >> =
> >> 2)})
> >>
> >> The above takes patient-level data, each record containing a date, and
> >> aggregates them by week according to that date, then plots 2 weekly time
> >> series, one for k12 and one for not-k12, each with a smoother. Note my
> >> disclaimer in the subtitle that "the final week shown may not yet be
> >> complete . . . ." since I might run this on any arbitrary day. How might I
> >> change the appearance of the smoother lines to emphasize to the viewer
> >> that
> >> the recent trends are tentative due to daily data still coming in?  For
> >> example, how might I make the far right-end stretch of the lines,
> >> representing the most recent week or two, dotted?
> >>
> >> Thanks.
> >>
> >> --Chris Ryan
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> __
> >> R-help@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.
> >>
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Environmental oddity.

2021-11-06 Thread Deepayan Sarkar
On Sun, Nov 7, 2021 at 6:05 AM Rolf Turner  wrote:
>
>
> I have two functions which appear to differ only in their environments.
> They look like:
>
> > d1
> > function (x, mean = 0, sd = 1, log = FALSE)
> > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd
> > 
>
> and
>
> > d2
> > function (x, mean = 0, sd = 1, log = FALSE)
> > (((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd)/sd
>
> Typing "environment(d1)" gives
>
> > 
>
> and typing "environment(d2)" gives
>
> > 
>
> The d2() function however gives an incorrect result:
>
> > d1(1,0,3,TRUE)
> > [1] -0.2962963
> > d2(1,0,3,TRUE)
> > [1] -0.889

It can't be as simple as that. I get the same result (as your d2) with
the following:

d <- function (x, mean = 0, sd = 1, log = FALSE) {
(((x - mean)/sd)^2 - 1) * if (log) 1 else dnorm(x, mean, sd) / sd
}
d(1, 0, 3, TRUE)
environment(d)
environment(d) <- as.environment("package:stats")
d(1, 0, 3, TRUE)

> In d2() the result of the if() statement does not get divided
> by the final "sd" whereas in d1() it does (which is the desired/correct
> result).
>
> Of course the code is ridiculously kludgy (it was produced by "symbolic
> differentiation").  That's not the point.  I'm just curious (idly?) as
> to *why* the association of the namespace:stats environment with d1()
> causes it to "do the right thing".

This sounds like a difference in precedence. The expression

if (log) 1 else dnorm(x, mean, sd) / sd

is apparently being interpreted differently as

d1: (if (log) 1 else dnorm(x, mean, sd)) / sd
d2: if (log) 1 else (dnorm(x, mean, sd)) / sd)

It's unclear how environments could affect this, so it would be very
helpful to have a reproducible example.

Best,
-Deepayan

> Can anyone give me any insight?  Ta.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> R-help@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@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.


Re: [R] How to rotate only one panel by 90 degrees in R plot?

2021-10-13 Thread Deepayan Sarkar
On Wed, Oct 13, 2021 at 12:13 PM Luigi Marongiu
 wrote:
>
> I have seen that the only package that easily rotate the plot is
> ggplot, so I ran:
> ```
> library(ggplot2)
> df = data.frame(MR = c(negative_mr, uncertain_mr, positive_mr),
> FCN = c(negative_fcn, uncertain_fcn, positive_fcn))
> p <- ggplot(df, aes(x=MR)) +
> geom_density()
> p + coord_flip()
> ```
> Even in this case, the plot is correctly rotated, but I can't place it
> in the allocated panel. ggplot2 simply overwrites the whole plot. This
> means I need to do the whole thing in ggplot2 (would lattice have an
> equivalent?) and split the plot into uneven panels with ggplot2.
> Changing x into y is a clever approach, but it is not the same as
> rotating a plot. But YES, that is exactly what I wanted to plot. Thank
> you!

Well, traditional R graphics (as well as lattice) requires a more DIY
approach. As Paul already indicated, you need to control what you are
plotting instead of relying on plot(density(...)) doing the right
thing.

Modifying your original code:

```
layout(matrix(c(1,2),nrow=1), widths=c(3,1)) # split panels unevenly

plot(negative_x, negative_y, ylim=c(0,0.5), xlim=c(0,41), cex=1.5,
 xlab=expression(bold("X")),
 ylab=expression(bold("Y")))
points(positive_x, positive_y, pch=16, cex=1.5)
points(uncertain_x, uncertain_y, pch=16, cex=1.5, col="grey")
legend("topleft",
   legend = c("Positives", "Negatives", "Uncertains"),
   pch = c(16, 1, 16), col=c("black", "black", "grey"), cex=0.8)

## calculate density (but don't plot yet)
d <- density(c(negative_y, uncertain_y, positive_y))

## Your original code is equivalent to this
## plot(d,
##  yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)

plot(d$y, d$x, type = "l",
 ##yaxt="n",
 xaxt="n", main=NA, ylab=NA, xlab=NA)
```

Note that in the last plot, I have plot(d$y, d$x, ...) instead of
plot(d$x, d$y, ...).

I have commented out your yaxt="n" to highlight something that may not
be initially obvious, which is that the axis limits of your two plots
do not match. To ensure that, you would additionally need to match
ylim:

```
plot(d$y, d$x, type = "l", ylim=c(0,0.5),
 yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)
```

Best,
-Deepayan

> On Wed, Oct 13, 2021 at 1:17 AM Bert Gunter  wrote:
> >
> > I don't know the gridGraphics package, and I haven't looked closely at what 
> > you are trying to do. But note that lattice functions construct grid 
> > "grobs" that can be saved and plotted in arbitrary, including rotated, 
> > viewports, using the print.trellis function. I frankly am pretty ignorant 
> > about such things, but this simple little example might give you some 
> > notion of how to proceed. You may also be able to do what you want with 
> > grid.layout() and pushing a suitably rotated viewport onto a layout. Others 
> > would have to advise on such details, if so.
> >
> > If I'm wrong and this is useless, just ignore without comment.
> >
> >
> >
> > dp <- densityplot(~y, main = "",
> >   xlab = "", ylab = "")
> > grid.newpage()
> > pushViewport(
> >viewport(width = unit(.5,"npc"),
> > height = unit(.3,"npc"),
> > angle = 270))
> > print(dp, newp = FALSE,  ## this is the print.trellis method
> >   panel.width = list(1,"npc"),
> >   panel.height = list(1, "npc")
> > )
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along and 
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Tue, Oct 12, 2021 at 1:43 PM Luigi Marongiu  
> > wrote:
> >>
> >> Hello,
> >> I would like to show a density plot of the Y axis. To do that, I would
> >> like to split the plot into a panel 2/3 long and a density plot 1/3
> >> long. The problem is that, since the density is on the Y axis, the
> >> density plot should be rotated byb90 degrees. I tried with the package
> >> gridGraphics but it rotates both panels.
> >> ```
> >> negative_y <- runif(50, 0, 0.099)
> >> negative_x <- runif(50, 1, 40)
> >> positive_y <- c(runif(30, 0.2, 0.5), runif(20, 0.4, 0.5))
> >> positive_x <- c(runif(30, 25, 40), runif(20, 10, 25))
> >> uncertain_y <- runif(10, 0.099, 0.2)
> >> uncertain_x <- runif(10, 2, 40)
> >> # plot on MR/FCN space
> >> layout(matrix(c(1,2),nrow=1), widths=c(3,1)) # split panels unevenly
> >> plot(negative_x, negative_y, ylim=c(0,0.5), xlim=c(0,41), cex=1.5,
> >> xlab=expression(bold("X")),
> >> ylab=expression(bold("Y")))
> >> points(positive_x, positive_y, pch=16, cex=1.5)
> >> points(uncertain_x, uncertain_y, pch=16, cex=1.5, col="grey")
> >> legend("topleft",
> >> legend = c("Positives", "Negatives", "Uncertains"),
> >> pch = c(16, 1, 16), col=c("black", "black", "grey"), cex=0.8)
> >> # plot density
> >> plot(density(c(negative_y, uncertain_y, positive_y)),
> >> yaxt="n", xaxt="n", main=NA, ylab=NA, xlab=NA)
> >> library(gridGraphics)
> >> grab_grob <- function(){
> >> grid.echo()
> >> grid.grab()
> >> }
> >

Re: [R] How to add error bars to lattice xyplot

2021-10-11 Thread Deepayan Sarkar
On Mon, Oct 11, 2021 at 5:41 PM Luigi Marongiu  wrote:
>
> Hello,
> I am trying to plot data using lattice. The basic plot works:
> ```
> Substance = rep(c("A", "B", "C", "D"),4)
> Concentration = rep(1:4,4),
> Value = c(62.8067, 116.2633,  92.2600,   9.8733,  14.8233,
>   92.3733, 98.9567,   1.4833,   0.6467,  50.6600,
>   25.7533,   0.6900, 0.2167,   7.4067,   6.9200,
>   0.0633)
> df = data.frame(Substance, Concentration, Value, stringsAsFactors = FALSE)
> Value = c(15.2974126, 16.3196089, 57.4294280,  9.1943370, 20.5567321,
> 14.0874424,
>38.3626672, 0.3780653,  0.4738495, 37.9124874, 16.2473916,  0.7218726,
>0.2498666,  8.4537585, 10.8058456,  0.1096966)
> dfsd = data.frame(Substance, Concentration, Value, stringsAsFactors = FALSE)
>
> library(lattice)
> COLS = c("gold", "forestgreen", "darkslategray3", "purple")
> xyplot(Value ~ Concentration,
>group = Substance, data = df,
>pch = 16, cex = 1.2, type = "b",
>xlab=expression(bold(paste("Concentration (", mu, "M)"))),
>ylab=expression(bold("Infection rate")),
>col=COLS,
>scales = list(x = list(log = 10, at=c(unique(df$Concentration))
>)
>),
>key = list(space="top", columns=4, col = "black",
>   points=list(pch=c(16, 16, 16, 16),
>   col=COLS,
>   text=list(c("6-PN", "8-PN", "IX", "XN")
>   )
>   )
>)
>
> )
> ```
> but how do I add the error bars?
> I tried with
> ```
> xyplot(Value ~ Concentration,
>group = Substance, data = df,
>pch = 16, cex = 1.2, type = "b",
>xlab=expression(bold(paste("Concentration (", mu, "M)"))),
>ylab=expression(bold("Infection rate")),
>col=COLS,
>scales = list(x = list(log = 10, at=c(unique(df$Concentration))
>)
>),
>key = list(space="top", columns=4, col = "black",
>   points=list(pch=c(16, 16, 16, 16),
>   col=COLS,
>   text=list(c("6-PN", "8-PN", "IX", "XN")
>   )
>   )
>),
>panel = function (x,y,) {
>  panel.segments(x0 = df$Concentration, x1 = df$Concentration,
> y0 = df$Value - dfsd$Value,
> y1 = df$Value + dfsd$Value,
> col = COLS)
>}
>
> )
> ```
> but the bars are plotted outside the graph.

You need to apply the log-transformation yourself, e.g.,

 panel.segments(x0 = log10(df$Concentration), x1 =
log10(df$Concentration),

But this is not really a scalable approach. You should check if
Hmisc::xYplot suits your needs:

https://search.r-project.org/CRAN/refmans/Hmisc/html/xYplot.html

Best,
-Deepayan

> What is the correct syntax? can I use raw data instead of making the
> mean and std dev separately?
> Thanks
>
> --
> Best regards,
> Luigi
>
> __
> R-help@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@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.


Re: [R] Missing text in lattice key legend

2021-10-11 Thread Deepayan Sarkar
On Mon, Oct 11, 2021 at 5:17 PM Luigi Marongiu  wrote:
>
> Hello,
> I am drawing some data with lattice using:
> ```
> library(lattice)
> COLS = c("gold", "forestgreen", "darkslategray3", "purple")
> xyplot(Value ~ Concentration,
>group = Substance, data = inf_avg,
>pch = 16, cex = 1.2, type = "b",
>xlab=expression(bold(paste("Concentration (", mu, "M)"))),
>ylab=expression(bold("Infection rate")),
>col=COLS,
>scales = list(x = list(log = 10, at=c(unique(inf_avg$Concentration))
>   )
>  ),
>key = list(space="top", columns=4, col = "black",
>points=list(pch=c(16, 16, 16, 16),
>col=COLS,
>text=list(c("6-PN", "8-PN", "IX", "XN")
> )
>)
>   ),
>panel = function(x,y) {
>  panel.xyplot(x,y)
>  errbar()
>}
> )
> ```
> It all works but the legend only shows the colored dots, there is no
> text. Is it something missing from the syntax?

Your text component is nested inside the points component. I think you
want it outside, e.g.,

xyplot(1 ~ 1,
   key = list(space="top", columns=4, col = "black",
  points=list(pch=c(16, 16, 16, 16),
  col=COLS),
  text=list(c("6-PN", "8-PN", "IX", "XN"))
  ))

Best,
-Deepayan

> Thanks
>
> --
> Best regards,
> Luigi
>
> __
> R-help@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@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.


Re: [R] How to use ifelse without invoking warnings

2021-10-08 Thread Deepayan Sarkar
On Sat, Oct 9, 2021 at 3:00 AM Ravi Varadhan via R-help
 wrote:
>
> Thank you to Bert, Sarah, and John. I did consider suppressing warnings, but 
> I felt that
> there must be a more principled approach.  While John's solution is what I 
> would prefer,
> I cannot help but wonder why `ifelse' was not constructed to avoid this 
> behavior.

Note that John's approach is mentioned twice in the help page for
ifelse, once in the examples, and once quite early on in the "Warning"
section:

 Sometimes it is better to use a construction such as

   (tmp <- yes; tmp[!test] <- no[!test]; tmp)

 , possibly extended to handle missing values in 'test'.

Best,
-Deepayan

> Thanks & Best regards,
> Ravi
> 
> From: John Fox 
> Sent: Thursday, October 7, 2021 2:00 PM
> To: Ravi Varadhan 
> Cc: R-Help 
> Subject: Re: [R] How to use ifelse without invoking warnings
>
>
>   External Email - Use Caution
>
>
>
> Dear Ravi,
>
> It's already been suggested that you could disable warnings, but that's
> risky in case there's a warning that you didn't anticipate. Here's a
> different approach:
>
>  > kk <- k[k >= -1 & k <= n]
>  > ans <- numeric(length(k))
>  > ans[k > n] <- 1
>  > ans[k >= -1 & k <= n] <- pbeta(p, kk + 1, n - kk, lower.tail=FALSE)
>  > ans
> [1] 0.0 0.006821826 0.254991551 1.0
>
> BTW, I don't think that you mentioned that p = 0.3, but that seems
> apparent from the output you showed.
>
> I hope this helps,
>   John
>
> --
> John Fox, Professor Emeritus
> McMaster University
> Hamilton, Ontario, Canada
> web: 
> https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsocialsciences.mcmaster.ca%2Fjfox%2F&data=04%7C01%7Cravi.varadhan%40jhu.edu%7Cfd882e7c4f4349db34e108d989bc6a9f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637692265160038474%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=Q33yXm36BwEVKUWO72CWFpSUx7gcEEXhM3qFi7n78ZM%3D&reserved=0
>
> On 2021-10-07 12:29 p.m., Ravi Varadhan via R-help wrote:
> > Hi,
> > I would like to execute the following vectorized calculation:
> >
> >ans <- ifelse (k >= -1 & k <= n, pbeta(p, k+1, n-k, lower.tail = FALSE), 
> > ifelse (k < -1, 0, 1) )
> >
> > For example:
> >
> >
> >> k <- c(-1.2,-0.5, 1.5, 10.4)
> >> n <- 10
> >> ans <- ifelse (k >= -1 & k <= n, pbeta(p,k+1,n-k,lower.tail=FALSE), ifelse 
> >> (k < -1, 0, 1) )
> > Warning message:
> > In pbeta(p, k + 1, n - k, lower.tail = FALSE) : NaNs produced
> >> print(ans)
> > [1] 0.0 0.006821826 0.254991551 1.0
> >
> > The answer is correct.  However, I would like to eliminate the annoying 
> > warnings.  Is there a better way to do this?
> >
> > Thank you,
> > Ravi
> >
> >
> >   [[alternative HTML version deleted]]
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://nam02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Cravi.varadhan%40jhu.edu%7Cfd882e7c4f4349db34e108d989bc6a9f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637692265160048428%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=FXX%2B4zNT0JHBnDFO5dXBDQ484oQF1EK5%2Fa0dG9P%2F4k4%3D&reserved=0
> > PLEASE do read the posting guide 
> > https://nam02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Cravi.varadhan%40jhu.edu%7Cfd882e7c4f4349db34e108d989bc6a9f%7C9fa4f438b1e6473b803f86f8aedf0dec%7C0%7C0%7C637692265160048428%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000&sdata=ss2ohzJIY6qj0eAexk4yVzTzbjXxK5VZNors0GpsbA0%3D&reserved=0
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Show only header of str() function

2021-09-02 Thread Deepayan Sarkar
On Thu, Sep 2, 2021 at 9:26 PM Enrico Schumann  wrote:
>
> On Thu, 02 Sep 2021, Luigi Marongiu writes:
>
> > Hello, is it possible to show only the header (that is: `'data.frame':
> > x obs. of  y variables:` part) of the str function?
> > Thank you
>
> Perhaps one more solution. You could limit the number
> of list components to be printed, though it will leave
> a "truncated" message.
>
> str(iris, list.len = 0)
> ## 'data.frame':150 obs. of  5 variables:
> ##   [list output truncated]

Or use 'max.level', which is also generally useful for nested lists:

str(iris, max.level=0)
## 'data.frame':150 obs. of  5 variables:

Best,
-Deepayan

> Since 'str' is a generic function, you could also
> define a new 'str' method. Perhaps something among
> those lines:
>
> str.data.frame.oneline <- function (object, ...) {
> cat("'data.frame':\t", nrow(object), " obs. of  ",
> (p <- length(object)),
> " variable", if (p != 1) "s", "\n", sep = "")
> invisible(NULL)
> }
>
> (which is essentially taken from 'str.data.frame').
>
> Then:
>
> class(iris) <- c("data.frame.oneline", class(iris))
>
> str(iris)
> ## 'data.frame':  150 obs. of  5 variables
>
> str(list(a = 1,
>  list(b = 2,
>   c = iris)))
> ## List of 2
> ##  $ a: num 1
> ##  $  :List of 2
> ##   ..$ b: num 2
> ##   ..$ c:'data.frame':   150 obs. of  5 variables
>
>
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enricoschumann.net
>
> __
> R-help@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@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.


Re: [R] interactively getting alist of functions for a given package?

2021-06-24 Thread Deepayan Sarkar
On Thu, Jun 24, 2021 at 2:10 PM Greg Minshall  wrote:
>
> Duncan,
>
> > Bert gave you an answer that depends on ls().  Whether there's
> > something like "set" that can return "asset" probably depends on
> > your front end, and may be customizable using the facilities described
> > in ?rcompgen.
>
> thanks.  i am just using command line R on linux.  i tried setting
> `rc.setting(fuzzy=TRUE, func=TRUE, ipck=TRUE)`, but
> `data.table::set` still only shows names that start with "set".
> Bert's `ls` answer works, but did you have an idea of how else this
> might be made to work with rcompgen?

That's not currently supported. It would not be difficult to get the
completion mechanism to return these matches; e.g.,

utils:::findMatches("set", ls(getNamespace("data.table")))

You can even experiment a bit with something like (only for normal completions)

complete.partial <- function(.CompletionEnv)
{
text <- .CompletionEnv[["token"]]
comps <- apropos(text)
.CompletionEnv[["comps"]] <- comps
}

rc.options(custom.completer = complete.partial)

The problem is that readline's interface doesn't really allow you to
choose one of these completions easily, and you will need to
explicitly type out at least the prefix.

Another problem with namespace completion in particular is that
readline will first complete to a non-trivial common prefix, if any.
This means that if

data.table::set

gives multiple completions, all starting with "data.table::", readline
will delete the "set" part, and subsequent attempts will just try to
complete "data.table::".

So adding an option to allow apropos-type matches is not difficult,
but given that RStudio has its own completion, and the limited
functionality of readline, not sure it's worth the effort.

Best,
-Deepayan


> cheers, Greg
>
> __
> R-help@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@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.


Re: [R] Variable labels

2021-05-14 Thread Deepayan Sarkar
On Fri, May 14, 2021 at 4:19 PM Steven Yen  wrote:
>
> Never mind what I said about "Clickable". All I meant was I created an
> item "definitions" that appears after I load the binary file, and that I
> can "click" (don's ask me what I mean by "click") the item in RStudio to
> read its contents -- variable definitions.

I _think_ what you see as 'clickable' items are the names of variables
in your workspace. So if you need to make some attributes clickable,
you need to save them with a new name. E.g.,

f <- 
foreign::read.dta("https://sites.google.com/site/md4stata/linked/international-macroeconomic-data-set/ERS_USDA_Historical.dta";)

f.attrs <- attributes(f)

'f.attrs' should now be visible and 'clickable'. But in theory, this
could also now contain more attributes that you cannot see in the
RStudio inspector, and you would have no way of knowing...

Hope that helps,

-Deepayan

> All I want to know at this pointis, is whether my way of getting the
> definitions in the environment "clumsy" and whether there are better
> ways to do it. Yes, you mention "attr..." but that is not as simple as
> viewing it in RStudio's environment pane. Thank you!
>
> On 2021/5/14 下午 06:37, PIKAL Petr wrote:
> > Hm. What do you mean by "clickable".
> >
> > #I can save any objects to a file
> > save(mydata,definitions, file="test.R")
> > rm("mydata", "definitions")
> >
> > #load them back
> > load("test.R")
> >
> > #but it does not make them "clickable". Point and click is something I am 
> > familiar with in Excel or similar programs byt not in R.
> >
> > #objects are back in the environment and one can inspect them by regular 
> > way (print, str, head, ...)
> > mydata
> >id age yrmarry
> > 1  1  35   4
> > 2  2  31   6
> > 3  3  21   4
> > 4  4  20   3
> > 5  5  19   7
> > 6  6  24   5
> > definitions
> > var.labels
> > id  Individual ID
> > age  Age in Years
> > yrmarry Years of marriage
> >
> > If you want definitions to be part of the data file just use attr.
> >
> > attr(mydata, "var.labels") <- definitions$var.labels
> >
> >   attributes(mydata)
> > $names
> > [1] "id"  "age" "yrmarry"
> >
> > $class
> > [1] "data.frame"
> >
> > $row.names
> > [1] 1 2 3 4 5 6
> >
> > $var.labels
> > [1] "Individual ID" "Age in Years"  "Years of marriage"
> >
> > Cheers
> > Petr
> >
> >> -Original Message-
> >> From: R-help  On Behalf Of Steven Yen
> >> Sent: Friday, May 14, 2021 11:20 AM
> >> To: Jim Lemon 
> >> Cc: R-help Mailing List 
> >> Subject: Re: [R] Variable labels
> >>
> >> Thanks to all, for bearing with me.
> >>
> >> Now I realize expss may not be what I need. I have now written a self-
> >> runnable, replicable set of codes (listed below). Perhaps that gives an 
> >> idea of
> >> what I need. Question is, whethet this is the right way to do this (to 
> >> have a
> >> clickable object to learn about variable
> >> definitions) or whether there are better ways. Thanks!
> >>
> >> Steven
> >>
> >> rm(list=ls())
> >> n<-6
> >> mydata<-data.frame(id=1:n,
> >>  age=floor(rnorm(n,25,10)),
> >>  yrmarry=floor(rnorm(n,5,2))) var.labels<-c(id  = 
> >> "Individual ID",
> >> age = "Age in Years",
> >> yrmarry = "Years of marriage")
> >> definitions<-as.data.frame(var.labels) # declare definitions as a data 
> >> frame
> >> save.image("c:/temp/a/try1.RData") # save binary .RData file
> >> rm(list=ls())  # clean environment
> >> load("c:/temp/a/try1.RData") # now load .RData file and definitions are
> >> clickable
> >># all I need is for user to be able to click
> >># and read the variable definitions
> >>
> >> On 2021/5/14 下午 05:15, Jim Lemon wrote:
> >>> Hi Steven,
> >>> I just happened to scan Petr's message to you and wondered if you were
> >>> looking for something related to the "describe" function in the
> >>> prettyR package (and a few others). For instance, if you do this:
> >>>
> >>> library(prettyR)
> >>> describe(mtcars)
> >>>
> >>> you get this:
> >>>
> >>> Description of mtcars
> >>>
> >>> Numeric
> >>> mean median  var sd valid.n
> >>> mpg   20.09  19.2036.32   6.03  32
> >>> cyl6.19   6.00 3.19   1.79  32
> >>> disp 230.72 196.30 15360.80 123.94  32
> >>> hp   146.69 123.00  4700.87  68.56  32
> >>> drat   3.60   3.70 0.29   0.53  32
> >>> wt 3.22   3.33 0.96   0.98  32
> >>> qsec  17.85  17.71 3.19   1.79  32
> >>> vs 0.44   0.00 0.25   0.50  32
> >>> am 0.41   0.00 0.25   0.50  32
> >>> gear   3.69   4.00 0.54   0.74  32
> >>> carb   2.81   2.00 2.61   1.62  32
> >>>
> >>> However, you can call almost any summary function as an argument to
> >>> describe. Suppose I wrote a function "fackey" that produced this
> >>> output on a factor variable "city":
> >>>
> >>> fack

Re: [R] Matrix::solve() with 1-d arrays -- treating "array" as "numeric"?

2021-04-18 Thread Deepayan Sarkar
On Sat, Apr 17, 2021 at 9:08 PM Martin Maechler
 wrote:
>
> >>>>> Deepayan Sarkar
> >>>>> on Fri, 16 Apr 2021 11:34:20 +0530 writes:
>
> > I get what I initially thought was unexpected behaviour from:
>
> > x <- tapply(runif(100), sample(5, 100, TRUE), mean)
> > solve(Diagonal(5), x)
> > # Error: not-yet-implemented method for solve(, ).
> > #  ->>  Ask the package authors to implement the missing feature.
>
> ((why did you not ask the package authors ?? --- never mind))
>
>
> > This is because x is a 1-D array, so the operation is not
> > well-defined. Would it make sense for Matrix to support this (treat
> > 1-D arrays as column vectors, as it does for plain vectors)? Or should
> > I make my intent clear with
>
> > solve(Diagonal(5), as.vector(x))
>
> well ...
>
> The "fun" thing is that it actually works when Matrix methods
> are not fully available, i.e., if you do *not* do
> require(Matrix) or equivalent,
> but rather only load the Matrix namespace via
>
>   solve(Matrix::Diagonal(5), x)
>
> actually currently works correctly by some "good coincidence"
> (I have not yet tried to understand, as that's a bit painful:
>  selectMethod("solve", c("ddiMatrix", "array"))  is "lying" here ! )
>
> However this looks like a more general problem with S4 methods
> -- and probably a good reason for asking on R-help --  namely,
> the fact that d1-dimensional (numeric) arrays are not automatically treated as
> (numeric) vectors i.e. class "numeric"  wrt S4 methods.
>
> In the following case the  solve() - coincidence does not help, BTW.
>
>  Diagonal(3) %*% array(1:3)
>
>  ## Error in Diagonal(3) %*% array(1:3) :
>  ##   not-yet-implemented method for  %*% 
>
>
> In principle, we should consider a way to tell that "array"
> should be tried as "vector",

Actually, if you think compatible 1-d numeric arrays should 'work',
then all I was thinking of was something along the following lines:
Add an additional

setMethod("solve", c("ANY", "array"), function(a, b, ...) ...

which would basically do a dimension check for b, for 1-d numeric
arrays call solve(a, as.vector(b), ...), and error out for dim(b) > 2.
The actual details may be more involved, but that's the basic idea.

> possibly via something likesetIs("array", "vector")  or
> rather  setIs("array", "numeric")  because in the Matrix package
> the vectors encountered are really numeric vectors.
>
> .. OTOH, in all of the 3 packages I co-author and which use S4  heavily,  
> Matrix, Rmpfr, lme4,
> I had till now decided *not* to  setIs()  because it never
> worked as I intended, or rather had unpleasant side effects.
>
> Here,
>   setIs("array", "numeric", test=is.numeric)
>
> gives
>
> Error in setIs("array", "numeric", test = is.numeric) :
> cannot create a 'setIs' relation when neither of the classes (“array” and 
> “numeric”) is local and modifiable in this package
>
> A more successful alternative had been to use  setClassUnion(),
> so I could consider defining
>
>   setClassUnion("mVector", c("numeric", "array"))
>
> and replace "numeric" in many of the method signatures by  "mVector"
> (but that would then also dispatch for 1d character arrays
>  ... not so nicely).

But you already have that problem, I think:

> s = matrix(letters[1:10], 5, 2)
> solve(Diagonal(5), s)
Error in .M.kind(data) :
  not yet implemented for matrix with typeof character

whereas

m = matrix(1:10, 5, 2)

works nicely. Unfortunately, both m and s have the same class
(c("matrix", "array")), so I don't think method dispatch would be able
to distinguish between them with the current design, and you anyway
need to check in the solve method for c("diagonalMatrix", "matrix").

Best,
-Deepayan


> > -Deepayan
>
> > __
> > R-help@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@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] Matrix::solve() with 1-d arrays

2021-04-15 Thread Deepayan Sarkar
I get what I initially thought was unexpected behaviour from:

x <- tapply(runif(100), sample(5, 100, TRUE), mean)
solve(Diagonal(5), x)
# Error: not-yet-implemented method for solve(, ).
#  ->>  Ask the package authors to implement the missing feature.

This is because x is a 1-D array, so the operation is not
well-defined. Would it make sense for Matrix to support this (treat
1-D arrays as column vectors, as it does for plain vectors)? Or should
I make my intent clear with

solve(Diagonal(5), as.vector(x))

?

-Deepayan

__
R-help@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.


Re: [R] add cex.axis =1.2 to qqunif.plot from lattice library

2021-03-27 Thread Deepayan Sarkar
In addition to what Rolf said, a relatively obscure but generally
useful way to "globally" control fontsize is to set the underlying
grid parameter (see ?grid::gpar). The easiest way to do this is
through the lattice settings, e.g.,

lattice.options(default.theme = list(grid.pars = list(cex = 1.5)))

This is particularly useful for high resolution PNG plots for
inclusion in HTML output via knitr.

-Deepayan

On Sun, Mar 28, 2021 at 4:28 AM Rolf Turner  wrote:
>
>
> On Sat, 27 Mar 2021 21:26:53 +
> Yuan Chun Ding  wrote:
>
> > Dear R user,
> >
> > The following qqunit.plot function generated correct qq plot,
> > however, I want to make axis label (1 ,2 ,.8) have larger size
> > for publication.  I tried to add cex.axis =1.2 code following the pch
> > =20, but it does not change size of axis label. I guess lattice
> > library setting  is different from standard R graphics setting.
>
> Yes indeed things are different.  Read e.g. the help for xyplot() --- a bit
> opaque until you get used to it, but the information actually *is* there.
>
> Your example is *far* too complicated for anyone to wade through,
> but here is a toy example just using xyplot:
>
> set.seed(42)
> x <- 1:10
> y <- rnorm(10)
> library(lattice)
> # Compare:
> print(xyplot(y ~ x))
> # with:
> print(xyplot(y ~ x,scales=list(cex=2)))
>
> To get an overall title you can use "main=", just like in base graphics.
> E.g.:
>
> print(xyplot(y ~ x,scales=list(cex=2),
>  main="This is a load of dingoes' kidneys."))
>
> If you can't get something like this to work in the context
> in which you are interested, then please provide a *minimal* (simple!)
> reproducible example illustrating the problem, and someone on this list
> will probably be able to help you.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> R-help@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@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.


Re: [R] unanticipated axis labels

2021-03-16 Thread Deepayan Sarkar
On Tue, Mar 16, 2021 at 11:35 PM Richard M. Heiberger  wrote:
>
> library(lattice)
> library(latticeExtra)
>
> barchart(matrix(c(1:6, 5:6)), main="unanticipated left axis labels", 
> ylab="unanticipated inside labels") +
>   latticeExtra::layer(panel.axis("left", half=FALSE, labels=1:8))

So to summarize, your problem case happens when you explicitly specify
'labels' but not 'at' in panel.axis(), right?

Unfortunately, this is not intended to work at all, so what you are
seeing is undefined behaviour. This is hinted at, but not quite
explicitly spelled out, in the documentation. I will fix that, and
maybe add a warning as well.

-Deepayan

> barchart(matrix(c(1:6, 5:6)), main="ok 1", ylab="anticipated") +
>   latticeExtra::layer(panel.axis("left", half=FALSE, labels=1:8, at=1:8))
>
> barchart(matrix(c(1:6, 5:6)), main="ok 2", ylab="anticipated") +
>   latticeExtra::layer(panel.axis("left", half=FALSE, at=1:8))
>
> barchart(matrix(c(1:6, 5:6)), main="ok 3", ylab="anticipated") +
>   latticeExtra::layer(panel.axis("left", half=FALSE))
>
>
>
> __
> R-help@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@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.


Re: [R] Very slow optim()

2021-03-13 Thread Deepayan Sarkar
On Sat, Mar 13, 2021 at 4:33 PM Spencer Graves
 wrote:
>
> Hi, Deepayan:
>
>
> On 2021-03-13 01:27, Deepayan Sarkar wrote:
> > On Sat, Mar 13, 2021 at 10:08 AM Spencer Graves
> >  wrote:
> >>
> >> TWO COMMENTS:
> >>
> >>
> >> 1.  DID YOU ASSIGN THE OUTPUT OF "optim" to an object, like "est <-
> >> optim(...)"?  If yes and if "optim" terminated normally, the 60,000+
> >> paramters should be there as est$par.  See the documentation on "optim".
> >>
> >>
> >> 2.  WHAT PROBLEM ARE YOU TRYING TO SOLVE?
> >>
> >>
> >>I hope you will forgive me for being blunt (or perhaps 
> >> bigoted), but
> >> I'm skeptical about anyone wanting to use optim to estimate 60,000+
> >> parameters.  With a situation like that, I think you would be wise to
> >> recast the problem as one in which those 60,000+ parameters are sampled
> >> from some hyperdistribution characterized by a small number of
> >> hyperparameters.  Then write a model where your observations are sampled
> >> from distribution(s) controlled by these random parameters.  Then
> >> multiply the likelihood of the observations by the likelihood of the
> >> hyperdistribution and integrate out the 60,000+ parameters, leaving only
> >> a small number hyperparameters.
> >
> > Just a comment on this comment: I think it's perfectly reasonable to
> > optimize 60k+ parameters with conjugate gradient. CG was originally
> > developed to solve linear equations of the form Ax=b. If x was not
> > large in size, one would just use solve(A, b) instead of an iterative
> > method.
> >
> > Use of CG is quite common in image processing. A relatively small
> > 300x300 image will give you 90k parameters.
> >
> > -Deepayan
> >
>
>   Thanks for this.
>
>
>   If both A and b are 300x300, then x will also be 300x300.

Sorry for being unclear: the images themselves (b or x) are viewed as
a vector, so would be 9x1. A would be 9x9, so essentially
impossible to construct. CG can solve Ax=b as long as Ax can be
evaluated (for arbitrary x).

>   What do you do in this case if A is not square or even ill 
> conditioned?

A has to be p.d.

>   Do you care if you get only one of many possible or approximate
> solutions, and the algorithm spends most of its time making adjustments
> in a singular subspace that would have best been avoided?

Well, in my experience, optim(method="CG") behaves quite badly if A is
not full-rank. I don't think other implementations will be any better,
but I am not sure.

If you are interested in practical uses of this, we can discuss more off-list.

-Deepayan


>   Spencer
>
>
>

__
R-help@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.


Re: [R] Very slow optim()

2021-03-12 Thread Deepayan Sarkar
On Sat, Mar 13, 2021 at 10:08 AM Spencer Graves
 wrote:
>
> TWO COMMENTS:
>
>
> 1.  DID YOU ASSIGN THE OUTPUT OF "optim" to an object, like "est <-
> optim(...)"?  If yes and if "optim" terminated normally, the 60,000+
> paramters should be there as est$par.  See the documentation on "optim".
>
>
> 2.  WHAT PROBLEM ARE YOU TRYING TO SOLVE?
>
>
>   I hope you will forgive me for being blunt (or perhaps bigoted), but
> I'm skeptical about anyone wanting to use optim to estimate 60,000+
> parameters.  With a situation like that, I think you would be wise to
> recast the problem as one in which those 60,000+ parameters are sampled
> from some hyperdistribution characterized by a small number of
> hyperparameters.  Then write a model where your observations are sampled
> from distribution(s) controlled by these random parameters.  Then
> multiply the likelihood of the observations by the likelihood of the
> hyperdistribution and integrate out the 60,000+ parameters, leaving only
> a small number hyperparameters.

Just a comment on this comment: I think it's perfectly reasonable to
optimize 60k+ parameters with conjugate gradient. CG was originally
developed to solve linear equations of the form Ax=b. If x was not
large in size, one would just use solve(A, b) instead of an iterative
method.

Use of CG is quite common in image processing. A relatively small
300x300 image will give you 90k parameters.

-Deepayan

__
R-help@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.


Re: [R] xyplot.zoo trouble with the index when I use microseconds (type POSIXct)

2021-02-22 Thread Deepayan Sarkar
On Mon, Feb 22, 2021 at 9:56 PM Laurent Rhelp  wrote:
>
> Dear R-Help-List,
>
> I have to process time series with a sampling frequency of 1 MHz.
> I use the POSIXct format for the date-times with microsecond in a zoo
> object and the xyplot.zoo function to do the graphs.
> As I show in the below example I had a trouble to plot the labels on the
> x-axis with an error message. I found a solution but I would like to
> know if I miss something.
> The link
> https://stackoverflow.com/questions/7726034/how-r-formats-posixct-with-fractional-seconds
>
> helped me to understand how to print the POSIXct value to see the
> microseconds thanks to the function myformat.POSIXct:
>
> myformat.POSIXct <- function(x, digits=6)
> {
>x2 <- round(unclass(x), digits)
>attributes(x2) <- attributes(x)
>x <- as.POSIXlt(x2,origin="1970-01-01",tz="GMT")
>x$sec <- round(x$sec, digits)
>format.POSIXlt(x, paste("%Y-%m-%d %H:%M:%OS",digits,sep=""),tz="GMT")
>
> }
>
>
> ## The example giving the error message:
>
> library(lattice)
> library(zoo)
>
> ##
> options(digits = 16) # to see all the digits on the screen
> options(digits.secs = 6) # to see the microseconds
> # mock data
> # a sine with a frequency f0 and two others with a delay
> Fs <- 1e+6 # sampling frequency 1 MHz
> Ts <- 1/Fs
> # frequency of the sinus
> f0 <- 10
> t0 <- 1/f0
> time <- seq(0, length = 1000, by = Ts)
> A1 <- 1
> y1 <- A1 * sin(2*pi*f0*time)
> y2 <- 2 * A1 * sin(2*pi*f0*(time+0.02))
> y3 <- 3 * A1 * sin(2*pi*f0*(time+0.05))
> ## creation of a dataframe:
> ##
> DF <- data.frame( time = time, y1 = y1, y2 = y2, y3 = y3)
> # Since I want to allow for the datetime POSIXct format on the x-axis
> # for the plot I transform my dataframe in a zoo object
> #
> # say that my acquisition began at "2021-02-08 09:15:50.00"
> #
> mystart <- as.POSIXct("2021-02-08 09:15:50.00", format = "%Y-%m-%d
> %H:%M:%OS",tz="GMT")
> mystart
> # To see the correct datetime I use the myformat.POSIXct function
> myformat.POSIXct(mystart)
> ##
> ## using the method seq.POSIXct as following doesn't work:
> ## mydatetime <- seq( mystart , length = nrow(DF), by = "0.01 sec")
> ## head( myformat.POSIXct(mydatetime) )
> ## if I use the following command it works:
> mydatetime <- seq( mystart , length = nrow(DF), by = 0.01)
> head( myformat.POSIXct(mydatetime) )
> ## I do the zoo object:
> DF.z <- zoo(DF[,-1],order.by = mydatetime)
> ## We don't see the correct value for the index:
> head(DF.z)
> # timey1 y2 y3
> # 2021-02-08 09:15:50.00 0e+00 0.000e+00
> 1.902113032590307e+00  3.673819061467132e-16
> # 2021-02-08 09:15:50.00 1e-06 5.877852522924730e-01
> 1.902113032590307e+00 -1.763355756877419e+00
> # 2021-02-08 09:15:50.01 2e-06 9.510565162951535e-01
> 1.175570504584947e+00 -2.853169548885460e+00
> # 2021-02-08 09:15:50.03 3e-06 9.510565162951536e-01
> 1.133099690464601e-15 -2.853169548885460e+00
> # 2021-02-08 09:15:50.04 4e-06 5.877852522924736e-01
> -1.175570504584946e+00 -1.763355756877420e+00
> # 2021-02-08 09:15:50.05 5e-06 5.665498452323003e-16
> -1.902113032590306e+00 -3.399299071393802e-15
> # If I use myformat.POSIXct I see that the index is correct in the
> object DF:
> head(myformat.POSIXct(index(DF.z)))
> ## and when I plot I have an error:
> xyplot(   DF.z
>, screens = c(1,1,1)
>, type = "l"
>, col = c("red","blue","black")
>
> )
>
> # Error in prettyDate_TMP(x, ...) : range too small for min.n

At least the immediate problem is that

lattice:::prettyDate_TMP(range(mydatetime))
# Error in lattice:::prettyDate_TMP(range(mydatetime)) :
# range too small for min.n

grDevices:::prettyDate(range(mydatetime))
# [1] "2021-02-08 09:15:48 GMT" "2021-02-08 09:15:49 GMT"
# [3] "2021-02-08 09:15:50 GMT" "2021-02-08 09:15:51 GMT"
# [5] "2021-02-08 09:15:52 GMT" "2021-02-08 09:15:53 GMT"

So the version in lattice (which was supposed to be a temporary
stopgap till R 2.12 was released) has not kept up. I will fix.

Does the output above look OK to you?

Best,
-Deepayan


> # if I process by hand the plot of the labels on the x-axis it works:
> #
> myend <- tail(mydatetime,1)
>
> myticks <- seq( mystart , to = myend , length = 5)
> mylabels <- format(myticks,"%H:%M:%OS")
>
> xyplot(  DF.z
>   , screens = c(1,1,1)
>   , type = "l"
>   , col = c("red","blue","black")
>   , scales = list(
>   y= list(relation = "free", abbreviate=TRUE),
>   x = list( at = myticks, labels = mylabels
> , rot = 45, cex = 0.5)
> )
> )
> # The microseconds are well taken into account with the window function
> # if I want to plot only 100 microseconds but there is of course the same
> # trouble for the plot
>
> myend <- as.POSIXct("2021-02-08 09:15:50.000100", format = "%Y-%m-%d
> %H:%M:%OS",tz="GMT")
> myformat.POSIXct(myend)
>
> DF.w <- w

Re: [R] FInd packages in need of Update (like RStudio)

2020-12-23 Thread Deepayan Sarkar
On Wed, Dec 23, 2020 at 8:09 PM Dr Eberhard W Lisse  wrote:
>
> Hi,
>
> does anyone know how one would look which packages require an update
> (like the Tools -> Update Packages) but can do it from R (ie Rscript) or
> any other way from the command line (on the MAc)

? old.packages

Best,
-Deepayan

> I would like to build this into my update script which currently does
> brew, cpan and pip3 :-)-O
>
> greetings, el
> --
> Dr. Eberhard W. Lisse   \ /   Obstetrician & Gynaecologist
> e...@lisse.na / *  |  Telephone: +264 81 124 6733 (cell)
> PO Box 8421 Bachbrecht  \  /  If this email is signed with GPG/PGP
> 10007, Namibia   ;/ Sect 20 of Act No. 4 of 2019 may apply
>
> __
> R-help@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@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.


Re: [R] counting duplicate items that occur in multiple groups

2020-11-18 Thread Deepayan Sarkar
On Wed, Nov 18, 2020 at 5:40 AM Bert Gunter  wrote:
>
> z <- with(Data2, tapply(Vendor,Account, I))
> n <- vapply(z,length,1)
> data.frame (Vendor = unlist(z),
>Account = rep(names(z),n),
>NumVen = rep(n,n)
> )
>
> ## which gives:
>
>Vendor Account NumVen
> A1  V1  A1  1
> A21 V2  A2  3
> A22 V3  A2  3
> A23 V1  A2  3
> A3  V4  A3  1
> A4  V2  A4  1
>
> Of course this also works for Data1
>
> Bill may be able to come up with a slicker version, however.

Perhaps

transform(Data2, nshare = as.vector(table(Account)[Account]))

(or dplyr::mutate() instead of transform(), if you prefer.)

-Deepayan

>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Tue, Nov 17, 2020 at 3:34 PM Tom Woolman 
> wrote:
>
> > Yes, good catch. Thanks
> >
> >
> > Quoting Bert Gunter :
> >
> > > Why 0's in the data frame? Shouldn't that be 1 (vendor with that
> > account)?
> > >
> > > Bert
> > > Bert Gunter
> > >
> > > "The trouble with having an open mind is that people keep coming along
> > and
> > > sticking things into it."
> > > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> > >
> > >
> > > On Tue, Nov 17, 2020 at 3:29 PM Tom Woolman 
> > > wrote:
> > >
> > >> Hi Bill. Sorry to be so obtuse with the example data, I was trying
> > >> (too hard) not to share any actual values so I just created randomized
> > >> values for my example; of course I should have specified that the
> > >> random values would not provide the expected problem pattern. I should
> > >> have just used simple dummy codes as Bill Dunlap did.
> > >>
> > >> So per Bill's example data for Data1, the expected (hoped for) output
> > >> should be:
> > >>
> > >>   Vendor Account Num_Vendors_Sharing_Bank_Acct
> > >> 1 V1  A1  0
> > >> 2 V2  A2  3
> > >> 3 V3  A2  3
> > >> 4 V4  A2  3
> > >>
> > >>
> > >> Where the new calculated variable is Num_Vendors_Sharing_Bank_Acct.
> > >> The value is 3 for V2, V3 and V4 because they each share bank account
> > >> A2.
> > >>
> > >>
> > >> Likewise, in the Data2 frame, the same logic applies:
> > >>
> > >>   Vendor Account Num_Vendors_Sharing_Bank_Acct
> > >> 1 V1  A1 0
> > >> 2 V2  A2 3
> > >> 3 V3  A2 3
> > >> 4 V1  A2 3
> > >> 5 V4  A3 0
> > >> 6 V2  A4 0
> > >>
> > >>
> > >>
> > >>
> > >>
> > >>
> > >> Thanks!
> > >>
> > >>
> > >> Quoting Bill Dunlap :
> > >>
> > >> > What should the result be for
> > >> >   Data1 <- data.frame(Vendor=c("V1","V2","V3","V4"),
> > >> > Account=c("A1","A2","A2","A2"))
> > >> > ?
> > >> >
> > >> > Must each vendor have only one account?  If not, what should the
> > result
> > >> be
> > >> > for
> > >> >Data2 <- data.frame(Vendor=c("V1","V2","V3","V1","V4","V2"),
> > >> > Account=c("A1","A2","A2","A2","A3","A4"))
> > >> > ?
> > >> >
> > >> > -Bill
> > >> >
> > >> > On Tue, Nov 17, 2020 at 1:20 PM Tom Woolman  > >
> > >> > wrote:
> > >> >
> > >> >> Hi everyone.  I have a dataframe that is a collection of Vendor IDs
> > >> >> plus a bank account number for each vendor. I'm trying to find a way
> > >> >> to count the number of duplicate bank accounts that occur in more
> > than
> > >> >> one unique Vendor_ID, and then assign the count value for each row in
> > >> >> the dataframe in a new variable.
> > >> >>
> > >> >> I can do a count of bank accounts that occur within the same vendor
> > >> >> using dplyr and group_by and count, but I can't figure out a way to
> > >> >> count duplicates among multiple Vendor_IDs.
> > >> >>
> > >> >>
> > >> >> Dataframe example code:
> > >> >>
> > >> >>
> > >> >> #Create a sample data frame:
> > >> >>
> > >> >> set.seed(1)
> > >> >>
> > >> >> Data <- data.frame(Vendor_ID = sample(1:1), Bank_Account_ID =
> > >> >> sample(1:1))
> > >> >>
> > >> >>
> > >> >>
> > >> >>
> > >> >> Thanks in advance for any help.
> > >> >>
> > >> >> __
> > >> >> R-help@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@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.
> > >>
> >
> >
> >
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list -- To 

Re: [R] R for-loop to add layer to lattice plot

2020-10-27 Thread Deepayan Sarkar
On Tue, Oct 27, 2020 at 6:04 PM Luigi Marongiu  wrote:
>
> Hello,
> I am using e1071 to run support vector machine. I would like to plot
> the data with lattice and specifically show the hyperplanes created by
> the system.
> I can store the hyperplane as a contour in an object, and I can plot
> one object at a time. Since there will be thousands of elements to
> plot, I can't manually add them one by one to the plot, so I tried to
> loop into them, but only the last is added.
> Here it the working example for more clarity:
>
> ```
> library(e1071)
> library(lattice)
> library(latticeExtra)
>
> make.grid <- function(x, n = 1000) {
>   grange = apply(x, 2, range)
>   x1 = seq(from = grange[1,1], to = grange[2,1], length = n)
>   x2 = seq(from = grange[1,2], to = grange[2,2], length = n)
>   expand.grid(X1 = x1, X2 = x2)
> }
>
> plot_list <- list()
> for (i in 1:10) {
>   x1 = rnorm(100, mean = 0.2, sd = 0.15)
>   y1 = rnorm(100, mean = 0.7, sd = 0.15)
>   y2 = rnorm(100, mean = 0.2, sd = 0.15)
>   x2 = rnorm(100, mean = 0.75, sd = 0.15)
>   df = data.frame(x = c(x1,x2), y=c(y1,y2),
>   z=c(rep(0, length(x1)), rep(1, length(x2
>   df$z = factor(c(rep(0, length(x1)), rep(1, length(x2
>   df[, "train"] <- ifelse(runif(nrow(df)) < 0.8, 1, 0)
>   trainset <- df[df$train == 1, ]
>   testset <- df[df$train == 0, ]
>   trainColNum <- grep("train", names(df))
>   trainset <- trainset[, -trainColNum]
>   testset <- testset[, -trainColNum]
>   svm_model <- svm(z ~ .,
>   data = trainset,
>   type = "C-classification",
>   kernel = "linear",
>   scale = FALSE)
>   # generate contour
>   xmat = make.grid(matrix(c(testset$x, testset$y),
>   ncol = 2, byrow=FALSE))
>   xgrid = as.data.frame(xmat)
>   names(xgrid) = c("x", "y")
>   z = predict(svm_model, xgrid)
>   xyz_dat = as.data.frame(cbind(xgrid, z))
>   plot_list[[i]] = contourplot(z ~ y+x, data=xyz_dat, pretty = TRUE,
>xlim=c(-1,50), ylim=c(-0.001, 0.05),
>labels = FALSE, col = "blue", lwd = 0.5)
>
> }
> # the contour is stored in the object plot_list
> str(plot_list) # confirm that there is data here
>
> # I can add one element at the time to lattice's xyplot and store it
> in an object P
> P = xyplot(y ~ x, group = z, data = df,
>pch = 16, cex = 1.5, alpha = 0.25) + as.layer(plot_list[[1]]) +
>   as.layer(plot_list[[2]])
> plot(P)  # this demonstrates that the lines are not the same
>
> # but if I add the elements via loop, it does not work
> for (i in 1:length(plot_list)) {
>   print(i)
>   P = xyplot(y ~ x, group = z, data = df,
>  pch = 16, cex = 1.5, alpha = 0.25) + as.layer(plot_list[[i]])
> }
> plot(P)
> ```
>
> Am I missing something?

Yes, as Mark says, you need to change the last part to something like

P = xyplot(y ~ x, group = z, data = df, pch = 16, cex = 1.5, alpha = 0.25)
for (i in 1:length(plot_list)) {
  print(i)
  P = P + as.layer(plot_list[[i]])
}
plot(P)

-Deepayan

> Thank you
>
> __
> R-help@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@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.


Re: [R] plot factors with dots in R

2020-08-28 Thread Deepayan Sarkar
On Thu, Aug 27, 2020 at 5:46 PM Luigi Marongiu  wrote:
>
> Hello,
> I have a dataframe as follows:
> ```
> x = c("0 pmol", "10 pmol", "100 pmol", "1000 pmol")
> y = c(0.9306, 1.8906, 2.2396, 2.7917)
> df = data.frame(x, y)
>
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x: chr  "0 pmol" "10 pmol" "100 pmol" "1000 pmol"
>  $ y: num  0.931 1.891 2.24 2.792
> ```
> I would like to visualize the data with the classic dots (pch=16) but:

Perhaps this is a good starting point:

with(df, dotchart(y, labels = x, pch = 16))

-Deepayan

> ```
> > plot(df$y ~ df$x)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> which is right because x is not numeric, so I took the factor:
> ```
> plot(df$y ~ factor(df$x)) # gives bars instead of dots
> plot(df$y ~ factor(df$x), pch = 16) # this also
> ```
> I tried to convert directly the dataframe:
> ```
> df$x = lapply(df$x, factor)
> > str(df)
> 'data.frame': 4 obs. of  2 variables:
>  $ x:List of 4
>   ..$ : Factor w/ 1 level "0 pmol": 1
>   ..$ : Factor w/ 1 level "10 pmol": 1
>   ..$ : Factor w/ 1 level "100 pmol": 1
>   ..$ : Factor w/ 1 level "1000 pmol": 1
>  $ y: num  0.931 1.891 2.24 2.792
>
> > plot(r$y ~ r$x, pch = 16)
> Error in plot.window(...) : need finite 'xlim' values
> In addition: Warning messages:
> 1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
> 2: In min(x) : no non-missing arguments to min; returning Inf
> 3: In max(x) : no non-missing arguments to max; returning -Inf
> ```
> If I try to pass the number of levels:
> ```
> plot(df$y ~ factor(df$x, 1:4), pch = 16) # this draw a boxplot with
> all data on level 1
>
> > df$x = lapply(df$x, factor(1:4))
> Error in match.fun(FUN) :
>   'factor(1:4)' is not a function, character or symbol
> ```
>
> Since the transformation has given only one level (1), my questions are:
> How do I tell R to use a dot instead of a line?
> What is the correct way of setting factors?
>
>
> --
> Best regards,
> Luigi
>
> __
> R-help@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@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.


Re: [R] COVID-19 datasets...

2020-05-07 Thread Deepayan Sarkar
On Thu, May 7, 2020 at 4:16 PM Thomas Petzoldt  wrote:
>
> On 07.05.2020 at 11:19 Deepayan Sarkar wrote:
> > On Thu, May 7, 2020 at 12:58 AM Thomas Petzoldt  wrote:
> >>
> >> Sorry if I'm joining a little bit late.
> >>
> >> I've put some related links and scripts together a few weeks ago. Then I
> >> stopped with this, because there is so much.
> >>
> >> The data format employed by John Hopkins CSSE was sort of a big surprise
> >> to me.
> >
> > Why? I find it quite convenient to drop the first few columns and
> > extract the data as a matrix (using data.matrix()).
> >
> > -Deepayan
>
> Many thanks for the hint to use data.matrix
>
> My aim was not to say that it is difficult, especially as R has all the
> tools for data mangling.
>
> My surprise was that "wide tables" and non-ISO dates as column names are
> not the "data base way" that we in general teach to our students

Well, I am all for long format data when it makes sense, but I would
disagree that that is always the "right approach". In the case of
regular multiple time series, as in this context, a matrix-like
structure seems much more natural (and nicely handled by ts() in R),
and I wouldn't even bother reshaping the data in the first place.

See, for example,

https://github.com/deepayan/deepayan.github.io/blob/master/covid-19/deaths.rmd

and

https://deepayan.github.io/covid-19/deaths.html

-Deepayan

> With reshape2::melt or tidyr::gather resp. pivot_longer, conversion is
> quite easy, regardless if one wants to use tidyverse or not, see example
> below.
>
> Again, thanks, Thomas
>
>
> library("dplyr")
> library("readr")
> library("tidyr")
>
> file <-
> "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
>
> dat <- read_delim(file, delim=",")
> names(dat)[1:2] <- c("Province_State", "Country_Region")
> dat2 <-
>dat %>%
>## summarize Country/Region duplicates
>group_by(Country_Region) %>% summarise_at(vars(-(1:4)), sum) %>%
>## make it a long table
>pivot_longer(cols = -Country_Region, names_to = "time") %>%
>## convert to ISO 8601 date
>mutate(time = as.POSIXct(time, format="%m/%e/%y"))
>
>
>
> >
> >> An opposite approach was taken in Germany, that organized it as a
> >> big JSON trees.
> >>
> >> Fortunately, both can be "tidied" with R, and represent good didactic
> >> examples for our students.
> >>
> >> Here yet another repo linking to the data:
> >>
> >> https://github.com/tpetzoldt/covid
> >>
> >>
> >> Thomas
> >>
> >>
> >> On 04.05.2020 at 20:48 James Spottiswoode wrote:
> >>> Sure. COVID-19 Data Repository by the Center for Systems Science and 
> >>> Engineering (CSSE) at Johns Hopkins University is available here:
> >>>
> >>> https://github.com/CSSEGISandData/COVID-19
> >>>
> >>> All in csv fiormat.
> >>>
> >>>
> >>>> On May 4, 2020, at 11:31 AM, Bernard McGarvey 
> >>>>  wrote:
> >>>>
> >>>> Just curious does anyone know of a website that has data available in a 
> >>>> format that R can download and analyze?
> >>>>
> >>>> Thanks
> >>>>
> >>>>
> >>>> Bernard McGarvey
> >>>>
> >>>>
> >>>> Director, Fort Myers Beach Lions Foundation, Inc.
> >>>>
> >>>>
> >>>> Retired (Lilly Engineering Fellow).
> >>>>
> >>>> __
> >>>> R-help@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.
> >>>>
> >>>
> >>> James Spottiswoode
> >>> Applied Mathematics & Statistics
> >>> (310) 270 6220
> >>> jamesspottiswoode Skype
> >>> ja...@jsasoc.com
> >>>
>
> --
> Dr. Thomas Petzoldt
> senior scientist
>
> Technische Universitaet Dresden
> Faculty of Environmental Sciences
> Institute of Hydrobiology
> 01062 Dresden, Germany
>
> https://tu-dresden.de/Members/thomas.petzoldt

__
R-help@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.


Re: [R] COVID-19 datasets...

2020-05-07 Thread Deepayan Sarkar
On Thu, May 7, 2020 at 12:58 AM Thomas Petzoldt  wrote:
>
> Sorry if I'm joining a little bit late.
>
> I've put some related links and scripts together a few weeks ago. Then I
> stopped with this, because there is so much.
>
> The data format employed by John Hopkins CSSE was sort of a big surprise
> to me.

Why? I find it quite convenient to drop the first few columns and
extract the data as a matrix (using data.matrix()).

-Deepayan

> An opposite approach was taken in Germany, that organized it as a
> big JSON trees.
>
> Fortunately, both can be "tidied" with R, and represent good didactic
> examples for our students.
>
> Here yet another repo linking to the data:
>
> https://github.com/tpetzoldt/covid
>
>
> Thomas
>
>
> On 04.05.2020 at 20:48 James Spottiswoode wrote:
> > Sure. COVID-19 Data Repository by the Center for Systems Science and 
> > Engineering (CSSE) at Johns Hopkins University is available here:
> >
> > https://github.com/CSSEGISandData/COVID-19
> >
> > All in csv fiormat.
> >
> >
> >> On May 4, 2020, at 11:31 AM, Bernard McGarvey 
> >>  wrote:
> >>
> >> Just curious does anyone know of a website that has data available in a 
> >> format that R can download and analyze?
> >>
> >> Thanks
> >>
> >>
> >> Bernard McGarvey
> >>
> >>
> >> Director, Fort Myers Beach Lions Foundation, Inc.
> >>
> >>
> >> Retired (Lilly Engineering Fellow).
> >>
> >> __
> >> R-help@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.
> >>
> >
> > James Spottiswoode
> > Applied Mathematics & Statistics
> > (310) 270 6220
> > jamesspottiswoode Skype
> > ja...@jsasoc.com
> >
> >
>
> __
> R-help@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@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.


Re: [R] [EXTERNAL] Re: overlaying graphs in xYplot (Hmisc)

2020-04-25 Thread Deepayan Sarkar
On Wed, Apr 22, 2020 at 11:44 PM Cade, Brian S via R-help
 wrote:
>
> All the xYplot() functions using Cbind() or cbind() does just exactly what I 
> want (Cbind
> provides aplot of 3 summary statistics and cbind provides the raw values).  I 
> just cannot
> find anyway to overlay them.

You are not really helping yourself by not providing reproducible code.

If you are lucky, the following might give you what you want:

library(latticeExtra)
p1 <- xYplot(...) # your first plot
p2 <- xYplot(...) # your second plot
p1 + p2

Or it might not.

-Deepayan

> Brian
>
>
> Brian S. Cade, PhD
>
> U. S. Geological Survey
> Fort Collins Science Center
> 2150 Centre Ave., Bldg. C
> Fort Collins, CO  80526-8818
>
> email:  ca...@usgs.gov
> tel:  970 226-9326
>
> 
> From: David Winsemius 
> Sent: Wednesday, April 22, 2020 11:10 AM
> To: Cade, Brian S ; r-help 
> Subject: [EXTERNAL] Re: [R] overlaying graphs in xYplot (Hmisc)
>
>
> On 4/22/20 7:31 AM, Cade, Brian S via R-help wrote:
> > Hi All.  I am trying to construct a graph using the xYplot() function in 
> > Hmisc package (thank you Frank Harrell) taking advantage of the Cbind() 
> > argument for plotting the median, 10th, and 90th quantiles and also the 
> > cbind() argument for individual data values.  I know how to do both of 
> > these separately, but I would really like to have them overlayed on each 
> > other.  I've tried various approaches with add=T, new=T, etc and none of 
> > those seem to work with xYplot().  Any pointers?
>
>
> I don't know the answer and you have presented no data or code, so I'm
> just going to address the question in the most halting and vague manner
> by first looking at the code and then looking at the documentaion.
> (That's possibly the reverse of the proper order.) The plotting
> functions in pkg:Hmisc can be in any of the three plotting paradigms. If
> you look at the code for xYplot it becomes almost immediately obvious
> that it is operative within the lattice plotting paradigm. (That means
> that using `new=T` or `add=T` would not work since those are base
> plotting strategies.)
>
>
> I'm not sure what the term "Cbind argument" might mean to you (and I've
> never used it), but I suspect you are intending to use a call to `Cbind`
> on the left hand side of a formula argument for xYplot.
>
> Re: The goal of "plotting the median, 10th, and 90th quantiles and also
> the cbind() argument for individual data values."
>
> It appears to me from the documentation that you would be expected to do some 
> pre-processing to aggregate your data into a summary format by groups before 
> plotting and then use named arguments to Cbind to designate the appropriate 
> columns to be used for median and the outer quantiles. See the example using 
> the dataset named dfr in the ?xYplot page following these comments:
>
> # The following example uses the summarize function in Hmisc to
> # compute the median and outer quartiles.  The outer quartiles are
> # displayed using "error bars"
>
> It should be trivial to modify the call to `summarize` to get .1 and .9 
> quantiles instead of quartiles.
>
> --
> David.
>
>
>
> >
> > Brian
> >
> >
> > Brian S. Cade, PhD
> >
> > U. S. Geological Survey
> > Fort Collins Science Center
> > 2150 Centre Ave., Bldg. C
> > Fort Collins, CO  80526-8818
> >
> > email:  ca...@usgs.gov
> > tel:  970 226-9326
> >
> >
> >[[alternative HTML version deleted]]
> >
> > __
> > R-help@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.
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Options for zooming plots other than zm()

2020-04-25 Thread Deepayan Sarkar
On Sat 25 Apr, 2020, 12:10 PM Robert Dodier, 
wrote:

> Hi,
>
> I am making some plots with plot() which have a fair number of points
> (thousands) and I would like to be able to interactively select a
> region of the plot and zoom in on it. I tried the zoom package which
> has the function zm() but I found that it was unworkably slow to
> refresh the display. I guess I can set the x and y range via xlim and
> ylim but I was hoping to do it interactively. Does someone have a
> suggestion for that?
>
> I looked at ggplot2 but I wasn't able to find something about
> interactive zooming, only noninteractive via plot limits. Perhaps I
> have overlooked something there?
>


How about plotly? It supports ggplot2 plots through ggplotly().

-Deepayan


> I have searched the mailing list archive and web pages in general but
> I haven't found anything other than zm(). Thank you in advance for
> your help, I appreciate it very much.
>
> best,
>
> Robert Dodier
>
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] ggplot stat smooth and poly

2020-04-05 Thread Deepayan Sarkar
On Thu, Apr 2, 2020 at 6:10 PM PIKAL Petr  wrote:

> Dear all
>
> I am not sure, but I believe that in past it was possible to add smoothing
> lines in ggplot even if some group did not have enough points to perform
> calculation (although I did not find any version which could deliver it).
>
> Here is the code and data
>
> library(ggplot2)
> p <- ggplot(test, aes(x=one, y=two, colour=three))
> p+geom_point(size=5)+stat_smooth(method="lm")
> ***line added to each group
>
> p+geom_point(size=5)+stat_smooth(method="lm", formula=y~poly(x,2))
> Warning message:
> Computation failed in `stat_smooth()`:
> 'degree' must be less than number of unique points
> ***no line added to any group
>
> test <- structure(list(one = 1:20, two = c(1L, 4L, 9L, 16L, 25L, 36L,
> 49L, 64L, 81L, 100L, 121L, 144L, 169L, 196L, 225L, 256L, 289L,
> 324L, 361L, 400L), three = c("a", "a", "a", "a", "b", "b", "b",
> "b", "c", "c", "c", "c", "c", "d", "d", "e", "e", "e", "e", "e"
> )), class = "data.frame", row.names = c(NA, -20L))
>
> My question:
> Is it possible to add smoothing line just to the groups where it can be
> added? I know that I could exclude "d" level from my data but I would
> prefer
> to keep them and add only smoothing lines where they could be computed.
>

Looks like there's a tryCatch around each panel, but not for each group
within panel. So this would work:

p + geom_point(size=2) + facet_wrap(~three) +
stat_smooth(method="lm", formula=y~poly(x,2))

but one problematic group is enough to make a whole panel fail.

Other than rewriting StatSmooth$compute_panel to protect each per-group
call, a workaround could be to replace method="lm" by a safe wrapper, e.g.,:

plm <- function(formula, data, ...)
{
ocall <- match.call(expand.dots = TRUE)
ocall[[1]] <- quote(lm)
fm <- try(eval(ocall, parent.frame()), silent = TRUE)
if (inherits(fm, "try-error"))
{
ocall[[2]] <- y ~ x
fm <- eval(ocall, parent.frame())
}
fm
}

p + geom_point(size=2) + stat_smooth(method=plm, formula=y~poly(x,2))

-Deepayan

Best regards
> Petr
> __
> R-help@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.
>

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] a simple reshape

2020-04-04 Thread Deepayan Sarkar
Hi,

[For a non-tidyverse solution:]

Your problem is ambiguous without a 'time' variable; e.g., why should
the answer not be

test2  <- data.frame(vntr1=c("v1","v2"),
 a1 =c(NA, 0.5693),
 a2 = c(0.02, 0.12),
 a3 =c(NA, 0.11),
 a4=c(0.98, 0.04))

? If you do add an artificial time variable, say using

test1 <- transform(test1,
time = unsplit(lapply(split(vntr1, vntr1), seq_along), vntr1))

to give

> test1
 vntr1  val time
1v1 0.981
2v1 0.022
3v2 0.591
4v2 0.122
5v2 0.113
6v2 0.044

then either reshape() or dcast() easily gives you what you want:

> reshape(test1, v.names = "val", idvar = "vntr1", direction = "wide", timevar 
> = "time")
 vntr1 val.1 val.2 val.3 val.4
1v1  0.98  0.02NANA
3v2  0.59  0.12  0.11  0.04

> reshape2::dcast(test1, vntr1 ~ time, value.var="val")
 vntr11234
1v1 0.98 0.02   NA   NA
2v2 0.59 0.12 0.11 0.04

-Deepayan

On Sat, Apr 4, 2020 at 12:28 AM Yuan Chun Ding  wrote:
>
> Hi R users,
>
> I want to do a data reshape from long to wide, I thought it was easy using 
> tidyverse spread function, but it did not work well. Can you help me?
>
> Thank you,
>
> Ding
>
> test1 data frame is long file and test2 is the wide file I want to get
>
> test1 <- data.frame (vntr1=c("v1","v1", "v2","v2","v2","v2"),
>  val =c(0.98,0.02, 0.59,0.12,0.11,0.04))
>
> test2  <- data.frame(vntr1=c("v1","v2"),
>  a1 =c(0.98, 0.5693),
>  a2 = c(0.02, 0.12),
>  a3 =c(NA, 0.11),
>  a4=c(NA, 0.04))
>
> the following code does not work
> test2 <-test1 %>%spread(vntr1, val)
>
>  Error: Each row of output must be identified by a unique combination of keys.
> Keys are shared for 6 rows:
> * 1, 2
> * 3, 4, 5, 6
> Do you need to create unique ID with tibble::rowid_to_column()?
> Call `rlang::last_error()` to see a backtrace
>
> --
> 
> -SECURITY/CONFIDENTIALITY WARNING-
>
> This message and any attachments are intended solely for the individual or 
> entity to which they are addressed. This communication may contain 
> information that is privileged, confidential, or exempt from disclosure under 
> applicable law (e.g., personal health information, research data, financial 
> information). Because this e-mail has been sent without encryption, 
> individuals other than the intended recipient may be able to view the 
> information, forward it to others or tamper with the information without the 
> knowledge or consent of the sender. If you are not the intended recipient, or 
> the employee or person responsible for delivering the message to the intended 
> recipient, any dissemination, distribution or copying of the communication is 
> strictly prohibited. If you received the communication in error, please 
> notify the sender immediately by replying to this message and deleting the 
> message and any accompanying files from your system. If, due to the security 
> risks, you do not wish to r
 eceive further communications via e-mail, please reply to this message and 
inform the sender that you do not wish to receive further e-mail from the 
sender. (LCP301)
>
> __
> R-help@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@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.


Re: [R] [FORGED] Grouping Question

2020-03-22 Thread Deepayan Sarkar
Another possible approach is to use split -> lapply -> rbind, which I
often find to be conceptually simpler:

d <- data.frame(Serial = c(1, 1, 2, 2, 2, 3, 3),
Measurement = c(17, 16, 12, 8, 10, 19, 13))

dlist <- split(d, d$Serial)
dlist <- lapply(dlist, within,
{
Serial_test <- if (all(Measurement <= 16)) "pass" else "fail"
Meas_test <- ifelse(Measurement <= 16, "pass", "fail")
})
do.call(rbind, dlist)

-Deepayan

On Sun, Mar 22, 2020 at 12:29 PM Rolf Turner  wrote:
>
>
> On 22/03/20 4:01 pm, Thomas Subia via R-help wrote:
>
> > Colleagues,
> >
> > Here is my dataset.
> >
> > SerialMeasurement Meas_test   Serial_test
> > 1 17  failfail
> > 1 16  passfail
> > 2 12  passpass
> > 2 8   passpass
> > 2 10  passpass
> > 3 19  failfail
> > 3 13  passpass
> >
> > If a measurement is less than or equal to 16, then Meas_test is pass. Else
> > Meas_test is fail
> > This is easy to code.
> >
> > Serial_test is a pass, when all of the Meas_test are pass for a given
> > serial. Else Serial_test is a fail.
> > I'm at a loss to figure out how to do this in R.
> >
> > Some guidance would be appreciated.
>
> In future, please present your data using dput(); makes life much easier
> for those trying to help you.  Your data are really the first two
> columns of what you presented --- the last two columns are your desired
> output.
>
> Let "X" be these first two columns.  Define
>
> foo <- function (X) {
> a <- with(X,Measurement <= 16)
> a <- ifelse(a,"pass","fail")
> b <- with(X,tapply(Measurement,Serial,function(x){all(x<=16)}))
> i <- match(X$Serial,names(b))
> b <- ifelse(b[i],"pass","fail")
> data.frame(Meas_test=a,Serial_test=b)
> }
>
> foo(X) gives:
>
> >   Meas_test Serial_test
> > 1  failfail
> > 2  passfail
> > 3  passpass
> > 4  passpass
> > 5  passpass
> > 6  failfail
> > 7  passfail
>
> If you want input and output combined, as in the way that you presented
> your data use cbind(X,foo(X)).
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> __
> R-help@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@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.


Re: [R] Unintended behaviour (possibly bugs)

2020-02-17 Thread Deepayan Sarkar
On Mon, Feb 17, 2020 at 10:24 AM Rui Barradas  wrote:
>
> Hello,
>
> Yes, this is definitely a bug.

I would argue that the only bug here is that the documentation doesn't
say that 'ylab' may not behave as expected.

dotchart() is mainly designed for 2-way tables (see the VADeaths
example), but it's implementation is really pretty hackish because it
has to work within the limited traditional graphics framework. The
main problem is that dot plots want to put horizontal y-axis labels
(usually derived from factor levels), which are often longer than the
default margins, so the margins are modified. Unfortunately they are
only re-set on exit, and so the ylab that is plotted inside dotchart()
may be clipped. Traditionally, Cleveland dot plots don't have a y-axis
label; it's assumed that the factor levels are sufficient (and for
2-way tables, there would be two variables, so there is no sensible
default).

I doubt that dotchart() is worth fixing (except to maybe disallow
ylab). If you want flexibility, use modern grid-based alternatives
such as lattice::dotplot() or ggplot2.

-Deepayan

> Even the matrix plot is puzzling, with a "1" as top row sort-of-label
> but no grid line. I'm trying to follow the source code of dotchart but
> am yet to understand exactly what it does to decide the margins settings.
>
>  if (!(is.null(labels) && is.null(glabels))) {
>nmai <- par("mai")
>nmai[2L] <- nmai[4L] + max(linch + goffset, ginch) +
>  0.1
>par(mai = nmai)
>  }
>
> This should be moved to r-devel?
>
> Rui Barradas
>
> Às 03:33 de 17/02/20, Alexey Shipunov escreveu:
> > John and Rui, thanks!
> >
> > However, if we use the proper object, the problem still persists:
> >
> > dotchart(c("3"=1, "2"=2, "1"=3), ylab="Ylab") # ylab is invisible
> > dotchart(c("aa"=1, "b"=2, "cc"=3), ylab="Ylab") # ylab is partly visible 
> > (!!!)
> > dotchart(c("aaa"=1, "bbb"=2, "ccc"=3), ylab="Ylab") # ylab is well visible
> >
> > If the object is matrix, ylab is visible:
> >
> > dotchart(matrix(1:3, dimnames=list(c("aa","bb","cc"), NULL)), ylab="Ylab")
> >
> > But the ?dotchart explicitly says that "x: either a vector or matrix
> > of numeric values" and then "labels: a vector of labels for each
> > point.  For vectors the default is to use ‘names(x)’ ...".
> >
> > So this is likely a bug. Do you agree?
> >
> > Alexey
> >
> > пн, 17 февр. 2020 г. в 01:55, Rui Barradas :
> >>
> >> Hello,
> >>
> >> I believe you are wrong, the error is not in dotchart, it's in your
> >> code. You assume that to plot an object of class "table" is the same as
> >> to plot an object of class "numeric".
> >>
> >> Inline.
> >>
> >> Às 12:21 de 16/02/20, Alexey Shipunov escreveu:
> >>> Dear list,
> >>>
> >>> I have been advised to share these with R-help instead of filling the
> >>> bug report:
> >>>
> >>> 1) dotchart() does not allow to see the left axis title ('ylab') and
> >>> cannot change the left margin (outer margin 2) of the plot
> >>>
> >>> The code:
> >>>
> >>> aa <- table(c(1, 1, 1, 2, 2, 3))
> >>> dotchart(aa, ylab="Ylab") # does not show 'ylab'
> >>
> >> You are right, it does *not* show 'ylab' but the user is warned.
> >>
> >>
> >> aa <- table(c(1, 1, 1, 2, 2, 3))
> >> dotchart(aa, ylab = "Ylab") # does show 'ylab'
> >> #Warning message:
> >> #In dotchart(aa, ylab = "Ylab") :
> >> #  'x' is neither a vector nor a matrix: using as.numeric(x)
> >>
> >>
> >> My code:
> >>
> >>
> >> (mar <- par("mar"))# new R session
> >> #[1] 5.1 4.1 4.1 2.1   # the left margin is 4.1
> >>
> >> aa <- as.numeric(table(c(1, 1, 1, 2, 2, 3)))
> >>
> >> dotchart(aa, ylab = "Ylab") # It does show 'ylab'
> >> old.par <- par(mar = mar + c(0, 5, 0, 0))
> >> par("mar")
> >> #[1] 5.1 9.1 4.1 2.1
> >>
> >> dotchart(aa, ylab = "Ylab")  # The left margin is now 9.1, much bigger
> >>
> >> par(old.par) # It does change the left margin
> >> dotchart(aa, ylab = "Ylab")  #  but only when a new graph is plotted.
> >>
> >>
> >>
> >>> old.par <- par(mar=c(1, 10, 1, 1)) ; dotchart(aa, ylab="Ylab") ;
> >>> par(old.par) # does not change left margin
> >>>
> >>> Possible solution:
> >>>
> >>> I researched the problem and think that the dotchart() code will need
> >>> few corrections. If there is an interest, I can post it here; or you
> >>> can look at the code of shipunov::Dotchart1() function.
> >>>
> >>> 2) example(hist) includes two "wrong" and "extreme" examples which
> >>> slow down and even crash R on some systems; this make it unsuitable
> >>> for demonstration in the class and strikes beginners in R who just
> >>> want to understand how hist() works. Actually, I did it last week (I
> >>> was not aware of these examples), and in the class two computers hang,
> >>> and many others were extremely slow.
> >>>
> >>> The code:
> >>>
> >>> example(hist)
> >>>
> >>> Possible solution:
> >>>
> >>> If R maintainers will enclose parts of "hist" example in \dontrun{},
> >>> this will allow to see the code but in the same time will not strike

Re: [R] Increasing space for main title in a lattice (xyplot()) graphics.

2020-02-12 Thread Deepayan Sarkar
Hi Bert,

You are right that the general solution is for 'main' to be a (grid)
grob. It is not clear (to me) what the "height" of a textGrob with
multiple labels should be, but the following gives reasonable results:

xyplot(1 ~ 1,
   main = textGrob(c("The quick brown fox jumped", "over the lazy dog"),
   x = unit(0.5, "npc"), y = unit(c(0.75, 0.25), "cm")))

I'm guessing your first attempt was with the default units ("npc") for y.

The correct grob (allowing more detailed control) to use for complex
grid objects is a frameGrob.

lattice does have an interface to create (simple) frameGrobs, for
constructing legends. This can be (ab)used as follows:

xyplot(1 ~ 1,
   main = draw.key(key = list(text = list(c("The quick brown fox jumped",
"over the lazy dog"),
  font = c(1, 2), col = c(2, 3)

-Deepayan

On Thu, Feb 13, 2020 at 11:36 AM Bert Gunter  wrote:
>
> OK. Now for a tougher problem: how to make the first line bold font and the 
> second line normal font (and/or different colors)?
>
> My reading of the docs did not reveal how to do it, but I found a way using a 
> textGrob for the title (i.e. main ). But it's tricky, as the "obvious 
> solution" of using different y values for the lines caused lattice to enlarge 
> the title viewport too much, shrinking the graph panels so that details were 
> lost. I think I have found a way to avoid this and make it work, but I'll 
> delay giving my somewhat clumsy "solution" until some of you have a chance to 
> find a more sensible approach, if you care to try.
>
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and 
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Wed, Feb 12, 2020 at 9:19 PM Deepayan Sarkar  
> wrote:
>>
>> On Thu, Feb 13, 2020 at 10:39 AM Richard M. Heiberger  
>> wrote:
>> >
>> > It works as anticipated for me
>> >
>> > > xyplot(1 ~ 1,
>> > + main="The quick brown fox jumped\n over the lazy dog.")
>> > > xyplot(1 ~ 1,
>> > + main="The quick brown fox jumped over the lazy dog.")
>> >
>> > Something else you are doing is probably causing the difficulty.
>>
>> Yes, the necessary space should be automatically allocated. Details of
>> version / device might help diagnosing the problem.
>>
>> -Deepayan
>>
>> >
>> > Rich
>> >
>> > On Wed, Feb 12, 2020 at 11:59 PM Rolf Turner  
>> > wrote:
>> > >
>> > >
>> > > I'm trying to do an xyplot() with a longish main title that I'd like to
>> > > split into two lines, something like
>> > >
>> > >  xyplot(,
>> > > main="The quick brown fox jumped\n over the lazy dog.")
>> > >
>> > > When I do this I only get the last half, i.e. the "over the lazy dog."
>> > > bit, and the first half doesn't appear.
>> > >
>> > > In base graphics I'd handle this sort of thing by increasing the third
>> > > entry of the "mar" parameter.
>> > >
>> > > How can increase the space allocated for the title in lattice graphics?
>> > > I've done a substantial amount of Googling and can't find anything
>> > > helpful.  I've fiddled about with trellis.par.set() and cannot seem to
>> > > get any effect.
>> > >
>> > > Could someone please give my poor feeble brain some guidance?  Ta.
>> > >
>> > > cheers,
>> > >
>> > > Rolf Turner
>> > >
>> > > --
>> > > Honorary Research Fellow
>> > > Department of Statistics
>> > > University of Auckland
>> > > Phone: +64-9-373-7599 ext. 88276
>> > >
>> > > __
>> > > R-help@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@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@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@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.


Re: [R] Increasing space for main title in a lattice (xyplot()) graphics.

2020-02-12 Thread Deepayan Sarkar
On Thu, Feb 13, 2020 at 10:39 AM Richard M. Heiberger  wrote:
>
> It works as anticipated for me
>
> > xyplot(1 ~ 1,
> + main="The quick brown fox jumped\n over the lazy dog.")
> > xyplot(1 ~ 1,
> + main="The quick brown fox jumped over the lazy dog.")
>
> Something else you are doing is probably causing the difficulty.

Yes, the necessary space should be automatically allocated. Details of
version / device might help diagnosing the problem.

-Deepayan

>
> Rich
>
> On Wed, Feb 12, 2020 at 11:59 PM Rolf Turner  wrote:
> >
> >
> > I'm trying to do an xyplot() with a longish main title that I'd like to
> > split into two lines, something like
> >
> >  xyplot(,
> > main="The quick brown fox jumped\n over the lazy dog.")
> >
> > When I do this I only get the last half, i.e. the "over the lazy dog."
> > bit, and the first half doesn't appear.
> >
> > In base graphics I'd handle this sort of thing by increasing the third
> > entry of the "mar" parameter.
> >
> > How can increase the space allocated for the title in lattice graphics?
> > I've done a substantial amount of Googling and can't find anything
> > helpful.  I've fiddled about with trellis.par.set() and cannot seem to
> > get any effect.
> >
> > Could someone please give my poor feeble brain some guidance?  Ta.
> >
> > cheers,
> >
> > Rolf Turner
> >
> > --
> > Honorary Research Fellow
> > Department of Statistics
> > University of Auckland
> > Phone: +64-9-373-7599 ext. 88276
> >
> > __
> > R-help@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@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@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.


Re: [R] Alignment of the title in a key for xyplot in lattice.

2019-12-24 Thread Deepayan Sarkar
On Tue, Dec 24, 2019 at 6:59 AM Jim Lemon  wrote:
>
> Hi Rolf,
> Following the docs back to draw.key, It looks like the ellipsis
> argument is ignored. I was hoping for a brilliant solution along the
> lines of:
>
> adj=0
>
> that could be passed down the functions like a hot potato, but was 
> disappointed.

Yes, the implementation of title is quite rudimentary, and should be
easy to enhance. The current invocation for drawing the title is
essentially

textGrob(label = key$title,
 gp = gpar(cex = key$cex.title,
   lineheight = key$lineheight))

which translates to (with defaults)

textGrob(label = key$title,
 x = 0.5, y = 0.5, default.units = "npc", just = "centre",
 gp = gpar(cex = key$cex.title,
   lineheight = key$lineheight))

To control the justification, the user needs to be able to specify at
least 'x' and 'just'. One should also be able to control other
graphical parameters.

A trickier issue is that the legend doesn't consider the title when
computing its width. I have never been able to decide whether it
should.

Anyway, I have some long-pending pull requests for improving legend
behaviour, which hopefully I will be able to get to soon. I will try
to address this at the same time.

-Deepayan


> Jim
>
> On Tue, Dec 24, 2019 at 9:26 AM Rolf Turner  wrote:
> >
> >
> > The title of a key seems to be horizontally centred in the key; I would
> > like to have it aligned with the left hand edge.  I.e. I would like the
> > first letter of the title to have the same horizontal position as the
> > first letters of the text strings.
> >
> > E.g. in the attached example I would like the "P" in "Point type" to be
> > directly above the "o" in "obsd" and "f" in "fitted".
> >
> > Is there any way to effect this?  Thanks.
> >
> > cheers,
> >
> > Rolf Turner
> >
> > --
> > Honorary Research Fellow
> > Department of Statistics
> > University of Auckland
> > Phone: +64-9-373-7599 ext. 88276
> > __
> > R-help@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@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.


Re: [R] ggpubr: order of non-numeric x-axis items

2019-10-26 Thread Deepayan Sarkar
On Sat, Oct 26, 2019 at 8:22 PM Bert Gunter  wrote:
>
> No. relevel() only changes the order in one specific way. Use `levels<-`()
> to reorder in a general way:
>
> > z <- factor(rep(letters[3:1],2))
> > z
> [1] c b a c b a
> Levels: a b c
> > z <-relevel(z, ref = "c")
> > z
> [1] c b a c b a
> Levels: c a b
> > levels(z)<- c("c","b","a")
> > z
> [1] c a b c a b
> Levels: c b a

No, that changes the data, not just the order of the levels; "b" and
"a" have been switched.

You need some version of

> factor(z, levels = c("c", "a", "b"))
[1] c b a c b a

See also ?reorder for a useful way of reordering the levels,
especially in plots.

-Deepayan

> Cheers,
> Bert
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and
> sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Sat, Oct 26, 2019 at 6:09 AM Patrick (Malone Quantitative) <
> mal...@malonequantitative.com> wrote:
>
> > Try using relevel() to organize the categories in your factor in the
> > desired order. You may need to use relevel(as.factor()) .
> >
> > On Sat, Oct 26, 2019 at 6:51 AM April Ettington
> >  wrote:
> > >
> > > Hi,
> > >
> > > When I use ggpubr with an x-axis utilizing descriptive categories (eg.
> > bar
> > > chart for different colors of car), it sorts all of the labels
> > > alphabetically.  Is there a way to change this so it shows in the order I
> > > want?
> > >
> > > Thanks,
> > >
> > > April
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > __
> > > R-help@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@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.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Stratifying data with xyplot

2019-03-15 Thread Deepayan Sarkar
On Tue, Mar 12, 2019 at 2:28 AM Kevin Wright  wrote:
>
> See the examples here:
> https://www.stat.ubc.ca/~jenny/STAT545A/block10_latticeNittyGritty.html

Excellent reference. The only improvement I could think of is to abuse
the non-standard evaluation of 'groups' to avoid repeating the name of
the dataset, which would go something like

xyplot(lifeExp ~ gdpPercap | factor(year), yDat, aspect = 2/3,
   grid = TRUE, scales = list(x = list(log = 10, equispaced.log = FALSE)),
   col = jDarkGray, pch = jPch,
   groups = list(cex = sqrt(pop/pi) / jCexDivisor,
 fill = color),
   panel = function(x, y, ..., groups, subscripts) {
   panel.xyplot(x, y,
cex = groups$cex[subscripts],
fill = groups$fill[subscripts], ...)
 })

Unfortunately, this doesn't work because prepanel.default.xyplot()
tries to be too smart and assumes that 'groups' is a factor. A
workaround is to override the default prepanel function; e.g.,

lattice.options(prepanel.default.xyplot =
function(x, y, ...) list(xlim = extendrange(range(x)),
 ylim = extendrange(range(y

I will try to fix prepanel.default.xyplot() for the next update of lattice.

-Deepayan


>
> On Mon, Mar 11, 2019 at 2:26 PM Sebastien Bihorel <
> sebastien.biho...@cognigencorp.com> wrote:
>
> > Hi,
> >
> > I am a big user/fan of the lattice package for plotting. As far as I know,
> > lattice only offers one method to stratify data within a xyplot panel,
> > using the groups arguments.
> > A contrario, the ggplot package allow users to use different variables for
> > coloring, setting the symbols, the line types or the size of symbols. This
> > frequently comes handy.
> > My question is whether any work has been done in the lattice ecosystem to
> > reproduce this functionality? If so, I would greatly appreciate any
> > pointers to the appropriate package documentation.
> >
> > Thank you
> >
> > Sebastien
> >
> > __
> > R-help@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.
> >
>
>
> --
> Kevin Wright
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Bug : Autocorrelation in sample drawn from stats::rnorm (hmh)

2018-10-05 Thread Deepayan Sarkar
On Fri, Oct 5, 2018 at 2:07 PM hmh  wrote:
>
> On 05/10/2018 10:28, Annaert Jan wrote:
> > you discard any time series structure;
> But that is PRECISELY what a call a bug:
> There should not be any "time series structure" in the output or rnorm,
> runif and so on but there is one.
>
> rnorm(N,0,1)
> should give on average the same output as
> sample(rnorm(N,0,1))

Agreed, but that is not what your code is testing. You seem to think
that something much more specific should be true; namely,

X[1:10] ~ iid normal, then

cor(X[1:9], X[2:10])

and

cor(sample(X[-1]), sample(X[-10]))

should have the same distribution. This is not at all obvious, and in
fact not true.

Please check the reference you have been pointed to. Here is a related
article in the same volume:

https://www.jstor.org/stable/2332719

-Deepayan


> Which is not the case. rnorm(N,0,1) should draw INDEPENDENT samples i.e.
> without time series structure !
>
>
> --
> - no title specified
>
> Hugo Mathé-Hubert
>
> ATER
>
> Laboratoire Interdisciplinaire des Environnements Continentaux (LIEC)
>
> UMR 7360 CNRS -  Bât IBISE
>
> Université de Lorraine  -  UFR SciFA
>
> 8, Rue du Général Delestraint
>
> F-57070 METZ
>
> +33(0)9 77 21 66 66
> - - - - - - - - - - - - - - - - - -
> Les réflexions naissent dans les doutes et meurent dans les certitudes.
> Les doutes sont donc un signe de force et les certitudes un signe de
> faiblesse. La plupart des gens sont pourtant certains du contraire.
> - - - - - - - - - - - - - - - - - -
> Thoughts appear from doubts and die in convictions. Therefore, doubts
> are an indication of strength and convictions an indication of weakness.
> Yet, most people believe the opposite.
>
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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.


Re: [R] Access function as text from package by name

2018-09-28 Thread Deepayan Sarkar
On Fri, Sep 28, 2018 at 2:16 PM Bert Gunter  wrote:
>
> Do you mean:
> ?get

Doesn't work with :: etc:

> get("graphics::box")
Error in get("graphics::box") : object 'graphics::box' not found

I think parse()+eval() is pretty much unavoidable. After that, it's a
choice between deparse() and print()+capture.output().

-Deepayan


> On Thu, Sep 27, 2018, 11:44 PM Sigbert Klinke 
> wrote:
>
> > Hi,
> >
> > I guess I was not clear enough: the name of the function is stored as
> > string. Solutions which use the object directly do not help unfortunately.
> >
> > Thanks Sigbert
> >
> > Am 27.09.2018 um 12:30 schrieb Sigbert Klinke:
> > > Hi,
> > >
> > > I want to have a function, e.g. graphics::box, as text.
> > > Currently I'am using
> > >
> > > deparse(eval(parse(text='graphics::box')))
> > >
> > > It is important that '::' and ':::' can be used in the name.
> > >
> > > Is there a simpler way?
> > >
> > > Thanks
> > >
> > > Sigbert
> > >
> > >
> > >
> > > __
> > > R-help@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.
> > >
> >
> >
> > --
> > https://hu.berlin/sk
> > https://hu.berlin/mmstat3
> >
> > __
> > R-help@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.
> >
>
> [[alternative HTML version deleted]]
>
> __
> R-help@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@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] The R Journal, Volume 6, Issue 2

2015-01-31 Thread Deepayan Sarkar
Dear All,

The latest issue of The R Journal is now available at
http://journal.r-project.org/archive/2014-2/

Many thanks to all contributors.

Regards,
-Deepayan

___
r-annou...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-announce

__
R-help@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] The R Journal, Volume 6, Issue 1

2014-08-05 Thread Deepayan Sarkar
Dear All,

The latest issue of The R Journal is now available at
http://journal.r-project.org/archive/2014-1/

Many thanks to all contributors, and apologies for the delay.

Regards,
-Deepayan

___
r-annou...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-announce

__
R-help@r-project.org mailing list
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.


Re: [R] Trellis devices and pointize

2014-06-21 Thread Deepayan Sarkar
On Thu, Jun 12, 2014 at 2:31 PM, Patrick Connolly
 wrote:
> On Mon, 09-Jun-2014 at 08:33AM +0100, Prof Brian Ripley wrote:
>
> |> The issue here is not trellis.device.
> |>
> |> You are using lattice plots (without mentioning lattice), which are
> |> based on package 'grid' and so using the grid sub-system of a
> |> device. That sub-system does not use the 'pointsize' of the device
> |> as its initial font size.  So you need to use
> |>
> |> grid::gpar(fontsize = 28)
> |>
> |> If I call that after opening the device I get what I guess you expected.
>
> I don't see it making any difference at all.  At one time I supposed that 
> some clever inner
> workings rescaled everything to something more sensible for a plotting
> region that size, but that is not the case.
>
>>   trellis.device(device = pdf, file = "Singers.pdf", height = 160/25.4,
> +  width = 160/25.4)
>
>> grid::get.gpar()$fontsize
> [1] 12
>> grid::gpar(fontsize = 8)
> $fontsize
> [1] 8
>
>> grid::get.gpar()$fontsize
> [1] 12
>>
>
> Evidently I missed something somewhere.  Grid graphics is sometimes a
> bit too subtle for me.  I know gpar() doesn't work exactly analogously
> to the way par() works.  I can't find any examples in Paul's "R
> Graphics" book or Deepayan's "Lattice" book (but that might just be
> lack of searching skills).
>
> Then I tried putting it in the call to print.trellis:
>
>   print(pik, plot.args = grid::gpar(fontsize = 8))
>
> and in the bwplot() call similar to the way I'd done in panel functions
> with an argument called gp but I get no error message or any
> difference in my resulting plot.
>
> I know I can fiddle with trellis.par.get() and trellis.par.set() but
> that's a bit long-winded when it's so simple to do in base graphics.

Unfortunately that's what you will need to do. The font sizes for text
and points are taken from

> trellis.par.get("fontsize")
$points
[1] 8

$text
[1] 12

and these are the hard-coded defaults for all devices.

There was a discussion last year:

https://stat.ethz.ch/pipermail/r-help/2013-June/354749.html

which almost convinced me to effectively change the default to the
device fontsize, but on further thought I changed my mind (and I see
now that I forgot to follow-up on the list to say so). What did change
was that you can now explicitly set

trellis.par.set(fontsize = list(text = NULL))

to make the default come from grid (which in turn takes the default
from the device).

So,

trellis.device(device = pdf, file = "Singers.pdf", height = 160/25.4,
  width = 160/25.4, pointsize = 28)
trellis.par.set(fontsize = list(text = NULL)) # new line
pik <- bwplot(voice.part ~ height, data = singer)# pointsize ignored
print(pik)
dev.off()

should give you what you want. And if you want this to be the default
behaviour, set

lattice.options(default.theme = list(fontsize = list(text = NULL)))

in your .Rprofile.

-Deepayan


> TA
>
> |>
> |>
> |> On 09/06/2014 07:54, Patrick Connolly wrote:
> |> >How is the pointsize set in trellis.devices?
> |> >
> |> >>From my reading of the trellis.device help file, I understood that the
> |> >pointsize arg would be referenced to the call to the pdf function.
> |> >
> |> >So I set up a trellis pdf device as so:
> |> >
> |> >   trellis.device(device = pdf, file = "Singers.pdf", height = 160/25.4,
> |> >  width = 160/25.4, pointsize = 28)
> |> >
> |> >A base R graphics plot works as I'd expected.
> |> >
> |> >   plot(1:10, 50:59) # silly plot with huge plotting characters and 
> letters
> |> >
> |> >However, pointsize is ignored in trellis plots;
> |> >
> |> >   pik <- bwplot(voice.part ~ height, data = singer)# pointsize ignored
> |> >   print(pik)
> |> >   dev.off()
> |> >
> |> >There are many trellis cex-type settings, but FWIU they're all
> |> >relative to the default size.  My question is: How do I set that
> |> >default?
> |> >
> |> >
> |> >R version 3.0.2 (2013-09-25)
> |> >Platform: i686-pc-linux-gnu (32-bit)
> |> >
> |> >locale:
> |> >  [1] LC_CTYPE=en_US.UTF-8   LC_NUMERIC=C
> |> >  [3] LC_TIME=en_US.UTF-8LC_COLLATE=en_US.UTF-8
> |> >  [5] LC_MONETARY=en_US.UTF-8LC_MESSAGES=en_US.UTF-8
> |> >  [7] LC_PAPER=en_US.UTF-8   LC_NAME=C
> |> >  [9] LC_ADDRESS=C   LC_TELEPHONE=C
> |> >[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
> |> >
> |> >attached base packages:
> |> >[1] grDevices utils stats graphics  methods   base
> |> >
> |> >other attached packages:
> |> >[1] RColorBrewer_1.0-5 lattice_0.20-24
> |> >
> |> >loaded via a namespace (and not attached):
> |> >[1] grid_3.0.2  plyr_1.8tools_3.0.2
> |> >
> |> >I've tried with R-3.1.0 on another machine so I don't think the
> |> >problem is with an old version.
> |> >
> |> >I doubt it has much to do with pdf specifically.  Attempts to use png,
> |> >bitmap, postscript devices all produce equivalent results.
> |> >
> |> >
> |> >
> |> >(Here's all the example code uninterrupted:)
> |> >
> |> >   trellis.device(device = pdf, file = "

Re: [R] Lattice, ggplot, and pointsize

2013-06-05 Thread Deepayan Sarkar
On Sun, May 26, 2013 at 12:47 AM, Milan Bouchet-Valat  wrote:
> Le mardi 21 mai 2013 à 21:39 +0100, Prof Brian Ripley a écrit :
>> On 21/05/2013 21:24, Bert Gunter wrote:
>> > At the risk of misunderstanding... (inline)
>> >
>> > On Tue, May 21, 2013 at 12:17 PM, Milan Bouchet-Valat  
>> > wrote:
>> >> Le mardi 21 mai 2013 à 08:17 -0700, Jeff Newmiller a écrit :
>> >>> That is like complaining that your hammer does not fit these
>> >>> newfangled Philips screws.
>> >>>
>> >>> These are different tools. Do not expect them to interoperate.
>> >> I understand that Lattice and ggplot2 do not use settings from par().
>> >> I'm fine with this, as these packages are different from base graphics
>> >> and have they own equivalent to tweak settings.
>> >>
>> >> What I do not understand is that one argument passed to output devices,
>> >> which are _not_ provided by package graphics, is ignored by these two
>> >> packages. Lattice and ggplot2 do not provide an alternative output
>> >> system,
>> >
>> > False, I believe, depending on what you mean by "output system". They
>> > both use grid graphics, not base graphics and with lattice, anyway,
>>
>> Indeed.  The issue is a design difference between the base and grid
>> graphics subsystems.  See the 'R Internals' manual for more details.
>
> Thanks for the pointers. Indeed there is some interesting documentation
> there. I've also had a deeper look at the code, and I've traced
> pointsize (called ps) back to the R_GE_gcontext struct in
> src/include/R_ext/GraphicsEngine.h.
>
> If I understand correctly, base graphics draw text using GText() in
> src/library/graphics/src/graphics.c, which in turn calls GEText() in
> src/main/engine.c. GEText() does take into account the pointsize.
>
> On the other hand, grid graphics draw text using gridText() from
> src/library/grid/src/grid.c. This function uses gcontextFromgpar() to
> get its R_GE_gcontext object. gcontextFromgpar() (defined in gpar.c)
> computes the pointsize from the fontsize gpar setting and a general
> scaling of the output:
> /*
>  * Scale by GSS_SCALE (a "zoom" factor)
>  */
> gc->ps = gpFontSize(gp, i) * REAL(gridStateElement(dd, GSS_SCALE))[0];
>
> What is interesting is that when a new device gets initialized by grid
> (initGPar() atin gpar.c), the fontsize gpar settings is set to the
> device starting pointsize:
> REAL(gpfs)[0] = dev->startps;
>
> And indeed this works with svg():
>> svg("test.svg", pointsize=5)
>> get.gpar("fontsize")
> $fontsize
> [1] 5
>
> ...but not with Lattice:
>> trellis.par.get("fontsize")
> $text
> [1] 12
>
> $points
> [1] 8
>
>
> So the problem does not appear to be a base vs. grid graphics issue, but
> rather something specific to Lattice and ggplot2. And indeed, when
> looking at Lattice's sources, canonical.theme() in settings.R does:
>  fontsize = list(text = 12, points = 8),
>
> As simple as that! I didn't need to look so deep into the code... :-/
>
>
> ?trellis.par.set says:
>  The initial settings for each device defaults to values
>  appropriate for that device. In practice, this boils down to three
>  distinct settings, one for screen devices like ‘x11’ and
>  ‘windows’, one for black and white plots (mostly useful for
>  ‘postscript’) and one for color printers (color ‘postcript’,
>  ‘pdf’). [This may not be up-to-date, though...]
>
> So it does not appear completely absurd to try to adjust to the device
> settings where appropriate. Pointsize seems such a case to me.
> canonical.theme() could set the text font size to get.gpar("fontsize").
> Since the default value is 12 for most devices, this would not change
> anything by default. (ggplot2 could probably benefit from a similar
> change.)

If I remember correctly (it was a long time ago), I had nothing in
particular against the default text fontsize. The default symbol size
in grid was larger than I liked, and so fontsize$points was set to
something more reasonable, and fontsize$text just seemed natural to
add. It is only ever used in a single call to gpar() inside
print.trellis. So it seems perfectly reasonable to take the default of
fontsize$text from grid instead.

I don't like the idea of making the default
'get.gpar("fontsize")$fontsize' though, because that requires a device
to be active, which canonical.theme() doesn't by design. Instead I
have changed the default to NULL, and the fontsize is now set from (in
order of priority)

1. trellis.par.get("fontsize")$text  # NULL by default
2. trellis.par.get("grid.pars")$fontsize # NULL by default
3. get.gpars()$fontsize # approximately device pointsize by default

Hopefully this works for you. You can test using the r-forge version.

-Deepayan

> I realize this is no longer a discussion relevant for R as a whole, but
> I am posting it here nevertheless in case somebody was interested. Maybe
> we should discuss this offlist with Deepayan.

[...]

__
R-help@r-pro

Re: [R] lattice dotplot reorder contiguous levels

2012-09-26 Thread Deepayan Sarkar
On Wed, Sep 26, 2012 at 3:36 PM, maxbre  wrote:
> sorry for my slow reply, this is what I worked out so far…
>
> my reproducible example and the code
>
> test<-structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
> 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("A",
> "B", "C", "D", "E"), class = "factor"), conc = c(2.32, 0.902,
> 0.468, 5.51, 1.49, 0.532, 0.72, 0.956, 0.887, 20, 30, 2.12, 0.442,
> 10, 50, 110, 3.36, 2.41, 20, 70, 3610, 100, 4.79, 20, 0.0315,
> 30, 60, 1, 3.37, 80, 1.21, 0.302, 0.728, 1.29, 30, 40, 90, 30,
> 0.697, 6.25, 0.576, 0.335, 20, 10, 620, 40, 9.98, 4.76, 2.61,
> 3.39, 20, 4.59), samp.time = structure(c(2L, 4L, 4L, 4L, 4L,
> 4L, 5L, 4L, 8L, 8L, 8L, 8L, 8L, 9L, 8L, 7L, 8L, 8L, 8L, 8L, 3L,
> 3L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 6L, 4L, 8L, 4L, 8L, 4L, 3L,
> 8L, 4L, 8L, 4L, 8L, 4L, 9L, 3L, 8L, 8L, 8L, 8L, 8L, 8L, 1L), .Label = c("2",
> "4", "12", "24", "96", "135", "167", "168", "169"), class = "factor")),
> .Names = c("site",
> "conc", "samp.time"), row.names = c(NA, 52L), class = "data.frame")
>
> test$samp.time.new <- with(test, reorder(samp.time:site, as.numeric(site)))
>
> m<-match(unique(droplevels(test$samp.time.new)),test$samp.time.new)
> lab<-as.character(test$samp.time[m])
>
> dotplot(samp.time.new~conc|site, data=test,
> ylim=lab,
> scales=list(x=list(log=10), y = list(relation = "free")),
> layout=c(1,5), strip=FALSE, strip.left=TRUE
> )
>
> Now labels are correctly spaced and sorted out but still a “slight” problem
> persist: i.e. the reordering of samp.time.new within each site; in fact, I
> would like to have an ascending order of sampling time but for some reason
> I’m not able to work it out properly…

For that, it should be enough to just reorder again by samp.time. Your
label code doesn't work then (I haven't tried to figure out why), but
your earlier idea using strsplit() does:


test$samp.time.new <-
with(test, reorder(reorder(samp.time:site, as.numeric(site)),
as.numeric(samp.time)))

lab <- sapply(strsplit(levels(test$samp.time.new), ":", fixed=TRUE), "[", 1)

dotplot(samp.time.new~conc|site, data=test,
ylim=lab,
scales=list(x=list(log=10), y = list(relation = "free")),
layout=c(1,5), strip=FALSE, strip.left=TRUE
)

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] lattice dotplot reorder contiguous levels

2012-09-20 Thread Deepayan Sarkar
On Thu, Sep 20, 2012 at 7:48 PM, maxbre  wrote:
> my reproducible example
>
> test<-structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L,
> 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
> 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L), .Label = c("A",
> "B", "C", "D", "E"), class = "factor"), conc = c(2.32, 0.902,
> 0.468, 5.51, 1.49, 0.532, 0.72, 0.956, 0.887, 20, 30, 2.12, 0.442,
> 10, 50, 110, 3.36, 2.41, 20, 70, 3610, 100, 4.79, 20, 0.0315,
> 30, 60, 1, 3.37, 80, 1.21, 0.302, 0.728, 1.29, 30, 40, 90, 30,
> 0.697, 6.25, 0.576, 0.335, 20, 10, 620, 40, 9.98, 4.76, 2.61,
> 3.39, 20, 4.59), samp.time = structure(c(2L, 4L, 4L, 4L, 4L,
> 4L, 5L, 4L, 8L, 8L, 8L, 8L, 8L, 9L, 8L, 7L, 8L, 8L, 8L, 8L, 3L,
> 3L, 2L, 4L, 4L, 4L, 4L, 4L, 1L, 4L, 6L, 4L, 8L, 4L, 8L, 4L, 3L,
> 8L, 4L, 8L, 4L, 8L, 4L, 9L, 3L, 8L, 8L, 8L, 8L, 8L, 8L, 1L), .Label = c("2",
> "4", "12", "24", "96", "135", "167", "168", "169"), class = "factor")),
> .Names = c("site",
> "conc", "samp.time"), row.names = c(NA, 52L), class = "data.frame")
>
>
>
> dotplot(samp.time~conc|site, data=test,
> scales=list(x=list(log=10), y = list(relation = "free")),
> layout=c(1,5), strip=FALSE, strip.left=TRUE
> )
>
>
> my objective is to use “site” as conditioning variable but with “samp.time”
> correctly grouped by “site”; the problem here is to ensure that levels of
> “samp.time” within each “site” are contiguous as otherwise they would be not
> contiguous in the dot plot itself (i.e, avoid that sort of holes in between
> y axis categories -see dotplot -)
>
>
> I’ve been trying with this but without much success
>
> test$samp.time.new<-
>   with(test,reorder(samp.time,as.numeric(site)))
>
>
> dotplot(samp.time.new~conc|site, data=test,
> scales=list(x=list(log=10), y = list(relation = "free")),
> layout=c(1,5), strip=FALSE, strip.left=TRUE
> )
>
> I think (I hope) a possible different solution is to create for "ylim" a
> proper character vector of different length to pass to each panel of the
> dotplot (I’m not posting this attempt because too much confused up to now)
>
> can anyone point me in the right direction?

The problem here is that there is crossing between sites and
samp.time. You can try some imaginative permutations of site, such as

test$samp.time.new <- with(test, reorder(samp.time,
as.numeric(factor(site, levels = c("A", "C", "D", "B", "E")

which gets all but site B right. There may be another permutation that
works for everything, but it would be much easier to make a nested
factor, i.e.,

test$samp.time.new <- with(test, reorder(samp.time:site, as.numeric(site)))

That just leaves getting the y-labels right, which I will leave for
you to figure out.

(Hint: ylim = some_function_of(levels(test$samp.time.new)))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Adding legends on plots using Lattice package

2012-09-20 Thread Deepayan Sarkar
On Mon, Sep 17, 2012 at 2:34 PM, jpm miao  wrote:
> To dear Dr Sarkar and anyone that knows about Lattice package,
>
>I make 4 graphs by Lattice package. Each of the graphs has two time
> series. All the series are plotted in plain lines by default, and I would
> like one series to be in plain line and the other to be in dotted line in
> each graph. How can I modify the command of xyplot in the following line to
> achieve this? It seems that "key" or "auto.key"  parameters are needed, but
> I don't know how to do it. Thank you very much!
>
>
> require(graphics)
> library(lattice)
> data1<-read.csv(file="G_Results_3FX1.csv", header=TRUE)

Reproducible example please (the EuStockMarkets dataset may be suitable).

-Deepayan


> xts<-ts(data1[,2:9],frequency = 4, start = c(1983, 1))
> xyplot(xts, screen=list("a","a","b","b","c","c","d","d"), layout=c(2,2),
> scales=list(x="same",y="same"))
>
>
>
> Miao
>
>
>
>
>

__
R-help@r-project.org mailing list
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.


Re: [R] Thinning Lattice Plot

2012-07-31 Thread Deepayan Sarkar
On Tue, Jul 31, 2012 at 6:43 PM, Elliot Joel Bernstein
 wrote:

> Thanks everyone for your replies. I didn't know about the ecdfplot function,
> so I'll start using that instead of Ecdf. Why is Ecdf not a lattice plot?
> The result certainly looks like other lattice plots, the arguments are
> similar to other lattice plots. In fact, internally it seems to just call
> the "histogram" function with a different prepanel and panel function. Is it
> not considered a lattice plot only because it isn't part of the lattice
> package?

Of course not. What you are saying is a valid description of the
Ecdf.formula() method, which definitely produces a lattice plot (or
trellis plot if you prefer). However, the example you gave, namely,

x <- rnorm(1e6)
Ecdf(x)

ends up calling Ecdf.default(), which is very much a traditional
graphics function. I should add that this is for Hmisc 3.9-2, and
don't know if the behaviour is different with other versions.

Note that Ecdf() has more features than ecdfplot(), in particular it
allows weights.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] lattice legen and auto.key conflict

2012-07-30 Thread Deepayan Sarkar
On Mon, Jul 30, 2012 at 10:16 PM, Bert Gunter  wrote:
> David:
>
> I think one needs to carefully parse the xyplot help, where it says:
>
> "To use more than one legend, or to have arbitrary legends not
> constrained by the structure imposed by key, use the legend argument.
> "
>
> So I presume that this is to be interpreted as: "ONLY the legend
> argument will be used when both key(including auto.key) and legend
> arguments are given." However, this is not clear to me either. The
> Help appears to leave the behavior when one tries to use both
> unspecified.

The help could indeed use some love.

Note however, that the entry for 'auto.key' (surely relevant here) says that:

  More precisely, if 'auto.key' is not 'FALSE', 'groups' is
  non-null, and there is no 'key' or 'legend' argument
  specified in the call, a key is created with 'simpleKey' with
  'levels(groups)' as the first ('text') argument.

And that condition is not satisfied here.

So that leaves the following two options (use key=, or update). The
documentation is silent about whether or not these should be
successful, but fortunately both seem to work.

xyplot(Sepal.Length+Sepal.Width~Petal.Length+Petal.Width, data=iris,
   groups = Species,
   key = simpleKey(text = levels(iris$Species), space="right"),
   legend=list(bottom=list(fun=grid.text,args=list(label="youpi !"

## or

p <- xyplot(Sepal.Length+Sepal.Width~Petal.Length+Petal.Width,
data=iris, groups=Species, auto.key=list(space="right"))

update(p, legend=list(bottom=list(fun=grid.text,args=list(label="youpi !"

(I wish that reading lattice help would be less like trying to follow
an Asimov robot story, but there are too many possible interactions to
document both comprehensively and accurately.)

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Thinning Lattice Plot

2012-07-30 Thread Deepayan Sarkar
On Tue, Jul 31, 2012 at 2:43 AM, Elliot Joel Bernstein
 wrote:
> Is there an easy way to "thin" a lattice plot? I often create plots from
> large data sets, and use the "pdf" command to save them to a file, but the
> resulting files can be huge, because every point in the underlying dataset
> is rendered in the plot, even though it isn't possible to see that much
> detail.
>
> For example:
>
> require(Hmisc)
> x <- rnorm(1e6)
>
> pdf("test.pdf")
> Ecdf(x)
> dev.off()

(This is not a lattice plot, BTW.)

> The resulting pdf files is 31MB.

Hmm, for me it's 192K. Perhaps you have not bothered to update R recently.

> Is there any easy way to get a smaller pdf
> file without having to manually prune the dataset?

In general, as David noted, you need to do some sort of data
summarization; great if tools are available to that, otherwise
yourself. In this case, for example, it seems reasonable to do

Ecdf(quantile(x, probs = ppoints(500, a=1)))

If you don't like to do this yourself, ecdfplot() in latticeExtra will allow

library(latticeExtra)
ecdfplot(x, f.value = ppoints(500, a=1))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] tck = 0 for the colorkey?

2012-07-13 Thread Deepayan Sarkar
On Fri, Jul 13, 2012 at 12:21 PM, Martin Ivanov  wrote:
>  Dear R users,
>
> I am struggling with the colorkey on a levelplot lattice graphic.
> I want that no ticks are printed on the colorkey. That is, I want their size 
> tck=0.
> Merely setting tck=0 t in the colorkey parameter does not work. Setting it in 
> the lattice.par.set()
> removes the ticks from the levelplot, but not from the colorkey.

The tick lengths are hard-coded. I will try to change that; meanwhile,
all I can suggest is something like

 levelplot(volcano, colorkey = list(axis.line = list(col = NA)))

which unfortunately also removes the border around the colorkey.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] trellis margin sizes in absolute units

2012-07-12 Thread Deepayan Sarkar
On Thu, Jul 12, 2012 at 9:39 PM, Martin Ivanov  wrote:
>  Dear R users,
>
> I have a lot of experience with traditional R graphics, but I decided to turn 
> to trellis as
> it was recommended for spatial graphs by the sp package. In traditional R 
> graphics
> I always first set the size of the device region absolute units (e.g.  mm) 
> and then I
> firmly fix the inner margins with mai and the outer margins with oma also in 
> absolute units.
> What is left from the device region is for the plot region. That is my 
> general idea.
>
> My question is, is it possible to achieve that full control with trellis 
> graphics? I have tried
> with the *.paddings parameters, but it seems to me that the margins are still 
> larger than I have set them.
> Here is my example code: (mp is a SpatialPixelsDataFrame)
>
> xlim <- c(.96*bbox(mp)[1, 1], 1.02*bbox(mp)[1, 2]);
> ylim <- c(.992*bbox(mp)[2, 1], 1.005*bbox(mp)[2, 2]);
> b <- 1; t <- 2; # b, t : should be bottom and top figure margins
> l <- 1; r <- 6; # l, r : should be left and right figure margins
> asp <- mapasp(data=mp, xlim=xlim, ylim=ylim);
> # I have set the width of the device region as w millimetres and here I 
> calculate
> # its height, taking care of the aspect ratio
> h <- b + t + asp*(w - (l + r));
>
> postscript(output, onefile=FALSE, paper="special", width=w/25.4, 
> height=h/25.4, horizontal=FALSE, fonts=c("Times"), print.it=FALSE, 
> colormodel="rgb", pointsize=12);
>
> # here I set all the possible paddings to make sure that all other margins 
> are 0:
> trellis.par.set(layout.widths=list(left.padding=l, right.padding=r, 
> key.ylab.padding = 0,
> ylab.axis.padding = 0, axis.key.padding = 0, units="mm"),
> layout.heights=list(bottom.padding=b, top.padding=t, main.key.padding = 0, 
> key.axis.padding = 0, axis.xlab.padding = 0,
> xlab.key.padding = 0, key.sub.padding = 0, units="mm"),
> axis.components=list(bottom=list(pad1=.8), left=list(pad1=.8)));
>
> But still, the resulting margins are larger than I expect and to achieve the 
> desired effect I have to set some parameters to negative numbers. So I guess 
> there are some other graphical parameters that come into play, about which I 
> do not know.
> Which margins are these *.padding parameters controlling: the outer (device) 
> or the inner (figure) margins?

The 'par.settings' are only multipliers for the defaults, and you want
to change the original defaults. For example, the left margin is given
by

> lattice.getOption("layout.widths")$left.padding
$x
[1] 0.5

$units
[1] "char"

which you want to change to, say, list(x = 1, units = "mm"). See
?lattice.options on how to do that (and also the 'lattice.options'
entry in ?xyplot).

You might have better luck with the development version at

https://r-forge.r-project.org/R/?group_id=638

as it has some related bugfixes and documentation improvements.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] the meaning of subscripts

2012-06-30 Thread Deepayan Sarkar
On Thu, Jun 28, 2012 at 9:27 PM, startend  wrote:
> Hi,
>
> Now i am dealing with longitudinal data set and I want to see the rough
> marginal plot for 2 variables separately.
> I found the code from one example here,
>
> reading <-
> read.table("http://www.ats.ucla.edu/stat/R/examples/alda/data/reading_pp.txt";,
> header=T, sep=",")
> reading[reading$id %in% c(4, 27, 31, 33, 41, 49, 69, 77, 87), ]
>
> xyplot(piat~age | id
> , data=reading[reading$id %in% c(4, 27, 31, 33, 41, 49, 69, 77, 87),
> ],panel=function(x,y,*subscripts*){
>        panel.xyplot(x, y, pch=16)
>           panel.lmline(x,y, lty=4)
>      panel.xyplot(reading$agegrp*[subscripts]*, y, pch=3)
>      panel.lmline(reading$agegrp*[subscripts]*,y)
> }
> , ylim=c(0, 80), as.table=T, *subscripts*=T)
>
> I just don't know what the subscripts for and the meaning of that.
> Can someone kindly let me know how it works.

See ?xyplot, particularly the entry for 'panel'.

If a lattice plot has one or more conditioning variables ('id' here),
then the data used in each panel is a subset of the full data.
'subscripts' is an optional argument passed to the panel function that
allows you to obtain the association between the original rows of the
data and the data used in the panels. For example, if your data is

 x  y id
 1  1  1
 2  2  2
 3  3  1
 4  4  2
 5  5  1
 6  6  2
 7  7  1
 8  8  2
 9  9  1
10 10  2

and the formula is y ~ x | id, then the first panel (corresponding to
id = 1) will have subscripts=c(1, 3, 5, 7, 9), and the second will
have c(2, 4, 6, 8, 10).

For a more realistic example, see
http://lattice.r-forge.r-project.org/Vignettes/src/lattice-tricks/regression-lines.pdf
(page 12).

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] lattice histogram log and non log values

2012-06-30 Thread Deepayan Sarkar
On Thu, Jun 28, 2012 at 2:41 AM, LCOG1  wrote:
> Hello all,
>  Please consider the following
>
> library(lattice)
> Colors. <-rep(brewer.pal(7, "Dark2"),2)
> color <- 1
>
> Data.X.. <- data.frame(UnitArea = c(rnorm(1000), rnorm(1000)), Type =
> c(rep("Base",1000),rep("Log",1000)))
>
>                        histogram( ~ UnitArea |  Type, data = Data.X..,
>          xlab = "Unit Area", type = "density",
>          panel = function(x, ... ){
>              panel.histogram(x, ...)
>              panel.mathdensity(dmath = dnorm, col = "black",
>                                args = list(mean=mean(x),sd=sd(x)))
>          }, col = Colors.[color], layout = c(1, 2),
>                   scales=list(log = c(F,T),tick.number=list(8), rot = c(0, 
> 90),
>                 x = list(relation = 'free')))
>
> I want to plot on the same page distributions both observed values and the
> logged values.  I tried using the log parameter e.g. log = c(F,T) but I dont
> think this is right.    When I tried transforming the data before plotting
> the scales were all messed up. Guidance would be appreciated.  Thanks

The latter would be the better approach. You haven't given code, but
you probably didn't add 'breaks=NULL', and without it all panels will
have a common set of breakpoints, so scales effectively will be the
same in all panels.

This works for me:

xx <- exp(rnorm(1000))

DF <- data.frame(UnitArea = c(xx, log(xx)),
 Type = c(rep("Base",1000), rep("Log",1000)))

histogram( ~ UnitArea |  Type, data = DF,
  xlab = "Unit Area", type = "density",
  panel = function(x, ... ){
  panel.histogram(x, ...)
  panel.mathdensity(dmath = dnorm, col = "black",
args = list(mean=mean(x), sd=sd(x)))
  },
  breaks = NULL,
  col = Colors.[color], layout = c(1, 2),
  scales = list(x = list(relation = 'free')))


> Also, is there a way to simply plot multiple panels like the base graphics
> package using  par(new = TRUE) in the following?  It just replaces the first
> plot so maybe I shouldn't be trying to use the lattice package with the base
> graphics package.

Yes, see ?plot.trellis.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Reordering levels of a factor within a lattice dotplot

2012-06-22 Thread Deepayan Sarkar
On Fri, Jun 22, 2012 at 5:50 PM, maxbre  wrote:
> Given my reproducible example
>
> myexample<-structure(list(site = structure(c(4L, 2L, 2L, 4L, 2L, 4L, 4L,
> 3L, 1L, 3L, 1L, 1L, 3L, 4L, 5L, 2L), .Label = c("A", "B", "C",
> "D", "E"), class = "factor"), obs = c(0.302, 0.956, 0.72, 1.21,
> 0.887, 0.728, 1.294, 20.493, 0.902, 0.031, 0.468, 2.318, 4.795,
> 89.581, 4.59, 3618.353), sample = structure(c(6L, 6L, 2L, 8L,
> 7L, 7L, 9L, 4L, 4L, 1L, 1L, 3L, 3L, 10L, 11L, 5L), .Label = c("18/08/2009",
> "20/04/2009", "03/12/2008", "17/03/2009", "05/01/2012", "21/04/2009",
> "17/07/2009", "17/04/2009", "21/07/2009", "29/01/2009", "16/07/2008"
> ), class = "factor", scores = structure(c(2, 3, 2, 3, 4, 4, 2,
> 5, 2, 4, 2), .Dim = 11L, .Dimnames = list(c("18/08/2009", "21/04/2009",
> "20/04/2009", "17/07/2009", "17/04/2009", "21/07/2009", "03/12/2008",
> "16/07/2008", "17/03/2009", "29/01/2009", "05/01/2012"), .Names =
> c("site",
> "obs", "sample"), row.names = c(NA, -16L), class = "data.frame")
>
>
> I want to dotplot with lattice the observations (obs) against sampling dates
> (sample) for each site but I need to reorder the factor levels of sampling
> dates based on the value of observations  within each site (rather than
> keeping the original arbitrary data)
>
> These are my two best (?) attempts both of them not properly working for
> different reasons
>
> #start
>
> library(lattice); library(latticeExtra)
>
> #first attempt
> myexample$sample<-
>  with(myexample, reorder(sample,obs))
>
>
> dotplot(sample ~ obs | site, data=myexample,
>        scales=list(x=list(log=TRUE), y=list(relation="free")),
>        xscale.components = xscale.components.logpower,
>        strip=FALSE, strip.left=TRUE, layout=c(1,5),
>
>        index.cond= function(x,y){median(x)},
>
>        panel = function(x,y,...) {
>          panel.dotplot(x,y,...)
>          panel.abline(v = median(x), col.line="red", lty="dotted")
>        }
>        )
>
>
> #second attempt
> myexample$sample<-
>  with(myexample, reorder(reorder(sample,obs), as.numeric(site)))
>
>
> dotplot(sample ~ obs | site, data=myexample,
>        scales=list(x=list(log=TRUE), y=list(relation="free")),
>        xscale.components = xscale.components.logpower,
>        strip=FALSE, strip.left=TRUE, layout=c(1,5),
>
>        index.cond= function(x,y){median(x)},
>
>        panel = function(x,y,...) {
>          panel.dotplot(x,y,...)
>          panel.abline(v = median(x), col.line="red", lty="dotted")
>        }
>        )
>
> #end
>
> There is to note the presence of some ties (i.e. same sampling dates,
> particularly noticeable for site A and B).
> The number of factor levels related to sampling dates (11) is different than
> total number of observations (17): is this responsible for the lack of
> reordering for factor sample in the dotplot?
> How to fix this ? How to get a neat y axis without that “holes” in between
> of the sampling dates within each site?
>
> Should I try to make somehow as much factor levels as the observations so
> that to avoid this sort of problem? Is there any alternative solution?

Yes, you need to avoid duplicates. Here is one way to do that:

myexample$sampleLabel <- as.character(myexample$sample)
myexample$sampleId <- gl(length(myexample$sample), 1)

myexample$sample2 <-
with(myexample, reorder(reorder(sampleId, obs),
as.numeric(site)))

## correct plot, but useless y-axis labels

dotplot(sample2 ~ reorder(site, obs) | site, data = myexample,
scales=list(x=list(log=TRUE), y=list(relation="free")),
xscale.components = xscale.components.logpower,
strip=FALSE, strip.left=TRUE, layout=c(1,5))

## match reordered levels with original order, and set axis labels

nl <- as.numeric(levels(myexample$sample2))

dotplot(sample2 ~ obs | reorder(site, obs), data = myexample,
scales=list(x=list(log=TRUE), y=list(relation="free")),
ylim = myexample$sampleLabel[nl],
xscale.components = xscale.components.logpower,
strip=FALSE, strip.left=TRUE, layout=c(1,5))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] aligning axis labels in a colorkey from levelplot

2012-06-22 Thread Deepayan Sarkar
On Tue, Jun 19, 2012 at 10:55 PM, Stephen Eglen
 wrote:
>>
>> Justification is hard-coded, and that's not easy to change. This is
>> not only for the colorkey; e.g.,
>>
>>   xyplot(y~1, scales = list(alternating = 3))
>>
>> will also give you left-aligned axes on the right.
>>
>> My only suggestion (other than custom labels as suggested by ilai) is
>>
>>  levelplot(matrix(4:12,3,3), colorkey = list(space = "left"))
>
> Thanks Deepayan; I tried the custom labels approach, but unfortunately I
> don't think that works as-is; the labels are of different widths as the
> font is not fixed-width.

You can specify a fixed-width fontfamily if that helps:

levelplot(matrix(seq(4,120,l=9),3,3),
  colorkey = list(at = seq(0, 120, 20),
  labels = list(labels = c('  0',' 20',' 40','
60',' 80','100','120'),
fontfamily = "courier",
font = 1)))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] exact relative positioning of lattice plots

2012-06-22 Thread Deepayan Sarkar
On Wed, Jun 20, 2012 at 7:18 PM, John G. Bullock  wrote:
>
>> In your actual use case, are there any annotations outside the panel
>> (tick marks, labels, etc.)? Things would be much simpler if not (as in
>> your example). In that case, just call the suitable panel function
>> after setting up the proper axis limits (after doing pushViewport or
>> seekViewport or whatever).
>>
>> If you do have extra annotation, you need to decide how much space
>> they should have.
>
> Thank you.  Yes, my actual use case does have annotations outside the panel.  
> Even so, this is helpful.

You should be able to specify absolute dimensions for the annotation
elements using ?lattice.options (also settable through the
'lattice.options' argument in xyplot() etc.). For a list of legitimate
component names, see

str(lattice.getOption("layout.heights"), max.level=2)

Each component should be a list of arguments to grid::unit().

Due to a bug this doesn't work properly in the current released
version, but should be fixed in the r-forge version available at

https://r-forge.r-project.org/R/?group_id=638

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] how to make a histogram with percentage on top of each bar?

2012-06-22 Thread Deepayan Sarkar
On Thu, Jun 21, 2012 at 10:56 PM, york8866  wrote:
> I have a dataset like the following:
> ID      DV
> 1       0.868576818
> 2       0.337120116
> 3       0.029233775
> 4       0.719783525
> 5       0.976631182
> 6       0.672941605
> 7       0.13239462
> 8       0.99936475
> 9       0.91540604
> 10      0.545686514
>
> to get a histogram with y axis as percentage, I wrote the following code"
> library(lattice)
> histogram(data)
>
> now , how to input the percentage and cumulative percentage on top of each
> bar?

You will need a custom panel function, along the lines of

mypanel <-
function(x, breaks, nint = round(log2(length(x)) + 1), ...)
{
if (missing(breaks))
breaks <- do.breaks(range(x, finite = TRUE), nint)
panel.histogram(x, breaks = breaks, ...)
h <- hist(x, breaks = breaks, plot = FALSE)
breaks <- h$breaks
nb <- length(breaks)
yy <- 100 * h$counts / length(x)
panel.text(x = (breaks[-1] + breaks[-nb])/2, y = yy,
   labels = round(cumsum(yy), 2), pos = 3)
}

histogram(data, panel = mypanel)

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] exact relative positioning of lattice plots

2012-06-20 Thread Deepayan Sarkar
On Wed, Jun 20, 2012 at 9:26 AM, John G. Bullock  wrote:
>
> Hello,
>
> I have two lattice plots for which panel.height is fixed.  I am trying to 
> position them
> in a column so that there is exactly half an inch of vertical space between 
> them.
> This would be a simple task if I were not fixing panel.height.  But given 
> that I am,
> I can't find a way to put exactly half an inch of space between the plots.  
> Is there a way?

In your actual use case, are there any annotations outside the panel
(tick marks, labels, etc.)? Things would be much simpler if not (as in
your example). In that case, just call the suitable panel function
after setting up the proper axis limits (after doing pushViewport or
seekViewport or whatever).

If you do have extra annotation, you need to decide how much space
they should have.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] aligning axis labels in a colorkey from levelplot

2012-06-18 Thread Deepayan Sarkar
On Sat, Jun 16, 2012 at 1:41 PM, Stephen Eglen
 wrote:
> R does a great job with the fine details regarding plots.  e.g in the
> following:
>
> library(lattice)
> y <- -4:4/10
> xyplot(y~1, las=1)
>
> the y axis is labelled with numbers -0.4, -0.2, 0.0, 0.2, 0.4 with the
> numbers aligned on the decimal point.
>
> How do I get the same behaviour in the colorkey of a levelplot?  e.g.
>
> levelplot(matrix(y,3,3))
>
> the numbers in the colorkey seem left-aligned, and because of the minus
> sign, the numbers now do not align on the decimal point.  Likewise when
> the number of digits changes:
>
> levelplot(matrix(4:12,3,3))

Justification is hard-coded, and that's not easy to change. This is
not only for the colorkey; e.g.,

  xyplot(y~1, scales = list(alternating = 3))

will also give you left-aligned axes on the right.

My only suggestion (other than custom labels as suggested by ilai) is

 levelplot(matrix(4:12,3,3), colorkey = list(space = "left"))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] some help to improve "hist to plot relative frequencies"

2012-06-18 Thread Deepayan Sarkar
On Fri, Jun 15, 2012 at 9:22 PM, gianni lavaredo
 wrote:
> Dear Researches,
>
> sorry for disturb. I wish to improve my figure in R plotting the relative
> frequencies of my data set.
>
> library(lattice)
> a <- c(0,0,0,1,1,2,4,5,6,7,7,7,7,7,8,8,8,8,9,9,9,9,10,10,11)
> histogram(a, xlab="myData")
>
> what i wish to do is:
>
> 1) invert the order of X and Y (eg: Precent of Total on X-axis and "MyData"
> on X-axis)
> 2) plot not the bar of histogram but a line (i tried with
> "lines(density(a))" but the result is not what i wish)

Take your pick:

ta <- table(a)

dotplot(ta, type = "h", lwd = 2, origin = 0)

dotplot(100 * prop.table(ta), type = "h", lwd = 2, origin = 0,
xlab = "Percent of total")

xyplot(as.numeric(names(ta)) ~ 100 * prop.table(ta),
   type = "h", lwd = 2, origin = 0, horizontal = TRUE,
   xlab = "Percent of total", ylab = "myData",
   xlim = c(-1, NA))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Adding title to colorkey

2012-06-18 Thread Deepayan Sarkar
On Sat, Jun 16, 2012 at 2:10 PM, Stephen Eglen
 wrote:
>
> A recent paper on visualisation (in Neuron, a leading neuroscience
> journal) surveyed how well previous articles in this journal labelled their
> graphs (e.g. axis labelling and describing their error bars).  Of
> particular interest is that (only) 40% of plots labelled what their
> colorkey was showing (variable and units).
>
> The paper is at http://dx.doi.org/10.1016/j.neuron.2012.05.001
>
> R is not yet that prominent (compared to matlab) in Neuroscience, so I
> doubt many of the graphs were generated by levelplot() and friends.
> However, how can the colorkey be labelled?  I notice that this topic has
> been raised before, e.g.
>
>  http://tolstoy.newcastle.edu.au/R/e16/help/11/11/2281.html
>
> For now, I've done:
>
> library(lattice)
> library(grid)
> levelplot(matrix(1:9,3,3),
>          par.settings = list(layout.widths = list(axis.key.padding = 4)))
> grid.text('title here', y=unit(0.5, "npc"),
>          rot=90, x=unit(0.88, "npc"))
>
> i.e. adding some space between levelplot and colorkey.  The
> x,y positions of the grid.text call need fine-tuning once the plot is
> close to finalised.
>
> Does anyone have a better solution for vertical colorkeys?  e.g. can the
> plot objected be interrogated to work out what the central x,y value is?

This is slightly simpler:

levelplot(matrix(1:9,3,3), ylab.right = "title here",
  par.settings = list(layout.widths = list(axis.key.padding = 0,
   ylab.right = 2)))

There really should be a function allowing easy construction of
complex legends combining simpler ones. There is currently only
mergedTrellisLegendGrob in latticeExtra (not very robust) which can be
used as follows:

library(latticeExtra)

p <- levelplot(matrix(1:9,3,3))
p$legend$right <-
list(fun = mergedTrellisLegendGrob(p$legend$right,
   list(fun = textGrob,
args = list("title here",
rot = -90)),
   vertical = FALSE))
p

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] how to add a vertical line for each panel in a lattice dotplot with log scale?

2012-06-15 Thread Deepayan Sarkar
On Mon, Jun 11, 2012 at 1:10 PM, maxbre  wrote:
> sorry but I can't close this thread with a viable solution other than the
> following one
> (i.e. by defining an user function to add line);
>
> I understand that the problem is related to the fact that:
> mean(log(.)) != log(mean(.)) is
> but for some reason I can't put all that in practice inside the
> panel.abline(...)
>
> I would appreciate if someone can show me how (sorry but at this point I
> must give up...),

I'm not sure why this is difficult. Once you realize that your problem
was in taking mean of the log-transformed values instead of the
original values, all you need to do is transform back to the original
scale, compute mean, and transform back.

dotplot(date_sampl_time_recs ~ lower_b_i | site, data=teq,
scales = list(x = list(log=TRUE)),
xscale.components = xscale.components.logpower,
layout = c(5,1),
panel = function(x,y,...) {
panel.grid(h=53, v=-1, lty="dotted", col="gray")
panel.dotplot(x,y,...)
panel.abline(v = median(x), col.line="red", lty="dotted")
panel.abline(v = log10(mean(10^x)), col.line="blue", lty="dotted")
}
)

-Deepayan

> thank you all for the help
>
>
> # code start
>
> addLine<- function(a=NULL, b=NULL, v = NULL, h = NULL, ..., once=F) {
>  tcL <- trellis.currentLayout()
>  k<-0
>  for(i in 1:nrow(tcL))
>    for(j in 1:ncol(tcL))
>      if (tcL[i,j] > 0) {
>        k<-k+1
>        trellis.focus("panel", j, i, highlight = FALSE)
>        if (once) panel.abline(a=a[k], b=b[k], v=v[k], h=h[k], ...) else
>          panel.abline(a=a, b=b, v=v, h=h, ...)
>        trellis.unfocus()
>      }
> }
>
>
>
> dotplot(date_sampl_time_recs ~ lower_b_i | site, data=teq,
>        scales=list(x=list(log=TRUE)),
>        xscale.components = xscale.components.logpower,
>        layout=c(5,1),
>        panel = function(x,y,...) {
>          panel.grid(h=53, v=-1, lty="dotted", col="gray")
>          panel.dotplot(x,y,...)
>          medians <- median(x)
>          panel.abline(v=medians, col.line="red", lty="dotted")
>          }
>        )
>
> medie<-as.vector(tapply(teq$lower_b_i,teq$site,mean))
>
> addLine(v=log10(medie), once=TRUE, col="blue", lty="dotted")
>
> # code end
>
>
>
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/how-to-add-a-vertical-line-for-each-panel-in-a-lattice-dotplot-with-log-scale-tp4632513p4632991.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> 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@r-project.org mailing list
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.


Re: [R] histogram fill lattice

2012-06-15 Thread Deepayan Sarkar
On Wed, Jun 13, 2012 at 8:10 PM, David Winsemius  wrote:
>
> On Jun 13, 2012, at 9:55 AM, Powell, Jeff wrote:
>
>>
>> Dear all,
>>
>> I would like to change the "fill" pattern of a histogram using histogram()
>> in the lattice package.  I know how to do so using hist(), but would prefer
>> to stay within lattice.
>>
>> dt1 <- rnorm(100,0,1)
>>
>> hist(dt1, density=3, angle=45)
>>
>
> I get diagonal striping.
>
>
>>
>> library(lattice)
>> histogram(dt1,
>>              xlab = "Histogram of rnorm(0,1)",
>>              type = "count",
>>              breaks = 15,
>>              col = "grey")
>
>
> I seem to remember that the diagonal hatched fills were disabled or not
> brought over to lattice from base S graphics because they were thought to be
> causing cognitive distortions[1,2].

The actual reason it was not originally implemented is simply that
grid did not support it.  Of course, the "bad"-ness of hatching may
have been the reason grid doesn't support it, and why no one has
bothered to change the status quo in so many years.

-Deepayan

> You already seem to be able to specify
> the color of the fill, and it's easy to get alternating colored fills with
> col = c("grey","red") ,  so it's not entirely clear what your unsatisfied
> goals are.



>
> --
> David Winsemius, MD
> West Hartford, CT
>
> 1]
> http://markmail.org/message/pmrrpjynrcnrwhg5?q=list:org%2Er-project%2Er-help+lattice+hatching
> 2]
> http://markmail.org/message/cj23tnsgsobs4mbu?q=list:org%2Er-project%2Er-help+lattice+hatching
>
>
> __
> R-help@r-project.org mailing list
> 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@r-project.org mailing list
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.


Re: [R] Lattice 3d coordinate transformation

2012-02-11 Thread Deepayan Sarkar
On Fri, Feb 10, 2012 at 12:43 AM, ilai  wrote:
> Hello List!
> I asked this before (with no solution), but maybe this time... I'm
> trying to project a surface to the XY under a 3d cloud using lattice.
> I can project contour lines following the code for fig 13.7 in
> Deepayan Sarkar's "Lattice, Multivariate Data Visualization with R",
> but it fails when I try to "color them in" using panel.levelplot.
> ?utilities.3d says there may be some bugs, and I think
> ltransform3dto3d() is not precise (where did I hear that?), but is
> this really the source of my problem? Is there a (simple?) workaround,
> maybe using 3d.wire but projecting it to XY? How? Please, any insight
> may be useful.

I don't think this will be that simple. panel.levelplot() essentially
draws a bunch of colored rectangles. For a "3D" projection, each of
these will become (four-sided) polygons. You need to compute the
coordinates of those polygons, figure out their fill colors (possibly
using ?level.colors) and then draw them.

-Deepayan


> Thanks in advance,
> Elai.
>
> A working example:
>
>  ## data "d" and predicted "surf":
> set.seed(1113)
> d <- data.frame(x=runif(30),y=runif(30),g=gl(2,15))
> d$z <- with(d,rnorm(30,3*asin(x^2)-5*y^as.integer(g),.1))
> d$z <- d$z+min(d$z)^2
> surf <- by(d,d$g,function(D){
>  fit <- lm(z~poly(x,2)*poly(y,2),data=D)
>  outer(seq(0,1,l=10),seq(0,1,l=10),function(x,y,...)
> predict(fit,data.frame(x=x,y=y)))
> })
> ##
> # This works to get contours:
> require(lattice)
> cloud(z~x+y|g,data=d,layout=c(2,1), type='h', lwd=3, par.box=list(lty=0),
>      scales=list(z=list(arrows=F,tck=0)),
>      panel.3d.cloud = function(x, y, z,rot.mat, distance,
> zlim.scaled, nlevels=20,...){
>        add.line <- trellis.par.get("add.line")
>        clines <- contourLines(surf[[packet.number()]],nlevels = nlevels)
>        for (ll in clines) {
>          m <- ltransform3dto3d(rbind(ll$x-.5, ll$y-.5,
> zlim.scaled[1]), rot.mat,
>                                distance)
>          panel.lines(m[1,], m[2,], col = add.line$col, lty = add.line$lty,
>                      lwd = add.line$lwd)
>        }
>        panel.3dscatter(x, y, z, rot.mat, distance, zlim.scaled =
> zlim.scaled, ...)
>      }
>      )
> # But using levelplot:
> panel.3d.levels <- function(x, y, z,rot.mat, distance, zlim.scaled,...)
> {
>    zz <- surf[[packet.number()]]
>    n <- nrow(zz)
>    s <- seq(-.5,.5,l=n)
>    m <- ltransform3dto3d(rbind(rep(s,n),rep(s,each=n),zlim.scaled[1]),
>                          rot.mat, distance)
>    panel.levelplot(m[1,],m[2,],zz,1:n^2,col.regions=heat.colors(20))
>    panel.3dscatter(x, y, z, rot.mat, distance, zlim.scaled = zlim.scaled, ...)
>  }
> cloud(z~x+y|g,data=d,layout=c(2,1), type='h', panel.3d.cloud = 
> panel.3d.levels,
>      scales=list(z=list(arrows=F,tck=0)),par.box=list(lty=0),lwd=3)
> # I also tried to "fill" between contours but can't figure out what to
> do with the edges and how to incorporate the x,y limits to 1st and nth
> levels.
> panel.3d.contour <- function(x, y, z,rot.mat, distance,xlim,ylim,
> zlim.scaled,nlevels=20,...)
> {
>    add.line <- trellis.par.get("add.line")
>    zz <- surf[[packet.number()]]
>    clines <- contourLines(zz,nlevels = nlevels)
>    colreg <- heat.colors(max(unlist(lapply(clines,function(ll) ll$level
>    for (i in 2:length(clines)) {
>      ll <- clines[[i]]
>      ll0 <- clines[[i-1]]
>      m <- ltransform3dto3d(rbind(ll$x-.5, ll$y-.5, zlim.scaled[1]),
> rot.mat, distance)
>      m0 <- ltransform3dto3d(rbind(ll0$x-.5, ll0$y-.5,
> zlim.scaled[1]), rot.mat, distance)
>      xvec <- c(m0[1,],m[1,ncol(m):1])
>      yvec <- c(m0[2,],m[2,ncol(m):1])
>      panel.polygon(xvec,yvec,col=colreg[ll$level],border='transparent')
>      panel.lines(m[1,], m[2,], col = add.line$col, lty = add.line$lty,
>                  lwd = add.line$lwd)
>    }
>    panel.3dscatter(x, y, z, rot.mat, distance, zlim.scaled = zlim.scaled, ...)
>  }
> cloud(z~x+y|g,data=d,layout=c(2,1), type='h', panel.3d.cloud = 
> panel.3d.contour,
>      scales=list(z=list(arrows=F,tck=0)),par.box=list(lty=0),lwd=3)
>
> #
>
> __
> R-help@r-project.org mailing list
> 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@r-project.org mailing list
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.


Re: [R] how to combine grouped data and ungrouped data in a trellis xyplot

2012-01-09 Thread Deepayan Sarkar
On Sun, Jan 8, 2012 at 9:45 AM, Mike Dahman  wrote:
> I'm hoping the community knowledge can help me out here. I have found great
> value in R, especially using it to generate charts, but I am still scaling
> the learning curve in a number of ways.
>
> I am looking plot one grouped line and one ungrouped line in a lattice plot.
>
> I can plot one grouped line like this (the line's color in each panel
> becomes dependent on the newpool value):
>
>
>
> xyp<-xyplot(cpucap~date|zone,data=df,type="l",groups=newpool,auto.key=list(points=F,lines=T),
>           main=paste(df$server[1]," CPU Caps\n",df$date[1]," to
> ",df$date[nrow(df)],sep="")
>    )
>    print(xyp)
>
>
> and I can plot two ungrouped lines using a panel=function with subscripts
> like this (maybe not the best way, but I found an example doing it this
> way):
>
>    xyplot(cpu~dt|zone,data=filt_zone_df,ylim=c(0,100),
>           main=paste(server," - Zone CPU (Blue) & Memory (Red)
> Util\n",filt_zone_df$ts[1],"-",filt_zone_df$ts[nrow(filt_zone_df)],sep=""),
>           panel=function(x,y,subscripts){
>               panel.lines(x,y)
>               
> panel.lines(filt_zone_df$dt[subscripts],filt_zone_df$mem[subscripts],col="red")
>    }, as.Table=T, subscripts=T)
>
>
> but I'm struggling with plotting one line that is grouped and one that
> isn't. When I try to pass group to the first panel.xyplot() function in the
> panel=function it either does nothing or bombs out.
>
> xyplot(cpu~dt|zone,data=servdf,ylim=c(0,100),groups=pool,auto.key=list(points=F,lines=T),type="l",
>           main="test",
>           panel=function(x,y,groups,subscripts,...){
>               panel.xyplot(x,y,groups,...)                     # would
> like this to be colored based on the groups=pool

Try

              panel.xyplot(x, y, groups = groups, subscripts = subscripts, ...)

-Deepayan

>
>  panel.lines(servdf$dt[subscripts],servdf$mem[subscripts],col="red")
>    }, as.Table=T, subscripts=T)
>
>
> A little nudge in the right direction is appreciated. I'm getting tripped
> up on how to get the groups definition into the panel function and also
> into the panel.xyplot function within it. I've tried using a number of
> variations in the arguments with the panel=function definition and the call
> to panel.xyplot() within it, but no success. My assumption was that the use
> of '...' would pass it on down, but that doesn't seem to be the
> case, especially since most of the examples I can find from googling show
> folks listing group as an argument, and sometimes have something like
> groups=groups. I've tried a number of things and thought it is time to ask
> for help.
>
> Regards,
>
> -mike
>
>        [[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> 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@r-project.org mailing list
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.


Re: [R] lattice key in blank panel

2011-12-15 Thread Deepayan Sarkar
On Fri, Dec 16, 2011 at 1:22 AM, Max Kuhn  wrote:
> Somewhere I've seen an example of an xyplot() where the key was placed
> in a location of a missing panel. For example, if there were 3
> conditioning levels, the panel grid would look like:
>
> 34
> 12
>
> In this (possibly imaginary) example, there were scatter plots in
> locations 1:3 and location 4 had no conditioning bar at the top, only
> the key.
>
> I can find examples of putting the legend outside of the panel
> locations (e.g to the right of locations 2 and 4 above), but that's
> not really what I'd like to do.

You are probably thinking of this example from ?splom:

splom(~iris[1:3]|Species, data = iris,
  layout=c(2,2), pscales = 0,
  varnames = c("Sepal\nLength", "Sepal\nWidth", "Petal\nLength"),
  page = function(...) {
  ltext(x = seq(.6, .8, length.out = 4),
y = seq(.9, .6, length.out = 4),
labels = c("Three", "Varieties", "of", "Iris"),
cex = 2)
  })

It's actually easier to do that with legends, as illustrated by this
(somewhat silly) modification:

splom(~iris[1:3]|Species, data = iris, groups = Species,
  layout=c(2,2), pscales = 0,
  varnames = c("Sepal\nLength", "Sepal\nWidth", "Petal\nLength"),
  auto.key = list(x = 0.75, y = 0.75, corner = c(0.5, 0.5)))

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] lines and points in xyplot()

2011-11-23 Thread Deepayan Sarkar
On Wed, Nov 23, 2011 at 10:48 PM, Doran, Harold  wrote:
> Given the following data, I want a scatterplot with the data points and the 
> predictions from the regression.
>
> Sigma <- matrix(c(1,.6,1,.6), 2)
> mu <- c(0,0)
> dat <- mvrnorm(5000, mu, Sigma)
>
> x <- dat[,1] * 50 + 200
> y <- dat[,2] * 50 + 200
>
> fm <- lm(y ~ x)
>
> ### This gives the regression line, but not the data
> xyplot(y ~ x,
>               type = c('g', 'p'),
>               panel = function(x, y){
>               panel.lines(x, predict(fm))
>               }
> )
>
> ### This gives both data but as point
> xyplot(y + predict(fm) ~ x,
>               type = c('g', 'p'),
>               )
>
> I know I can add an abline easily, but my problem is a bit more complex and 
> the code above is just an example.
> What is the best way for the predicted data to form a solid line and let the 
> data points remain as points

See

http://lattice.r-forge.r-project.org/Vignettes/src/lattice-tricks/regression-lines.pdf

(This is a work in progress, so feedback would be appreciated.)

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Adding units to levelplot's colorkey

2011-11-22 Thread Deepayan Sarkar
On Tue, Nov 15, 2011 at 6:53 PM, Carlisle Thacker
 wrote:
> Sorry that I was not clear.  I was asking how to add annotation to
> levelplot's colorkey, not the levelplot itself.  The only entry I can
> find from the help pages is via its labels.
>
> Googling did yield this:  " draw.colorkey() doesn't support a title for
> the legend".  So I presume there is also no support for any other
> annotation.

That is correct. Note that having support for more annotation is not
technically difficult, but it's not clear what a good design would be.

For the purpose you describe, I typically do something like

levelplot(volcano, colorkey = list(space = "top"),
  main = "Height (cm)")

But I don't know how to do this nicely with a vertical color key.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Drawing ticks in the 3rd and 4th row of a lattice

2011-11-22 Thread Deepayan Sarkar
On Fri, Nov 18, 2011 at 11:22 AM, Ashim Kapoor  wrote:
> Dear all,
>
> I want to draw ticks on the 3rd and 4th row of a lattice. How do I do this
> ? In my search of the help, I discovered a parameter alternating,which kind
> of says where the ticks will be but does not suffice for me.

You need to explain more clearly what you want. Your plot does already
have ticks in the 3rd and 4th row. If you need the x-axes labeled in
each row, you need to have something like

 scales=list(x = list(relation = "free", rot = 45, ...

-Deepayan

> I am running this command : -
>
> barchart(X03/1000~time|Company,
>         data=df1[which(df1$time!=1),],
>         horiz=F,
>
> scales=list(x=list(rot=45,labels=paste("Mar",c("07","08","09","10","11"
>         ,par.strip.text=list(lineheight=1,lines=2,cex=.75),
>         ylab = "In Rs.
> Million",xlab="",layout=c(3,4),as.table=T,between=list(y=1))
>
>
> where my data is  : -
>
>> dput(df1)
> structure(list(Company = structure(c(9L, 7L, 1L, 6L, 8L, 4L,
> 2L, 5L, 11L, 10L, 9L, 7L, 1L, 6L, 8L, 4L, 2L, 5L, 11L, 10L, 9L,
> 7L, 1L, 6L, 8L, 4L, 2L, 5L, 11L, 10L, 9L, 7L, 1L, 6L, 8L, 4L,
> 2L, 5L, 11L, 10L, 9L, 7L, 1L, 6L, 8L, 4L, 2L, 5L, 11L, 10L, 9L,
> 7L, 1L, 6L, 8L, 4L, 2L, 5L, 11L, 10L), .Label = c("Bharat Petroleum Corpn.
> Ltd.",
> "Chennai Petroleum Corpn. Ltd.", "Company Name", "Essar Oil Ltd.",
> "Hindalco Industries Ltd.", "Hindustan Petroleum Corpn. Ltd.",
> "Indian Oil Corpn. Ltd.", "Mangalore Refinery & Petrochemicals Ltd.",
> "Reliance Industries Ltd.", "Steel Authority Of India Ltd.",
> "Sterlite Industries (India) Ltd."), class = "factor"), time = c(7,
> 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9,
> 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
> 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1), X03 = c(722931.1, 751620.5, 304456.3, 294868.9, 192712.6,
> 36695.4, 188313.4, 98954.9, 100088.7, 72379.9, 848517.5, 864562.2,
> 347310.9, 301022.1, 253514.5, 165661.6, 206377.7, 108897, 109336.3,
> 71207.6, 1003504.6, 1145993.8, 392261.5, 341086, 289737.4, 359837.2,
> 252964.3, 90036.2, 90474.8, 127623.2, 1411082.1, 907480.4, 364637.5,
> 290915.7, 255397.4, 328557.2, 202855.3, 118725.4, 116647.6, 106254.9,
> 1772254.7, 1204856.9, 469935.6, 313527.6, 320131.1, 384323.5,
> 260813.9, 137403.3, 137238.5, 136888.4, 1151658, 974902.76, 375720.36,
> 308284.06, 262298.6, 255014.98, 64.92, 110803.36, 110757.18,
> 102870.8)), row.names = c("Reliance Industries Ltd..7", "Indian Oil Corpn.
> Ltd..7",
> "Bharat Petroleum Corpn. Ltd..7", "Hindustan Petroleum Corpn. Ltd..7",
> "Mangalore Refinery & Petrochemicals Ltd..7", "Essar Oil Ltd..7",
> "Chennai Petroleum Corpn. Ltd..7", "Hindalco Industries Ltd..7",
> "Sterlite Industries (India) Ltd..7", "Steel Authority Of India Ltd..7",
> "Reliance Industries Ltd..8", "Indian Oil Corpn. Ltd..8", "Bharat Petroleum
> Corpn. Ltd..8",
> "Hindustan Petroleum Corpn. Ltd..8", "Mangalore Refinery & Petrochemicals
> Ltd..8",
> "Essar Oil Ltd..8", "Chennai Petroleum Corpn. Ltd..8", "Hindalco Industries
> Ltd..8",
> "Sterlite Industries (India) Ltd..8", "Steel Authority Of India Ltd..8",
> "Reliance Industries Ltd..9", "Indian Oil Corpn. Ltd..9", "Bharat Petroleum
> Corpn. Ltd..9",
> "Hindustan Petroleum Corpn. Ltd..9", "Mangalore Refinery & Petrochemicals
> Ltd..9",
> "Essar Oil Ltd..9", "Chennai Petroleum Corpn. Ltd..9", "Hindalco Industries
> Ltd..9",
> "Sterlite Industries (India) Ltd..9", "Steel Authority Of India Ltd..9",
> "Reliance Industries Ltd..10", "Indian Oil Corpn. Ltd..10", "Bharat
> Petroleum Corpn. Ltd..10",
> "Hindustan Petroleum Corpn. Ltd..10", "Mangalore Refinery & Petrochemicals
> Ltd..10",
> "Essar Oil Ltd..10", "Chennai Petroleum Corpn. Ltd..10", "Hindalco
> Industries Ltd..10",
> "Sterlite Industries (India) Ltd..10", "Steel Authority Of India Ltd..10",
> "Reliance Industries Ltd..11", "Indian Oil Corpn. Ltd..11", "Bharat
> Petroleum Corpn. Ltd..11",
> "Hindustan Petroleum Corpn. Ltd..11", "Mangalore Refinery & Petrochemicals
> Ltd..11",
> "Essar Oil Ltd..11", "Chennai Petroleum Corpn. Ltd..11", "Hindalco
> Industries Ltd..11",
> "Sterlite Industries (India) Ltd..11", "Steel Authority Of India Ltd..11",
> "Reliance Industries Ltd..1", "Indian Oil Corpn. Ltd..1", "Bharat Petroleum
> Corpn. Ltd..1",
> "Hindustan Petroleum Corpn. Ltd..1", "Mangalore Refinery & Petrochemicals
> Ltd..1",
> "Essar Oil Ltd..1", "Chennai Petroleum Corpn. Ltd..1", "Hindalco Industries
> Ltd..1",
> "Sterlite Industries (India) Ltd..1", "Steel Authority Of India Ltd..1"
> ), .Names = c("Company", "time", "X03"), reshapeLong = structure(list(
>    varying = structure(list(X03 = c("X03.07", "X03.08", "X03.09",
>    "X03.10", "X03.11", "X03.1")), .Names = "X03", v.names = "X03", times =
> c(7,
>    8, 9, 10, 11, 1)), v.names = "X03", idvar = "Company", timevar =
> "time"), .Names = c("varying",
> "v.names", "idvar", "timevar")), class = "data.frame")
>
>        [[alternative HTML version

Re: [R] Lattice xyplot log scale labels help!

2011-11-20 Thread Deepayan Sarkar
On Fri, Sep 16, 2011 at 6:56 PM, Deepayan Sarkar
 wrote:
> On Fri, Sep 16, 2011 at 2:17 AM, Cram Rigby  wrote:
>> I have a problem with lattice log scales that I could use some help with.
>>
>> I'm trying to print log y-axis scales without exponents in the labels.
>>  A similar thread with Deepayan' recommendation is here:
>> http://tolstoy.newcastle.edu.au/R/e11/help/10/09/9865.html.  For
>> example, this code using xyplot produces a logged y-axis but the
>> labels  (e.g. "10^1.2") are not very user-friendly:
>>
>> xyplot(24:300~24:300, scales=list(y=list(log=T)))
>>
>> So, trying another y.scale.component function, we get something more
>> agreeable for y-axis scale labels:
>>
>> xyplot(24:300~24:300, scales=list(y=list(log=T)), yscale.components =
>> yscale.components.log10.3)
>>
>>
>> However, my problem is that occasionally I'll have to plot data that
>> doesn't quite "work".  For example, in the following example, I only
>> get one y-axis scale label:
>>
>> xyplot(11:30~11:30, scales=list(y=list(log=T)), yscale.components =
>> yscale.components.log10.3)
>>
>> or worse, no y-axis scale labels:
>>
>> xyplot(11:19~11:19, scales=list(log=T), yscale.components =
>> yscale.components.log10.3)
>>
>>
>> What would be most helpful is if someone can show me how to use an
>> xyplot y-scale function to mimic log y-scale labels generated with the
>> standard plot command.  This seems to work regardless of the
>> underlying data range:
>>
>> plot(11:30,11:30,log = "y")
>> plot(24:300,24:300,log="y")
>
> That is because the standard graphics log-axis rules (which is
> codified in axTicks(), and depends critically on par("yaxp")) is more
> sophisticated than latticeExtra:::logTicks() (which was originally
> written as a proof-of-concept demo).
>
> To make logTicks() equally sophisticated, we need to replicate the
> logic used to compute par("yaxp") (in src/main/graphics.c, I think),
> which is doable, but not trivial.

And thanks to Martin Maechler, these nontrivial computations are now
more easily accessible outside the traditional graphics world. In the
development version of lattice (on r-forge, not yet released to CRAN),
you can do

xyplot(11:30~11:30, scales=list(y=list(log=T, equispaced.log = FALSE)))

etc. This requires R 2.14.0 or better.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] right justify right-axis tick values in lattice

2011-10-17 Thread Deepayan Sarkar
On Sun, Oct 16, 2011 at 7:20 PM, David Winsemius  wrote:
>
> On Oct 16, 2011, at 1:17 AM, Richard M. Heiberger wrote:
>
>> How can I right justify the right-axis tick values?  They appear in the
>> example below as left-justified.
>>
>> I have tried several different ways and all fail in different ways.
>>
>> The example below creates the right axis tick value with no attempt at
>> adjustment.
>>
>> alternates I have tried are
>> 1. formatting the values.  This doesn't work because they are in a
>> proportional font and the blanks
>> are too narrow.

Using format() and a fixed-width font is not too difficult:

panel.right <- function(x, y, ...) {
panel.barchart(x, y, ...)
panel.axis(side="right", at = pretty(y),
   labels = format(pretty(y)),
   text.fontfamily = "Courier",
   outside=TRUE)
}

A more general solution is not simple, and I think trying to modify
the current panel.axis() to incorporate it would make it unnecessarily
complicated. If one is desired, that should either be a separate
specialized function, or a clean reimplementation of panel.axis() from
scratch.

If anyone contributes such a function, I would be happy to include it
in lattice.

-Deepayan


>> 2. gsub all leading " " characters into two "  " characters.  This
>> overcompenates because a blank
>> is slightly wider than half a digit width.
>>
>> I prefer to keep the default font.  I am willing to go to a fixed width
>> font
>> (courier for example), but I haven't
>> figured out the incantation to make that work in graphics.
>>
>> here is my example:
>>
>> panel.right <- function(x, y, ...) {
>>  panel.barchart(x, y, ...)
>>  print(x);print(y)
>>  panel.axis(side="right", at=pretty(y), outside=TRUE)
>
> If I am reading the code correctly, the justification calculation is
> "hard-calculated" in the sense of not accepting optional control inside
> panel.axis in this code:
>
> if (draw.labels && !is.null(labels)) {
>        {
>            just <- if (outside)
>                switch(side, bottom = if (rot[1] == 0) c("centre",
>                  "top") else c("right", "centre"), top = if (rot[1] ==
>                  0) c("centre", "bottom") else c("left", "centre"),
>                  left = if (rot[2] == 90) c("centre", "bottom") else
> c("right",
>                    "centre"), right = if (rot[2] == 90) c("centre",
>                    "top") else c("left", "centre"))
>
>
>> }
>> mybar <- function(...) {
>>  args <- list(...)
>>  args$par.settings=list(clip=list(panel="off"))
>>  args$par.settings$layout.widths$axis.key.padding <- 4
>
> Since you are allowing the labels to be automatically generated there does
> not appear to be an optional parameter that you can throw the other way. I
> tried modifying your code to supply an explicit set of labels but they
> appear to have been trimmed of their leading spaces.
>
> Hacking panel.axis by changing the "left" to "right" also require()'s grid
> be loaded and you also need to add a triple colon call to
> lattice:::chooseFace, and you need to figure out how to move the
> justification reference over to the right by adding the correct amount in
> "npc" coordinates to orient.factor in the last grid.text call. I suspect
> after experimentation that the reference range is [0,1] along the
> axis-annotation-width, so this modification to that final grid.text call
> works:
>
>  ... ,  x = unit(1, "npc") - (orient.factor-1) * lab.unit, ...
>
> Attached is the code that give the attached plot if you change your
> panel.axis call to:
>       
>       panel.axis.rt(side="right", at=pretty(y), outside=TRUE)
>
> (And remember to load grid.)
>
>
>>  do.call("barchart", args)
>> }
>> mybar(c(1,10,100,10,1) ~ "abcd", panel=panel.right, ylab.right="right")
>>
>>
>> thanks
>> Rich
>>
>
>
>>        [[alternative HTML version deleted]]
>>
>> __
>> R-help@r-project.org mailing list
>> 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.
>
> David Winsemius, MD
> West Hartford, CT
>
>
> __
> R-help@r-project.org mailing list
> 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@r-project.org mailing list
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.


Re: [R] how to plot two surfaces with lattice::wireframe

2011-10-15 Thread Deepayan Sarkar
On Fri, Oct 14, 2011 at 3:07 AM, Carl Witthoft  wrote:
> Hi all,
> I'd like to plot the Real and Imaginary parts of some f(z) as two different
> surfaces in wireframe (the row/column axes are the real and imag axes).  I
> know I can do it by, roughly speaking, something like
>
> plotz <- expand.grid(x={range of Re(z)}, y={range of Im(z), groups=1:2)
> plotz$func<-c(Re(f(z),Im(f(z))
> wireframe(func~x*y,data=plotz,groups=groups)
>
> But that seems like a clunky way to go, especially if I happen to have
> started out with a nice matrix of the f(z) values.
>
> So, is there some simpler way to write the formula in wireframe?  I

This is reasonably simple:

fz <- matrix(complex(real = 1:100, imaginary = 101:200), 10, 10)

zr <- as.vector(row(fz))
zc <- as.vector(row(fz))
wireframe(Re(fz) + Im(fz) ~ zr + zc)

The as.vector() are needed because matrix x and y in the formula have
a special meaning.

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] Multiple levelplot with title

2011-10-15 Thread Deepayan Sarkar
On Sun, Oct 9, 2011 at 10:00 PM, Richard O. Legendi
 wrote:
> Hi all,
>
> I'm new to R and to the mailing list, so please bear with me :-)
>
> I would like to create multiple levelplots on the same chart with a nice
> main title with something like this:
>
>  print(levelplot(matrix(c(1,2,3,4), 2, 2)), split=c(1, 1, 2, 1))
>  print(levelplot(matrix(c(1,2,3,4), 2, 2)), split=c(2, 1, 2, 1),
>        newpage=FALSE)
>
> I found a trick:
>
>  mtext("Test", outer = TRUE, cex = 1.5)
>
> here:
>
>  https://stat.ethz.ch/pipermail/r-help/2008-July/168163.html
>
> but it doesn't works for me.
>
> Could anyone please show me some pointers what should I read in order to get
> an insight why this isn't working as I expect?

That part's easy: lattice is drawing using grid graphics, and mtext is
drawing using "traditional" graphics, and the two don't (easily) mix.

You will need to delve into grid a little bit for what you want. Do
you have a good reason to have separate levelplots? One of the main
points of lattice is to avoid such things.

-Deepayan

> What I managed to find a workaround by using panel.text(), but I don't
> really like it since it requires defined x/y coordinates and not scales if
> the picture is resized.
>
>        panel.text(x=20, y=110, "Test")
>
> Thanks in advance!
> Richard
>
> __
> R-help@r-project.org mailing list
> 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@r-project.org mailing list
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.


Re: [R] Superposing mean line to xyplot

2011-10-15 Thread Deepayan Sarkar
2011/10/10 Niccolò Bassani :
> Dear R-users,
> I'm using lattice package and function xyplot for the first time so
> you will excuse me for my inexperience. I'm facing quite a simple
> problem but I'm having troubles on how to solve it, I've read tons of
> old mails in the archives and looked at some slides from Deepayan
> Sarkar but still can not get the point.
>
> This is the context. I've got data on 9 microRNAs, each miRNA has been
> measured on three different arrays and on each array I have 4
> replicates for each miRNA, which sums up to a total of 108
> measurements. I've the suspect that measurement on the first array are
> systematically lower than the others so I wanted to draw some line
> plot where each panel correspond to a miRNA, and each line correspond
> to one of the four replicates (that is: first replicate of miRNA A on
> array 1 must be connected to first replicate of miRNA A on array 2 and
> so on), so that for each panel there are 4 series of three points
> connected by a line/segment. I've done this easily with lattice doing
> this:
>
> array = rep(c("A","B","C"),each = 36) # array replicate
> spot =  rep(1:4,27) # miRNA replicate on each array
> miRNA = rep(rep(paste("miRNA",1:9,sep="."),each=4),3) # miRNA label
> exprs = rnorm(mean=2.8,n = 108) # intensity
> data = data.frame(miRNA,array,spot,exprs)
> xyplot(exprs ~ array|miRNA,data=data,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")))
>
> Now, I want to superpose to each panel an other series of three points
> connected by a line, where each point represent the mean of the four
> replicates of the miRNA on each array, a sort of mean line. I've tried
> using the following, but it's not working as expected:
>
> xyplot(exprs ~ array|miRNA,data=array,type="b",groups=spot,xlab="Array",ylab
> = "Intensity",col="black",lty=2:5,scales = list(y = list(relation =
> "free")), panel = function(x,y,groups,subscripts){
>        panel.xyplot(x,y,groups=groups,subscripts=subscripts)
>        
> panel.superpose(x,y,panel.groups=panel.average,groups=groups,subscripts=subscripts)
> })

Is this close to what you are looking for?

xyplot(exprs ~ array | miRNA, data = data, col="black",lty=2:5,
   type="b", groups=spot, xlab="Array",
   ylab = "Intensity",
   scales = list(y = list(relation = "free")),
   panel = function(x, y, groups, subscripts, ...){
   panel.xyplot(x, y, groups=groups, subscripts=subscripts, ...)
   panel.average(x, y, col = "grey", lwd = 2, horizontal = FALSE)
   })

-Deepayan

__
R-help@r-project.org mailing list
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.


Re: [R] "pos" in panel.text

2011-10-15 Thread Deepayan Sarkar
On Mon, Oct 10, 2011 at 6:31 PM, Allan Sikk  wrote:
> Here's the code. The problem seems to be specific for lattice as I can
> easily use a vector with pos in "plot".

lattice::panel.text() does not support vector 'pos'. (Not very
difficult to fix, and I'll put it on my TODO list). For now, you will
need to use separate calls for different 'pos' values.

-Deepayan

> trellis.device(,width=600, height = 400)
> xyplot(Npop~Narea,
> scales=list(x=list(log=TRUE, at=my.at,labels = formatC(my.at, big.mark =
> ",", format="d")),
> y=list(log=TRUE, at=c(1,10,100,1000,1,10,100))),
> panel=function(...) {
>     panel.xyplot(..., type="p", col="black", cex=.5, pch=20)
>     panel.text(x=log10(Narea), y=log10(Npop), lab=t,  cex=.5, pos=c(4,2))
>         }
>
> )

__
R-help@r-project.org mailing list
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.


  1   2   3   4   5   6   7   8   >