An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110503/b561c15f/attachment.pl>
Controlling the extent of ablines on plot
7 messages · Jerome Asselin, Ryan Utz, Greg Snow +2 more
On Tue, 2011-05-03 at 10:36 -0600, Ryan Utz wrote:
Hi all, I'm attempting to make a quite-specific plot where the axes cross at the origin and with gridlines for guidance. I've been using ablines to create the reference lines because I want a lot of control as to where they are placed on the axis. This command works very well for such control. However... These ablines don't seem to work when I specify the origin as 0,0. They go beyond the x-axis at both ends, rendering a quite ugly graph (and they push back the y-axis title some). Behold: ### x<-c(0,1,2,3,4,5) y<-c(0,2,4,6,8,10) plot(x,y, axes=FALSE) axis(1,at=c(0,1,2,2.5,3,4,5),pos=0) axis(2,at=c(0,2,4,6,8,10),pos=0) abline(h=c(1,2,3,4,5)) ### Is there any way for me to specify that these ablines should not go beyond the y-axis extent? I just want a pretty graph! Thanks! Ryan
Maybe you could use lines()? plot(1:2) lines(c(1,2),c(1.5,1.5))
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110503/f8113c08/attachment.pl>
Check your par() settings, specifically "xpd". For more control see ?clip. If that does not do enough for you then use lines or segments for complete control.
Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111 > -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Ryan Utz > Sent: Tuesday, May 03, 2011 10:36 AM > To: r-help at r-project.org > Subject: [R] Controlling the extent of ablines on plot > > Hi all, > > I'm attempting to make a quite-specific plot where the axes cross at > the > origin and with gridlines for guidance. I've been using ablines to > create > the reference lines because I want a lot of control as to where they > are > placed on the axis. This command works very well for such control. > However... > > These ablines don't seem to work when I specify the origin as 0,0. They > go > beyond the x-axis at both ends, rendering a quite ugly graph (and they > push > back the y-axis title some). > > Behold: > > ### > x<-c(0,1,2,3,4,5) > y<-c(0,2,4,6,8,10) > > plot(x,y, axes=FALSE) > axis(1,at=c(0,1,2,2.5,3,4,5),pos=0) > axis(2,at=c(0,2,4,6,8,10),pos=0) > abline(h=c(1,2,3,4,5)) > ### > > Is there any way for me to specify that these ablines should not go > beyond > the y-axis extent? I just want a pretty graph! > > Thanks! > Ryan > > -- > > Ryan Utz, Ph.D. > Aquatic Ecologist/STREON Scientist > National Ecological Observatory Network > > Home/Cell: (724) 272-7769 > Work: (720) 746-4844 ext. 2488 > > [[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.
Hi Ryan, The issue is the plotting region is slightly padded. The easiest option, I think, would be to clip() it. I have a general sense that one of the par() options would let you adjust the padding to 0, but I could just be imagining that (anyone else??). Anyway, here are some options: ### plot(0:5, seq(0, 10, 2), axes=FALSE) axis(1, at=c(0,1,2,2.5,3,4,5), pos=0) axis(2, at=seq(0, 10, 2), pos=0) ## using clip and abline clip(0, 5, 0, 10) abline(h = 1:5) dev.new() plot(0:5, seq(0, 10, 2), axes=FALSE) axis(1, at=c(0,1,2,2.5,3,4,5), pos=0) axis(2, at=seq(0, 10, 2), pos=0) ## using lines, but without retyping as much sapply(1:5, function(y) lines(c(0, 5), c(y, y))) ## or even easier, getting the same thing with segments segments(0, 1:5, 5, 1:5) HTH, Josh
On Tue, May 3, 2011 at 10:26 AM, Ryan Utz <utz.ryan at gmail.com> wrote:
Well... that could work. Problem is in the actual graphs I'm making, there are to be >30 lines per graph (as many as 60 in some cases). Any way I could use the lines command without having to write out 60 lines of code per figure? That's why I like ablines; you just have to specify a single value and it will put a horizontal line at that number. Thanks, Ryan
Maybe you could use lines()? plot(1:2) lines(c(1,2),c(1.5,1.5))
-- Ryan Utz, Ph.D. Aquatic Ecologist/STREON Scientist National Ecological Observatory Network Home/Cell: (724) 272-7769 Work: (720) 746-4844 ext. 2488 ? ? ? ?[[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.
Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
On Tue, May 3, 2011 at 10:40 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
The issue is the plotting region is slightly padded. ?The easiest option, I think, would be to clip() it. ?I have a general sense that one of the par() options would let you adjust the padding to 0, but I could just be imagining that (anyone else??).
Not loving it, but this is sort of what I meant... plot(0:5, seq(0, 10, 2), axes=FALSE, xaxs = "i", yaxs = "i") axis(1, at=c(0,1,2,2.5,3,4,5), pos=0) axis(2, at=seq(0, 10, 2), pos=0) abline(h = 1:5)
On 03/05/2011 1:26 PM, Ryan Utz wrote:
Well... that could work. Problem is in the actual graphs I'm making, there are to be>30 lines per graph (as many as 60 in some cases). Any way I could use the lines command without having to write out 60 lines of code per figure? That's why I like ablines; you just have to specify a single value and it will put a horizontal line at that number. Thanks, Ryan
Write your own abline. For example, with your previously posted example:
xlim <- c(0, 5)
ylim <- c(0, 10)
abline2 <- function(h, v) {
if (!missing(h)) {
n <- length(h)
segments( rep(xlim[1], n), h, rep(xlim[2], n), h)
}
if (!missing(v)) {
n <- length(v)
segments( rep(ylim[1], n), v, rep(ylim[2], n), v)
}
}
Just replace your abline() call with abline2().
Duncan Murdoch