#
# Contrived example where we draw a grouped scatterplot with a legend and
# manually add corresponding best fit lines for each group...
#
# First draw the grouped scatterplot
tinyplot(Sepal.Length ~ Petal.Length | Species, iris)
# Preserving adjusted par settings is good for adding elements to our plot
for (s in levels(iris$Species)) {
abline(
lm(Sepal.Length ~ Petal.Length, iris, subset = Species==s),
col = which(levels(iris$Species)==s)
)
}
# Get saved par from before the preceding tinyplot call (but don't use yet)
sp = get_saved_par("before")
# Note the changed margins will affect regular plots too, which is probably
# not desirable
plot(1:10)
# Reset the original parameters (could use `par(sp)` here)
tpar(sp)
# Redraw our simple plot with our corrected right margin
plot(1:10)
#
# Quick example going the other way, "correcting" for par.restore = TRUE...
#
tinyplot(Sepal.Length ~ Petal.Length | Species, iris, restore.par = TRUE)
# Our added best lines will be wrong b/c of misaligned par
for (s in levels(iris$Species)) {
abline(
lm(Sepal.Length ~ Petal.Length, iris, subset = Species==s),
col = which(levels(iris$Species)==s), lty = 2
)
}
# grab the par settings from the _end_ of the preceding tinyplot call to fix
tpar(get_saved_par("after"))
# now the best lines are correct
for (s in levels(iris$Species)) {
abline(
lm(Sepal.Length ~ Petal.Length, iris, subset = Species==s),
col = which(levels(iris$Species)==s)
)
}
# reset again to original saved par settings before exit
tpar(sp)
Run the code above in your browser using DataLab