Home > Software design >  Layering/stacking/multiple graphs on one plot using symbols
Layering/stacking/multiple graphs on one plot using symbols

Time:05-28

I'm trying to do this with base-r

plot(x=NA, ylim = range(smokyants$elevation_m), xlim = range(smokyants[,5:42]))
for(ii in seq(1,length(ants)))
{
  points(unlist(elevation)~unlist(ants[ii]))  
  #symbols(smokyants$elevation_m, unlist(ants[ii]), circles = unlist(ants[ii]))
}

I'd like to use symbols() the way I'm using points(), above. But when I do it'll display multiple plots. However points() will behave how I want and will layer everything onto one graph.

Thanks in advance.

CodePudding user response:

The symbols() function invokes plot() at the end, so each time it's making a new plot. You can change this behaviour by adding par(new=TRUE) before the next plot. For example:

data(mtcars)

with(subset(mtcars, am == 1), symbols(hp, mpg, circles = wt, xlim=c(40, 350), ylim=c(10,34)))
par(new=TRUE)
with(subset(mtcars, am == 0), symbols(hp, mpg, squares = wt, xlim=c(40, 350), ylim=c(10,34),fg="red"))

Created on 2022-05-27 by the reprex package (v2.0.1)

One thing to keep in mind is that the x- and y-limits need to be the same for each plot, otherwise you will get a mismatch, potentially, between the two plots. Here's what it would look like if we forgot to harmonize the axis limits:

data(mtcars)

with(subset(mtcars, am == 1), symbols(hp, mpg, circles = wt, ))
par(new=TRUE)
with(subset(mtcars, am == 0), symbols(hp, mpg, squares = wt, fg="red"))

Created on 2022-05-27 by the reprex package (v2.0.1)

  • Related