wander Hi
wander Hi

Reputation: 137

overlaying a plot without axis title in R (without ggplot2)

I couldn't find the answer to my question anywhere on forum. I have two plots at the moment, one of which is the zoomed in of another plot. For instance, I have the following:

    attach(mtcars)
plot(wt, mpg, main="Scatterplot Example", 
     xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)

plot(wt, mpg, main="Scatterplot Example", 
     xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19, xlim=c(0, 3) )

I have trying to overlay the 2nd plot (with the xlim onto the right upper corner of the plot) depicted by this picture.

At first I tried assigning the plot into a character and use the following code below, however that didn't work.

vp <- viewport(x=0.78,y=0.75,width=0.35, height=0.338)

full <- function() {
  print(g1)
  theme_set(theme_bw(base_size = 8))
  print(subplot2, vp = vp)
}

full()

For clarifications, g1 is the big plot while subplot2 is the plot i'd like to overlay.

Any help would be much appreciated. Is there a way to make the overlay happen?

Upvotes: 2

Views: 249

Answers (1)

Pierre Lapointe
Pierre Lapointe

Reputation: 16277

You can use par(fig=c(0.5,1,0.5,1), new=TRUE) like so. The numbers inside the fig option are x0,x1,y0,y1 and represent percentage of the plot. In this case, the figure starts (x0) at 50% of the x-axis and ends (x1) at 100% of the original axis.

plot(wt, mpg, main="Scatterplot Example", 
     xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19)
par(fig=c(0.5,1,0.5,1), new=TRUE)
plot(wt, mpg, main="", 
     xlab="Car Weight ", ylab="Miles Per Gallon ", pch=19, xlim=c(0, 3) )

enter image description here

Upvotes: 2

Related Questions