Hi all How can I change the limits (xlim or ylim) in a plot that has been already created? For example, consider this naive example curve(dbeta(x,2,4)) curve(dbeta(x,8,13),add=T,col=2) When adding the second curve, it goes off the original limits computed by R for the first graph, which are roughly, c(0,2.1) I know two obvious solutions for this, which are: 1) passing a sufficiently large parameter e.g. ylim=c(0,4) to the first graphic curve(dbeta(x,2,4),ylim=c(0,4)) curve(dbeta(x,8,13),add=T,col=2) or 2) switch the order in which I plot the curves curve(dbeta(x,8,13),col=2) curve(dbeta(x,2,4),add=T) but I guess if there is any way of adjusting the limits of the graphic "a posteriori", once you have a plot with the undesired limits, forcing R to redraw it with the new limits, but without having to execute again the "curve" commands Hope I made myself clear Best regards and thank you very much in advance -- View this message in context: http://r.789695.n4.nabble.com/Change-the-limits-of-a-plot-a-posteriori-tp4129750p4129750.html Sent from the R help mailing list archive at Nabble.com.
jcano wrote on 12/01/2011 12:12:03 PM:> Hi all > > How can I change the limits (xlim or ylim) in a plot that has beenalready> created? > > For example, consider this naive example > curve(dbeta(x,2,4)) > curve(dbeta(x,8,13),add=T,col=2) > > When adding the second curve, it goes off the original limits computedby R> for the first graph, which are roughly, c(0,2.1) > > I know two obvious solutions for this, which are: > 1) passing a sufficiently large parameter e.g. ylim=c(0,4) to the first > graphic > curve(dbeta(x,2,4),ylim=c(0,4)) > curve(dbeta(x,8,13),add=T,col=2) > > or > > 2) switch the order in which I plot the curves > curve(dbeta(x,8,13),col=2) > curve(dbeta(x,2,4),add=T) > > but I guess if there is any way of adjusting the limits of the graphic"a> posteriori", once you have a plot with the undesired limits, forcing Rto> redraw it with the new limits, but without having to execute again the > "curve" commands > > Hope I made myself clear > > Best regards and thank you very much in advanceThere is no way to adjust the limits of the plot "a posteriori". You could set it up so that the limits of the plot are determined directly from the data you are plotting. x <- seq(0, 1, 0.01) y.range <- range(dbeta(x, 2, 4), dbeta(x, 8, 13)) curve(dbeta(x, 2, 4), ylim=y.range) curve(dbeta(x, 8, 13), ylim=y.range, add=T, col=2) Jean [[alternative HTML version deleted]]
On 01/12/2011 1:12 PM, jcano wrote:> Hi all > > How can I change the limits (xlim or ylim) in a plot that has been already > created?You can't, if you're using classic R graphics. They use an "ink on paper" model of graphics. If you want to change what you've drawn, you get a new piece of paper. Your two solutions below are the usual methods to work around this limitation. The first is the easiest method in general, though it's not particularly easy if you're using curve(). Generally if you're planning to plot y1 vs x1 and y2 vs x2, you can set your ylim to range(c(y1, y2)) and your xlim to range(c(x1, x2)) and things are fine. If you want to follow this strategy with curve(), you need to call it twice for each curve: once to find the range of points it would plot (they are in the return value of the function), and a second time to redo the plot with the calculated xlim and ylim. Duncan Murdoch> For example, consider this naive example > curve(dbeta(x,2,4)) > curve(dbeta(x,8,13),add=T,col=2) > > When adding the second curve, it goes off the original limits computed by R > for the first graph, which are roughly, c(0,2.1) > > I know two obvious solutions for this, which are: > 1) passing a sufficiently large parameter e.g. ylim=c(0,4) to the first > graphic > curve(dbeta(x,2,4),ylim=c(0,4)) > curve(dbeta(x,8,13),add=T,col=2) > > or > > 2) switch the order in which I plot the curves > curve(dbeta(x,8,13),col=2) > curve(dbeta(x,2,4),add=T) > > but I guess if there is any way of adjusting the limits of the graphic "a > posteriori", once you have a plot with the undesired limits, forcing R to > redraw it with the new limits, but without having to execute again the > "curve" commands > > Hope I made myself clear > > Best regards and thank you very much in advance > > > -- > View this message in context: http://r.789695.n4.nabble.com/Change-the-limits-of-a-plot-a-posteriori-tp4129750p4129750.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 zoomplot function in the TeachingDemos package can be used for this (it actually redoes the entire plot, but with new limits). This will generally work for a quick exploration, but for quality plots it is suggested to create the 1st plot with the correct range to begin with. -- 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 jcano > Sent: Thursday, December 01, 2011 11:12 AM > To: r-help at r-project.org > Subject: [R] Change the limits of a plot "a posteriori" > > Hi all > > How can I change the limits (xlim or ylim) in a plot that has been > already > created? > > For example, consider this naive example > curve(dbeta(x,2,4)) > curve(dbeta(x,8,13),add=T,col=2) > > When adding the second curve, it goes off the original limits computed > by R > for the first graph, which are roughly, c(0,2.1) > > I know two obvious solutions for this, which are: > 1) passing a sufficiently large parameter e.g. ylim=c(0,4) to the first > graphic > curve(dbeta(x,2,4),ylim=c(0,4)) > curve(dbeta(x,8,13),add=T,col=2) > > or > > 2) switch the order in which I plot the curves > curve(dbeta(x,8,13),col=2) > curve(dbeta(x,2,4),add=T) > > but I guess if there is any way of adjusting the limits of the graphic > "a > posteriori", once you have a plot with the undesired limits, forcing R > to > redraw it with the new limits, but without having to execute again the > "curve" commands > > Hope I made myself clear > > Best regards and thank you very much in advance > > > -- > View this message in context: http://r.789695.n4.nabble.com/Change-the- > limits-of-a-plot-a-posteriori-tp4129750p4129750.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.