How to fit all points into plot?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to fit all points into plot?

Martin Lam
Hi,

I have a problem when I want to add new points (or a
new line) to the graph. Some points (or parts of the
line) are not shown on the graph because they lie
beyond the scale of the axis. Is there a way to
overcome this so all points (or the entire line) are
shown on the graph? Here's an example of my problem:

colors = c("red", "blue")

plot(x=rnorm(100,0,1), y=runif(100), type="p", pch=21,
col = colors[1])

# if I add these points not all of them are shown on
the graph
lines(x=rnorm(100,3,1), y=runif(100), type="p",
pch=24, col = colors[2])

Thanks in advance,

Martin

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to fit all points into plot?

Sean Davis



On 12/29/05 5:19 PM, "Martin Lam" <[hidden email]> wrote:

> Hi,
>
> I have a problem when I want to add new points (or a
> new line) to the graph. Some points (or parts of the
> line) are not shown on the graph because they lie
> beyond the scale of the axis. Is there a way to
> overcome this so all points (or the entire line) are
> shown on the graph? Here's an example of my problem:
>
> colors = c("red", "blue")
>
> plot(x=rnorm(100,0,1), y=runif(100), type="p", pch=21,
> col = colors[1])
>
> # if I add these points not all of them are shown on
> the graph
> lines(x=rnorm(100,3,1), y=runif(100), type="p",
> pch=24, col = colors[2])

You will need to use something like max and min for ALL of your x and y
values and then set your own ylim and xlim for the plot.

 xold <- rnorm(100,0,1)
 yold <- runif(100)
 xnew <- rnorm(100,3,1)
 ynew <- runif(100)

 plot(xold,yold,xlim=c(min(c(xold,xnew)),max(c(xold,xnew))),
      ylim=c(min(c(yold,ynew)),max(c(yold,ynew))))

 lines(xold,yold,type='p')

This isn't tested, but I hope you get the idea.

Sean

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to fit all points into plot?

Duncan Murdoch
On 12/29/2005 5:33 PM, Sean Davis wrote:

>
>
> On 12/29/05 5:19 PM, "Martin Lam" <[hidden email]> wrote:
>
>
>>Hi,
>>
>>I have a problem when I want to add new points (or a
>>new line) to the graph. Some points (or parts of the
>>line) are not shown on the graph because they lie
>>beyond the scale of the axis. Is there a way to
>>overcome this so all points (or the entire line) are
>>shown on the graph? Here's an example of my problem:
>>
>>colors = c("red", "blue")
>>
>>plot(x=rnorm(100,0,1), y=runif(100), type="p", pch=21,
>>col = colors[1])
>>
>># if I add these points not all of them are shown on
>>the graph
>>lines(x=rnorm(100,3,1), y=runif(100), type="p",
>>pch=24, col = colors[2])
>
>
> You will need to use something like max and min for ALL of your x and y
> values and then set your own ylim and xlim for the plot.

This is the right idea, but a quicker way is to use the range() function:

>
>  xold <- rnorm(100,0,1)
>  yold <- runif(100)
>  xnew <- rnorm(100,3,1)
>  ynew <- runif(100)
>
>  plot(xold,yold,xlim=c(min(c(xold,xnew)),max(c(xold,xnew))),
                   xlim=range(c(xold,xnew)),

>       ylim=c(min(c(yold,ynew)),max(c(yold,ynew))))
         ylim=range(c(yold,ynew))

>
>  lines(xold,yold,type='p')
>
> This isn't tested, but I hope you get the idea.
>
> Sean
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: How to fit all points into plot?

Patrick Burns
In reply to this post by Martin Lam
Another approach to what has already been suggested
is to use 'matplot' (as in matrix plot).

Patrick Burns
[hidden email]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Martin Lam wrote:

>Hi,
>
>I have a problem when I want to add new points (or a
>new line) to the graph. Some points (or parts of the
>line) are not shown on the graph because they lie
>beyond the scale of the axis. Is there a way to
>overcome this so all points (or the entire line) are
>shown on the graph? Here's an example of my problem:
>
>colors = c("red", "blue")
>
>plot(x=rnorm(100,0,1), y=runif(100), type="p", pch=21,
>col = colors[1])
>
># if I add these points not all of them are shown on
>the graph
>lines(x=rnorm(100,3,1), y=runif(100), type="p",
>pch=24, col = colors[2])
>
>Thanks in advance,
>
>Martin
>
>______________________________________________
>[hidden email] mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>
>
>  
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html