Durant, James T. (ATSDR/DCHI/SSB)
2012-Oct-18 17:49 UTC
[R] svyplot and svysmooth with hexbin
Hi all- So sorry to bother you all with something pretty basic. I am trying to add the lines method output from svysmooth to a svyplot with style="grayhex". However, the line either appears in the wrong place or if I am running in R Studio it causes the system to crash. I know this is something to do with Lattice graphics, but for the life of me I can not figure out how. Dr. Lumley in his excellent book on page 118 mentions that there is code on his website to do this, but I can not find it. So for example: library(survey) data(api) dclus2<-svydesign(id=~dnum+snum, fpc=~fpc1+fpc2, data=apiclus2) svyplot(api00~api99, dclus2) s1 <-svysmooth(api00~api99, dclus2) lines(s1) #works svyplot(api00~api99, dclus2, style="grayhex") lines(s1) #does not work (line either appears in the wrong position in RGui or crashes RStudio). VR James James T. Durant, MSPH CIH Environmental Health Scientist US Agency for Toxic Substances and Disease Registry Atlanta, GA 30341 770-488-0668 [[alternative HTML version deleted]]
try adding legend = 0 to your svyplot() On Thu, Oct 18, 2012 at 1:49 PM, Durant, James T. (ATSDR/DCHI/SSB) < hzd3@cdc.gov> wrote:> Hi all- > > So sorry to bother you all with something pretty basic. > > I am trying to add the lines method output from svysmooth to a svyplot > with style="grayhex". However, the line either appears in the wrong place > or if I am running in R Studio it causes the system to crash. > > I know this is something to do with Lattice graphics, but for the life of > me I can not figure out how. Dr. Lumley in his excellent book on page 118 > mentions that there is code on his website to do this, but I can not find > it. > > > > So for example: > > library(survey) > > data(api) > dclus2<-svydesign(id=~dnum+snum, fpc=~fpc1+fpc2, data=apiclus2) > svyplot(api00~api99, dclus2) > > s1 <-svysmooth(api00~api99, dclus2) > > lines(s1) > > #works > > svyplot(api00~api99, dclus2, style="grayhex") > lines(s1) > > #does not work (line either appears in the wrong position in RGui or > crashes RStudio). > > > VR > > James > > James T. Durant, MSPH CIH > Environmental Health Scientist > US Agency for Toxic Substances and Disease Registry > Atlanta, GA 30341 > 770-488-0668 > > > > > [[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. >[[alternative HTML version deleted]]
Hi On 19/10/2012 6:49 a.m., Durant, James T. (ATSDR/DCHI/SSB) wrote:> Hi all- > > So sorry to bother you all with something pretty basic. > > I am trying to add the lines method output from svysmooth to a > svyplot with style="grayhex". However, the line either appears in > the wrong place or if I am running in R Studio it causes the system > to crash. > > I know this is something to do with Lattice graphics, but for the > life of me I can not figure out how. Dr. Lumley in his excellent book > on page 118 mentions that there is code on his website to do this, > but I can not find it.There may be a higher-level interface to do this, but here's a nuts-and-bolts solution to do what I think you want ... # Save the result from svyplot() call. temp <- svyplot(api00~api99, dclus2, style="grayhex") # Part of the result is the viewport that svyplot() # used to draw the plot (technically its a hexViewport). # Push that hexViewport so we can draw in the original # plot region and scales. pushHexport(temp$plot.vp) # Use grid.lines() to draw the smoother relative to the # "native" scales in the plot hexViewport. lapply(s1, function(z) { grid.lines(z$x, z$y, default="native") }) Hope that helps Paul> > > So for example: > > library(survey) > > data(api) dclus2<-svydesign(id=~dnum+snum, fpc=~fpc1+fpc2, > data=apiclus2) svyplot(api00~api99, dclus2) > > s1<-svysmooth(api00~api99, dclus2) > > lines(s1) > > #works > > svyplot(api00~api99, dclus2, style="grayhex") lines(s1) > > #does not work (line either appears in the wrong position in RGui or > crashes RStudio). > > > VR > > James > > James T. Durant, MSPH CIH Environmental Health Scientist US Agency > for Toxic Substances and Disease Registry Atlanta, GA 30341 > 770-488-0668 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ 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.-- 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/
Here's an example. The problem is that hexbin uses grid graphics, not the base graphics that plot.svysmooth() uses, so you need to do the plotting yourself with grid. The necessary viewport is returned by svyplot() hexstuff<-svyplot(api00~api99, design=dstrat, style="hex", xlab="1999 API",ylab="2000 API") smoothh<-svysmooth(api00~api99, design=dstrat, method="quantreg",quantile=0.75) smoothl<-svysmooth(api00~api99, design=dstrat, method="quantreg",quantile=0.25) pushHexport(hexstuff$plot.vp) grid.lines(smoothh$api99$x,smoothh$api99$y,default.units="native",gp=gpar(col="purple",lwd=2)) grid.lines(smoothl$api99$x,smoothl$api99$y,default.units="native",gp=gpar(col="purple",lwd=2)) popViewport() -thomas On Fri, Oct 19, 2012 at 6:49 AM, Durant, James T. (ATSDR/DCHI/SSB) <hzd3 at cdc.gov> wrote:> Hi all- > > So sorry to bother you all with something pretty basic. > > I am trying to add the lines method output from svysmooth to a svyplot with style="grayhex". However, the line either appears in the wrong place or if I am running in R Studio it causes the system to crash. > > I know this is something to do with Lattice graphics, but for the life of me I can not figure out how. Dr. Lumley in his excellent book on page 118 mentions that there is code on his website to do this, but I can not find it. > > > > So for example: > > library(survey) > > data(api) > dclus2<-svydesign(id=~dnum+snum, fpc=~fpc1+fpc2, data=apiclus2) > svyplot(api00~api99, dclus2) > > s1 <-svysmooth(api00~api99, dclus2) > > lines(s1) > > #works > > svyplot(api00~api99, dclus2, style="grayhex") > lines(s1) > > #does not work (line either appears in the wrong position in RGui or crashes RStudio). > > > VR > > James > > James T. Durant, MSPH CIH > Environmental Health Scientist > US Agency for Toxic Substances and Disease Registry > Atlanta, GA 30341 > 770-488-0668 > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.-- Thomas Lumley Professor of Biostatistics University of Auckland