Am just feeling my way into the grid library, and cannot figure out how to define the plot limits. 3/5 of the example polygons below plot in the default 0-1 range viewport. But when I try to redefine the viewport the polygons plot in the same places. I also get the same result without employing push/pop. (As you can see from the scale I'm trying to introduce, I want to plot map polygons.) Grateful for an idea where I'm going wrong. pushViewport(viewport(xscale=c(-180,180),yscale=c(-90,90))) grid.polygon(x=c(0,5,5,0)/100, y=c(0,0,5,5)/100, draw = T) grid.polygon(x=c(100,95,95,100)/100, y=c(100,100,95,95)/100, draw = T) grid.polygon(x=c(50,70,70,50)/100, y=c(50,50,70,70)/100, draw = T) grid.polygon(x=-c(10,10,40,40)/100, y=c(10,40,40,10)/100, draw = T) #NEGATIVE X grid.polygon(x=c(50,52,52,50)/100, y=-c(50,50,52,52)/100, draw = T) #NEGATIVE Y popViewport() Thanks in advance. -- View this message in context: http://r.789695.n4.nabble.com/Defining-a-viewport-scale-in-Grid-tp4393974p4393974.html Sent from the R help mailing list archive at Nabble.com.
Read the Details section in ?viewport carefully. You are treating
xscale/yscale as if they are xlim/ylim in base graphics. They are not.
It may take some trial and error on your part to figure out how
exactly this works, in general you are setting the size and location
of each polygon relative to the parent viewport (in this case the
plotting region), so you need to make the sizes smaller. Here are a
couple of examples:
pushViewport(viewport(width=unit(.5,'npc'),height=unit(.5,'npc'),just=c('left','bottom')))
# ... draw your 5 polygons
dev.off()
pushViewport(viewport(width=unit(.5,'npc'),height=unit(.5,'npc'),just='bottom',yscale=c(-1,1)))
# ... draw your 5 polygons
Hope this helps
On Thu, Feb 16, 2012 at 6:47 AM, geotheory <geotheory1 at gmail.com>
wrote:> Am just feeling my way into the grid library, and cannot figure out how to
> define the plot limits. ?3/5 of the example polygons below plot in the
> default 0-1 range viewport. ?But when I try to redefine the viewport the
> polygons plot in the same places. ?I also get the same result without
> employing push/pop. ?(As you can see from the scale I'm trying to
introduce,
> I want to plot map polygons.) ?Grateful for an idea where I'm going
wrong.
>
> pushViewport(viewport(xscale=c(-180,180),yscale=c(-90,90)))
>
> grid.polygon(x=c(0,5,5,0)/100, y=c(0,0,5,5)/100, draw = T)
> grid.polygon(x=c(100,95,95,100)/100, y=c(100,100,95,95)/100, draw = T)
> grid.polygon(x=c(50,70,70,50)/100, y=c(50,50,70,70)/100, draw = T)
> grid.polygon(x=-c(10,10,40,40)/100, y=c(10,40,40,10)/100, draw = T)
> #NEGATIVE X
> grid.polygon(x=c(50,52,52,50)/100, y=-c(50,50,52,52)/100, draw = T)
> #NEGATIVE Y
>
> popViewport()
>
> Thanks in advance.
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Defining-a-viewport-scale-in-Grid-tp4393974p4393974.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
The default units of polygonGrob are "npc", I think you want "native" instead. Try the following, library(grid) d = data.frame(x=rnorm(100, 10), y=rnorm(100, -100)) v = dataViewport(xData=d$x, yData=d$y) grid.points(d$x,d$y, default.units="native", vp=v) HTH, b. On 17 February 2012 02:47, geotheory <geotheory1 at gmail.com> wrote:> Am just feeling my way into the grid library, and cannot figure out how to > define the plot limits. ?3/5 of the example polygons below plot in the > default 0-1 range viewport. ?But when I try to redefine the viewport the > polygons plot in the same places. ?I also get the same result without > employing push/pop. ?(As you can see from the scale I'm trying to introduce, > I want to plot map polygons.) ?Grateful for an idea where I'm going wrong. > > pushViewport(viewport(xscale=c(-180,180),yscale=c(-90,90))) > > grid.polygon(x=c(0,5,5,0)/100, y=c(0,0,5,5)/100, draw = T) > grid.polygon(x=c(100,95,95,100)/100, y=c(100,100,95,95)/100, draw = T) > grid.polygon(x=c(50,70,70,50)/100, y=c(50,50,70,70)/100, draw = T) > grid.polygon(x=-c(10,10,40,40)/100, y=c(10,40,40,10)/100, draw = T) > #NEGATIVE X > grid.polygon(x=c(50,52,52,50)/100, y=-c(50,50,52,52)/100, draw = T) > #NEGATIVE Y > > popViewport() > > Thanks in advance. > > -- > View this message in context: http://r.789695.n4.nabble.com/Defining-a-viewport-scale-in-Grid-tp4393974p4393974.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.