rmaia / pavo

tools for the analysis of color data in R
http://pavo.colrverse.com
GNU General Public License v2.0
68 stars 17 forks source link

Is there a way to put inset-plot(s) and/or photographs into an aggplot? #237

Closed juandemedel closed 2 years ago

juandemedel commented 2 years ago

Hello dear community @Bisaloo, @thomased. I have been working with pavo to plot light spectrums on an aggplot. Besides, I have several plots and photographs that I would like to put into one plot (e.g insets). Thereby, I am wondering if there is a way to do so? Would you please give me some advice?

thomased commented 2 years ago

Hi @juandemedel, pavo's plot() and aggplot() etc. mostly just use base graphics, so you don't need to do anything pavo-specific. There are many ways to achieve that kind of thing via fiddling with the margins & options on base graphics, or using packages like magick (which pavo uses in places) or imager for adding photographs. Examples

# Library
library(pavo)

# Spectral data
data(flowers)

# Plot main spec figure
par(fig = c(0, 1, 0, 1))
aggplot(flowers)

# Plot inset spec figure, and control it's location with 'fig'
par(fig = c(0.01, 0.35, 0.5, 1), new = TRUE)  
aggplot(flowers, 
        xaxt = 'n',
        yaxt = 'n',
        xlab = '',
        ylab = '',
        shadecol = 'forestgreen',
        lcol = 'black')
Screen Shot 2022-06-18 at 9 27 21 am

or the same using pavo's image-handling abilities and fiddling with base plot options just like above:

# Library
library(pavo)

# Load flower spectra
data(flowers)

# Load a built-in image in pavo
papilio <- getimg(system.file("testdata/images/butterflies/papilio.png", package = "pavo"))

# Plot main figure
par(fig = c(0, 1, 0, 1))
aggplot(flowers)

# Add image
par(fig = c(0.01, 0.35, 0.5, 1), new = TRUE)  
plot(papilio, 
     axes = FALSE,
     xlab = '',
     ylab = '',
     main = '')
Screen Shot 2022-06-18 at 10 05 34 am