rose
rose

Reputation: 1981

Zoom in the subplot in the existing plot in R

I have the following plot in R. I would like to have a subplot in the existing plot form x =[0, 2] and y=[0, 2], also I want to zoom into that subplot. How can I do this in R?

 lin <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
 linm <- lm(y ~ x, data = lin, subset = 2:4)
 plot(y ~ x, data = lin)
 abline(linm)

Upvotes: 1

Views: 1543

Answers (2)

agstudy
agstudy

Reputation: 121578

You can do this for example:

lin <- data.frame(x = c(0:6), y = c(0.3, 0.1, 0.9, 3.1, 5, 4.9, 6.2))
linm <- lm(y ~ x, data = lin, subset = 2:4)
plot(y ~ x, data = lin)
abline(linm)
## to overlap the 2 plots
par(new=TRUE, oma=c(3,1,1,2))
## create a layout to plot the subplot in the right bottom corner
layout(matrix(1:4,2))
## use xlim and ylim to zoom the subplot
plot(y ~ x, data = lin,xlim=c(0,2), ylim=c(0,2))
abline(linm)

enter image description here

Upvotes: 4

James Travis
James Travis

Reputation: 21

You can change the limits of a plot using the xlim and ylim arguments to the plot function. For example

plot(y~x, data = lin, xlim=c(0,2), ylim=c(0,2))

Upvotes: 2

Related Questions