Paul Murrell
2021-Apr-15 01:26 UTC
[R] graphics useRaster check_irregular definition for date or posix
Hi Feeding R-help back in, in case my suggestions might be of use to someone else ... Thanks for the examples - so the main benefit you are looking for is the labelling on the axes (date labels) ? If you are just trying to avoid the annoying white lines, it may just be your PNG/PDF viewer; see ... http://cran.stat.auckland.ac.nz/doc/FAQ/R-FAQ.html#Why-are-there-unwanted-borders Even so, making this work for date values for x/y seems like a useful thing. However, ... Neither your "fix" nor my "fix" actually works for the example that you have provided. It just reveals another problem within image.default(), which is the calculation of "midpoints" ... if (length(x) > 1 && length(x) == nrow(z)) { # midpoints dx <- 0.5*diff(x) x <- c(x[1L] - dx[1L], x[-length(x)] + dx, x[length(x)] + dx[length(x)-1]) } This calculation does NOT produce the desired result for "Date"s (the diff() of the resulting modified 'x' is no longer regular). So this needs a bit more thought - let me know if you come up with a fix for that calculation before me :) Paul On 15/04/21 4:05 am, cdanek at posteo.de wrote:> Hi Paul > > Thanks for your answer. I just answer to you, I dont know if this is > correct (also the maillist?) > > x <- y <- seq(as.Date("2020-1-1"), as.Date("2020-12-31"), l=12) > > z <- array(rnorm(length(x)*length(y)), c(length(x), length(y))) > image(x, y, z, useRaster=FALSE) # ok > > image(x, y, z, useRaster=TRUE) # error: ?useRaster = TRUE? can only be used with a regular grid > > x <- y <- seq_len(12) > > image(x, y, z, useRaster=FALSE) # ok > > image(x, y, z, useRaster=TRUE) # ok > > The difference is the plot quality: useRaster = TRUE yields better quality as it omits strange white lines (in both png and pdf). I did not find a pattern when those white lines appear and when not. Hence, I cannot provide a reproducable example in which you can see the differences induced by the useRaster argument. In simple examples like the one above there is no difference between useRaster true and false plots. > > Do you have a suggestion on how to continue? > > Thanks a lot, > Chris > > Am 14.04.2021 04:37 schrieb Paul Murrell: > >> Hi >> >> I doubt it is intended (to deliberately exclude "difftime" objects). >> >> Can you please supply a full image() example (with 'x' and/or 'y' as Dates and a 'z') ? So that I can see what ... >> >> image(x, y, z, useRaster=FALSE) >> >> ... looks like, so I can see what you want ... >> >> image(x, y, z, useRaster=TRUE) >> >> ... to look like. >> >> I also wonder whether switching to ... >> >> dx[1][rep(1, length(dx))] >> >> ... might be better than switching to ... >> >> as.numeric(dx) >> >> It produces the same result for "difftime" objects, and may have a better chance of working better with other objects (although I confess that not having thought of using a "difftime" for 'x' I am also failing to think of further possibilities for 'x'). >> >> Paul >> >> On 14/04/21 3:11 am,cdanek at posteo.de <mailto:cdanek at posteo.de> wrote: >>> Hi The function `check_irregular()` defined within >>> `graphics::image.default()` checks if the `useRaster` argument for >>> `graphics::image()` can be true or must be false. According to this >>> function, the following example vector is irregular: ``` time <- >>> seq(as.Date("2020-1-1"), as.Date("2020-12-31"), l=12) >>> check_irregular(time, time) # TRUE ``` In my view, this is not >>> correct. In this case, the `all.equal`-call does not evaluate to true >>> due to the special class of `dx` (or `dy`). If I slightly rewrite the >>> function as ``` my_check_irregular <- function (x, y) { dx <- >>> as.numeric(diff(x)) dy <- as.numeric(diff(y)) (length(dx) && >>> !isTRUE(all.equal(dx, rep(dx[1], length(dx))))) || (length(dy) && >>> !isTRUE(all.equal(dy, rep(dy[1], length(dy))))) } ``` the correct >>> answer is obtained (i.e. that the input vector is not irregular based >>> on the rational behind `all.equal`): ``` my_check_irregular(time, >>> time) # FALSE ``` The same applies to POSIX* objects. I was wondering >>> if this is intended or not? Thanks a lot for any answer, Chris >>> [[alternative HTML version deleted]] >>> ______________________________________________ R-help at r-project.org >>> <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and >>> more, see https://stat.ethz.ch/mailman/listinfo/r-help >>> <https://stat.ethz.ch/mailman/listinfo/r-help> >>> <https://stat.ethz.ch/mailman/listinfo/r-help >>> <https://stat.ethz.ch/mailman/listinfo/r-help>> >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> <http://www.R-project.org/posting-guide.html> >>> <http://www.R-project.org/posting-guide.html >>> <http://www.R-project.org/posting-guide.html>> >>> and provide commented, minimal, self-contained, reproducible code.-- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/
cd@@ek m@iii@g oii posteo@de
2021-Apr-15 07:27 UTC
[R] graphics useRaster check_irregular definition for date or posix
Hi Paul Thanks for the FAQs related to the unwanted white lines. However, even if it is just the png/pdf viewer that causes the white lines, I would like to try to avoid them as much as possible, and setting useRaster to true does that. My wish would be that I can use a date or POSIX* vector as x and/or y axis as it is without using some numerical values instead and then always need to take special care for the corresponding labels. Particularly for date/POSIX* objects this can be annoying. So the axes or their labels are not my problem, I just need the useRaster true or false decision to be consistent. How do you deduce that the diff of the midpoints (i.e. the modifided x) is not "regular"? This, again, will be decided later in image.default() by check_irregular(), what brings us back to my initial post? Cheers, Chris Am 15.04.2021 03:26 schrieb Paul Murrell:> Hi > > Feeding R-help back in, in case my suggestions might be of use to someone else ... > > Thanks for the examples - so the main benefit you are looking for is the labelling on the axes (date labels) ? > > If you are just trying to avoid the annoying white lines, it may just be your PNG/PDF viewer; see ... > > http://cran.stat.auckland.ac.nz/doc/FAQ/R-FAQ.html#Why-are-there-unwanted-borders [1] > > Even so, making this work for date values for x/y seems like a useful thing. However, ... > > Neither your "fix" nor my "fix" actually works for the example that you have provided. It just reveals another problem within image.default(), which is the calculation of "midpoints" ... > > if (length(x) > 1 && length(x) == nrow(z)) { # midpoints > dx <- 0.5*diff(x) > x <- c(x[1L] - dx[1L], x[-length(x)] + dx, > x[length(x)] + dx[length(x)-1]) > } > > This calculation does NOT produce the desired result for "Date"s (the diff() of the resulting modified 'x' is no longer regular). > > So this needs a bit more thought - let me know if you come up with a fix for that calculation before me :) > > Paul > > On 15/04/21 4:05 am, cdanek at posteo.de wrote: > Hi Paul Thanks for your answer. I just answer to you, I dont know if this is correct (also the maillist?) x <- y <- seq(as.Date("2020-1-1"), as.Date("2020-12-31"), l=12) z <- array(rnorm(length(x)*length(y)), c(length(x), length(y))) image(x, y, z, useRaster=FALSE) # ok image(x, y, z, useRaster=TRUE) # error: 'useRaster = TRUE' can only be used with a regular grid x <- y <- seq_len(12) image(x, y, z, useRaster=FALSE) # ok image(x, y, z, useRaster=TRUE) # ok The difference is the plot quality: useRaster = TRUE yields better quality as it omits strange white lines (in both png and pdf). I did not find a pattern when those white lines appear and when not. Hence, I cannot provide a reproducable example in which you can see the differences induced by the useRaster argument. In simple examples like the one above there is no difference between useRaster true and false plots. Do you have a suggestion on how to continue? Thanks a lot, Chris Am 14.04.2021 04:37 schrieb Paul Murrell: Hi Idoubt it is intended (to deliberately exclude "difftime" objects). Can you please supply a full image() example (with 'x' and/or 'y' as Dates and a 'z') ? So that I can see what ... image(x, y, z, useRaster=FALSE) ... looks like, so I can see what you want ... image(x, y, z, useRaster=TRUE) ... to look like. I also wonder whether switching to ... dx[1][rep(1, length(dx))] ... might be better than switching to ... as.numeric(dx) It produces the same result for "difftime" objects, and may have a better chance of working better with other objects (although I confess that not having thought of using a "difftime" for 'x' I am also failing to think of further possibilities for 'x'). Paul On 14/04/21 3:11 am,cdanek at posteo.de <mailto:cdanek at posteo.de> wrote: Hi The function `check_irregular()` defined within `graphics::image.default()` checks if the `useRaster` argument for `graphics::image()` can be true or must be false. According to this function, the following example vector is irregular: ``` time <- seq(as.Date("2020-1-1"), as.Date("2020-12-31"), l=12) check_irregular(time, time) # TRUE ``` In my view, this is not correct. In this case, the `all.equal`-call does not evaluate to true due to the special class of `dx` (or `dy`). If I slightly rewrite the function as ``` my_check_irregular <- function (x, y) { dx <- as.numeric(diff(x)) dy <- as.numeric(diff(y)) (length(dx) && !isTRUE(all.equal(dx, rep(dx[1], length(dx))))) || (length(dy) && !isTRUE(all.equal(dy, rep(dy[1], length(dy))))) } ``` the correct answer is obtained (i.e. that the input vector is not irregular based on the rational behind `all.equal`): ``` my_check_irregular(time, time) # FALSE ``` The same applies to POSIX* objects. I was wondering if this is intended or not? Thanks a lot for any answer, Chris [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org <mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help [2] <https://stat.ethz.ch/mailman/listinfo/r-help [2]> <https://stat.ethz.ch/mailman/listinfo/r-help [2] <https://stat.ethz.ch/mailman/listinfo/r-help [2]>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html [3] <http://www.R-project.org/posting-guide.html [3]> <http://www.R-project.org/posting-guide.html [3] <http://www.R-project.org/posting-guide.html [3]>> and provide commented, minimal, self-contained, reproducible code. Links: ------ [1] http://cran.stat.auckland.ac.nz/doc/FAQ/R-FAQ.html#Why-are-there-unwanted-borders [2] https://stat.ethz.ch/mailman/listinfo/r-help [3] http://www.R-project.org/posting-guide.html [[alternative HTML version deleted]]