Reputation: 2970
I have two columns of data, f.delta
and g.delta
that I would like to produce a scatter plot of in R.
Here is how I am doing it.
plot(f.delta~x, pch=20, col="blue")
points(g.delta~x, pch=20, col="red")
The problem is this: the values of f.delta
vary from 0 to -7; the values of g.delta
vary from 0 to 10.
When the plot is drawn, the y axis extends from 1 to -7. So while all the f.delta
points are visible, any g.delta
point that has y>1 is cut-off from view.
How do I stop R from automatically setting the ylims from the data values. Have tried, unsuccessfully, various combinations of yaxt
, yaxp
, ylims
.
Any suggestion will be greatly appreciated.
Thanks, Anjan
Upvotes: 3
Views: 12592
Reputation: 173547
In addition to Gavin's excellent answer, I also thought I'd mention that another common idiom in these cases is to create an empty plot with the correct limits and then to fill it in using points
, lines
, etc.
Using Gavin's example data:
with(df,plot(range(x),range(f.delta,g.delta),type = "n"))
points(f.delta~x, data = df, pch=20, col="blue")
points(g.delta~x, data = df, pch=20, col="red")
The type = "n"
causes plot
to create only the empty plotting window, based on the range of x and y values we've supplied. Then we use points
for both columns on this existing plot.
Upvotes: 5
Reputation: 174778
You need to tell R what the limits of the data are and pass that as argument ylim
to plot()
(note the argument is ylim
not ylims
!). Here is an example:
set.seed(1)
df <- data.frame(f.delta = runif(10, min = -7, max = 0),
g.delta = runif(10, min = 0, max = 10),
x = rnorm(10))
ylim <- with(df, range(f.delta, g.delta)) ## compute y axis limits
plot(f.delta ~ x, data = df, pch = 20, col = "blue", ylim = ylim)
points(g.delta ~ x, data = df, pch = 20, col = "red")
Which produces
Upvotes: 3