Keith Ponting
2008-Oct-08 14:15 UTC
[R] histogram loses top row with alpha transparency under Windows
Hello all. Trying to use transparency for overlaid "histogram" plots I have come across an interesting inconsistency, possibly a bug when running under Windows. Originally noticed in R 2.7.1, it is still there in 2.8.0 beta. library(lattice) zz <- function(n,alpha) { ranges <- NULL for(ds in 1:n){ ranges <- rbind(ranges,data.frame(confidence=c(0,100),dataset=as.character(ds),cor rect=c(FALSE,TRUE))) } panel.colhist = function(x, group.number, col, ...) { panel.histogram(x, col=group.number+1, ...) } x <- histogram(~confidence|dataset,data=ranges,alpha=alpha, panel=panel.superpose,panel.groups=panel.colhist,groups=correct) print(x) } zz(12,1) # works as expected, 12 identical plots zz(12,0.5) # top row of plots has no bars at all, lower rows are as expected zz(1,1) # two bars fine zz(1,0.5) # no bars at all (I also find the default breaks slightly counter-intuitive here, as the number of breaks seems to depend on the total size of the data, rather than on the size within each plot - the bars meet in the zz(1,1) case but are well separated in the zz(12,1) case.) I am running on 64-bit Windows Vista Business SP1, session info is as follows: R version 2.8.0 beta (2008-10-07 r46631) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lattice_0.17-15 loaded via a namespace (and not attached): [1] grid_2.8.0 It appears to work correctly under (Suse) linux, with the following session characteristics: R version 2.7.2 (2008-08-25) i686-pc-linux-gnu locale: LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.U TF-8;LC_MONETARY=C;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAMEC;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATI ON=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lattice_0.17-13 loaded via a namespace (and not attached): [1] grid_2.7.2 sysname release "Linux" "2.6.22.18-0.2-default" version nodename "#1 SMP 2008-06-09 13:53:20 +0200" "node76" machine login "i686" "kponting" user "kponting" Keith Ponting Aurix Ltd, Malvern WR14 3SZ UK
Richard Cotton
2008-Oct-09 12:09 UTC
[R] histogram loses top row with alpha transparency under Windows
k.ponting wrote:> > Hello all. > > Trying to use transparency for overlaid "histogram" plots I have come > across an interesting inconsistency, possibly a bug when running under > Windows. Originally noticed in R 2.7.1, it is still there in 2.8.0 beta. > > library(lattice) > zz <- function(n,alpha) > { > ranges <- NULL > for(ds in 1:n){ > ranges <- > rbind(ranges,data.frame(confidence=c(0,100),dataset=as.character(ds),cor > rect=c(FALSE,TRUE))) > } > panel.colhist = function(x, group.number, col, ...) { > panel.histogram(x, col=group.number+1, ...) > } > x <- histogram(~confidence|dataset,data=ranges,alpha=alpha, > > panel=panel.superpose,panel.groups=panel.colhist,groups=correct) > print(x) > } > zz(12,1) # works as expected, 12 identical plots > zz(12,0.5) # top row of plots has no bars at all, lower rows are as > expected > zz(1,1) # two bars fine > zz(1,0.5) # no bars at all >The rectangles being drawn extend higher than the top of the panel. (Your y axis ranges from 0 to 50, but the bars go up to 100.) In the top row of plots, depending upon the shape of your device window, the bars can extend beyond the range of the device window. For some reason, (take a look in panel.rect), when you specify alpha less than 1, this prevents the bar being drawn. You need to add type="count" to the call to histogram, or rescale the bar heights. ----- Regards, Richie. Mathematical Sciences Unit HSL -- View this message in context: http://www.nabble.com/histogram-loses-top-row-with-alpha-transparency-under-Windows-tp19879687p19897501.html Sent from the R help mailing list archive at Nabble.com.
Keith Ponting
2008-Oct-10 11:17 UTC
[R] histogram loses top row with alpha transparency under Windows
Richard.Cotton wrote:> The rectangles being drawn extend higher than the top of the panel.(Your y> axis ranges from 0 to 50, but the bars go up to 100.) >Thankyou - I can also make the bars on the lower panels vanish by tinkering with ylim.> In the top row of plots, depending upon the shape of your devicewindow, the> bars can extend beyond the range of the device window. For somereason,> (take a look in panel.rect), when you specify alpha less than 1, this > prevents the bar being drawn.The problem lies deeper. I can demonstrate the same effect with raw grid calls (but cannot find a way into "grid.draw" to see what is going on): cols <- rainbow(2,alpha=0.5) library(grid) grid.newpage() pushViewport(plotViewport(c(5,4,2,2))) # draw viewport limits: grid.rect() # VISIBLE: small projection outside viewport, transparent fill and edge grid.rect(gp=gpar(fill=cols[2],col=cols[1]),height=unit(1.1,"npc"),width =unit(0.18,"npc"),x=0.1) # VISIBLE: small projection outside viewport, standard colours fill and edge grid.rect(gp=gpar(fill=2,col=3),height=unit(1.1,"npc"),width=unit(0.18," npc"),x=0.3) # INVISIBLE: larger projection outside viewport, transparent colours fill and edge grid.rect(gp=gpar(fill=cols[2],col=cols[1]),height=unit(1.5,"npc"),width =unit(0.18,"npc"),x=0.5) # VISIBLE: larger projection outside viewport, standard colours fill and edge grid.rect(gp=gpar(fill=2,col=3),height=unit(1.5,"npc"),width=unit(0.18," npc"),x=0.7) # MIXED: larger projection outside viewport, standard colour edge visible, transparent fill not grid.rect(gp=gpar(fill=cols[2],col=3),height=unit(1.5,"npc"),width=unit( 0.18,"npc"),x=0.9) I can also show the same effect using standard graphics calls: par(xpd=NA) plot(c(1,2)) rect(1.01,1,1.19,2.2,border=cols[1],col=cols[2]) rect(1.21,1,1.39,2.2,border=3,col=2) rect(1.41,1,1.59,4,border=cols[1],col=cols[2]) rect(1.61,1,1.79,4,border=3,col=2) rect(1.81,1,1.99,4,border=3,col=cols[2]) Both of these examples have missing alpha transparency colours under Windows but produce the expected filled rectangles under Linux.> > You need to add type="count" to the call to histogram, or rescale thebar> heights.My post was a rather simplified example - I am actually trying to make visible a "small" distribution on the skirt of a large one, which means that scaling the bar heights is not sufficient. I have worked around the problem by modifying panel.histogram to _clip_ the bar heights. Keith Keith Ponting Aurix Ltd, Malvern WR14 3SZ UK
Prof Brian Ripley
2008-Oct-14 11:09 UTC
[R] histogram loses top row with alpha transparency under Windows
I *presume* this is the windows() device in a current version of R, but you failed to tell us almost any of the information requested in the posting guide. If so, you are seeing how Windows GDI copying operations work: if the region to be copied goes off-window, they clip the region completely. Your example works in R-devel, and if that survives a few weeks of testing, I might port the workaround to 2.8.1. (A similar fix in 2.7.1 patched ended up with undesirable side effects in 2.7.2, so careful testing is needed.) Implementing semi-transparent colours on windows() was hard, and it would be nice to see some appreciation of the effort involved. On Fri, 10 Oct 2008, Keith Ponting wrote:> Richard.Cotton wrote: > >> The rectangles being drawn extend higher than the top of the panel. > (Your y >> axis ranges from 0 to 50, but the bars go up to 100.) >> > > Thankyou - I can also make the bars on the lower panels vanish by > tinkering with ylim. > >> In the top row of plots, depending upon the shape of your device > window, the >> bars can extend beyond the range of the device window. For some > reason, >> (take a look in panel.rect), when you specify alpha less than 1, this >> prevents the bar being drawn. > > The problem lies deeper. I can demonstrate the same effect with raw grid > calls (but cannot find a way into "grid.draw" to see what is going on): > > cols <- rainbow(2,alpha=0.5) > library(grid) > grid.newpage() > pushViewport(plotViewport(c(5,4,2,2))) > # draw viewport limits: > grid.rect() > # VISIBLE: small projection outside viewport, transparent fill and edge > grid.rect(gp=gpar(fill=cols[2],col=cols[1]),height=unit(1.1,"npc"),width > =unit(0.18,"npc"),x=0.1) > # VISIBLE: small projection outside viewport, standard colours fill and > edge > grid.rect(gp=gpar(fill=2,col=3),height=unit(1.1,"npc"),width=unit(0.18," > npc"),x=0.3) > # INVISIBLE: larger projection outside viewport, transparent colours > fill and edge > grid.rect(gp=gpar(fill=cols[2],col=cols[1]),height=unit(1.5,"npc"),width > =unit(0.18,"npc"),x=0.5) > # VISIBLE: larger projection outside viewport, standard colours fill and > edge > grid.rect(gp=gpar(fill=2,col=3),height=unit(1.5,"npc"),width=unit(0.18," > npc"),x=0.7) > # MIXED: larger projection outside viewport, standard colour edge > visible, transparent fill not > grid.rect(gp=gpar(fill=cols[2],col=3),height=unit(1.5,"npc"),width=unit( > 0.18,"npc"),x=0.9) > > I can also show the same effect using standard graphics calls: > > par(xpd=NA) > plot(c(1,2)) > rect(1.01,1,1.19,2.2,border=cols[1],col=cols[2]) > rect(1.21,1,1.39,2.2,border=3,col=2) > rect(1.41,1,1.59,4,border=cols[1],col=cols[2]) > rect(1.61,1,1.79,4,border=3,col=2) > rect(1.81,1,1.99,4,border=3,col=cols[2]) > > Both of these examples have missing alpha transparency colours under > Windows but produce the expected filled rectangles under Linux. > >> >> You need to add type="count" to the call to histogram, or rescale the > bar >> heights. > > My post was a rather simplified example - I am actually trying to make > visible a "small" distribution on the skirt of a large one, which means > that scaling the bar heights is not sufficient. I have worked around the > problem by modifying panel.histogram to _clip_ the bar heights. > > Keith > > Keith Ponting > Aurix Ltd, Malvern WR14 3SZ UK > > ______________________________________________ > R-help at 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595