[R-sig-eco] Fixing vertical and horizontal axes in R

Marc Taylor marchtaylor at gmail.com
Thu May 12 08:58:16 CEST 2016


Hi Margaret,

Here is an example of scaling your figure based on the extension of the
axes. The margins are added as additional space beyond that scaling (here
in inches with par(mai=...) ). Having the additional argument asp=1 will
maintain the same aspect ration for both axes:

DIMS <- dim(volcano)
DIMS
fig.width <- 6
fig.height <- fig.width * (DIMS[2]/DIMS[1])
fig.height
MAI <- c(1,1,0.1,0.1) # margins in inches
fig.width <- fig.width + sum(OMI[c(2,4)]) # final figure width
fig.height <- fig.height + sum(OMI[c(1,3)]) # final figure height

jpeg("volcano.jpeg", width = fig.width, height = fig.height, units="in",
res=400)
op <- par(mai=MAI)
image(x=seq(nrow(volcano)), y=seq(ncol(volcano)), z=volcano, asp=1)
abline(v=seq(0, nrow(volcano), by=5), col=8, lty=3, lwd=0.75)
abline(h=seq(0, ncol(volcano), by=5), col=8, lty=3, lwd=0.75)
par(op)
dev.off()

Cheers,
Marc

On Thu, May 12, 2016 at 8:51 AM, Shane Baylis <shane.m.baylis at gmail.com>
wrote:

> Hi Margaret,
>
> eqscplot() can handle this scenario. Can you try:
>
> fn <- "SpeciesRateLevels11F.jpg"
> jpeg(fn, width = 600, height = 700)
> zmeshR <- matrix(zmeshR,ng,ng)
> eqscplot(x = x, y = y, type = "n", xlab = "Easting", ylab = "Northing",
>                main = "Estimated number of native species / .01 ha")
> image(xgrid,ygrid,zmeshR,bty="l",
>        col=zz.colors,
>        add = TRUE)
> points(x,y,col="red",cex=0.7)
> lines(Jboundary[,1],Jboundary[,2],col="black",lwd=2)
>
> b <- contourLines(xgrid,ygrid ,
>               zmeshR, nlevels=11,
>               levels = c(2, 4, 6, 8,10,12,14,16,18,20,22))
> for (i in 1:11){
>      lines(b[[i]], col="black", lwd=1)
> }
>
> In this case the limits of the plot will be set in terms of x and/or y,
> rather than xgrid and ygrid. If you need to include more of the
> background that was generated in your original image() call, you can use
> dummy x and y variables in the eqscplot() call, so that the whole
> image() area falls between their minimum and maximum.
>
> Cheers,
>
>   Shane
>
>
>
> On 12/05/16 15:55, Margaret Donald wrote:
> > Thanks, Shane, but that is precisely my problem. The final graph will
> > not be the default windows size and I want to
> >     a) nominate the overall height & width, and
> >     b) enforce the equal scales on the x and y axes.
> >
> > My code currently looks like this:
> >
> > # Generate image plot 2:
> > #-----------------------
> > fn <- "SpeciesRateLevels11F.jpg"
> > jpeg(fn, width=600, height=700)
> > zmeshR <- matrix(zmeshR,ng,ng)
> > image(xgrid,ygrid,zmeshR,bty="l",xlab="Easting",ylab="Northing",
> >       col=zz.colors,  xlim=c(xx1, xx2), ylim=c(yy1, yy2),
> >       main="Estimated number of native species /.01 ha")
> > points(x,y,col="red",cex=0.7)
> > lines(Jboundary[,1],Jboundary[,2],col="black",lwd=2)
> >
> > b <- contourLines(xgrid,ygrid ,
> >              zmeshR, nlevels=11,
> >              levels = c(2, 4, 6, 8,10,12,14,16,18,20,22))
> > for (i in 1:11){
> >     lines(b[[i]], col="black", lwd=1)
> > }
> >
> > Cheers,
> > Margaret Donald
> >
> >
> > On 12 May 2016 at 14:02, Shane Baylis <shane.m.baylis at gmail.com
> > <mailto:shane.m.baylis at gmail.com>> wrote:
> >
> >     Hi Margaret,
> >
> >     It sounds like you're after eqscplot(), in package MASS. The
> >     overall size can be set as for any plot window, but note that if
> >     you scale the plot by dragging the limits of the plot window (or
> >     similar), the plot will need to be re-generated after scaling the
> >     window to ensure that equal-scale is maintained.
> >
> >
> https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/eqscplot.html
> >
> >     Hope that helps,
> >
> >      Shane Baylis
> >
> >     On 12/05/16 13:13, Margaret Donald wrote:
> >
> >         I have a figure which uses the same measurement units both
> >         vertically and
> >         horizontally. I need the interval representing 20 m to be
> >         identical for
> >         both the x and y axes.  How do I specify this? And at the same
> >         time dictate
> >         the size of the overall graph which is being saved as a jpeg.
> >
> >
> >         Regards,
> >         Margaret Donald
> >
> >
> >
> >
> >     _______________________________________________
> >     R-sig-ecology mailing list
> >     R-sig-ecology at r-project.org <mailto:R-sig-ecology at r-project.org>
> >     https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
> >
> >
> >
> >
> > --
> > Margaret Donald BA (Hon), M. App Stat, Ph.D, AStat
> > Statistician, Anne Clements & Associates
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology
>

	[[alternative HTML version deleted]]



More information about the R-sig-ecology mailing list