Reputation: 1981
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
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)
Upvotes: 4
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