0

iam very new to R and having difficulties to add several images to my plot. Adding a single picture works perfectly fine by using the following code:

`add_image_centre <- function(plot_path, image_path) {
 fig <- image_read(plot_path)
 fig <- image_resize(fig, "1000x1000")
 img <- image_read(image_path)
 img <- image_scale(img, "62x85")
 image_composite(fig, img, offset = "+387+442") 
 }
 imagepl <- add_image_centre(plot_path = ".png", image_path = ".png") 
 imagepl 

 image_write(imagepl, ".png")
 `

How can i add several images this way?

I have tried copying the code but then it just changes to the last picture added and removes the first one.

NORSTATS.
  • 13
  • 4
  • Welcome to SO. Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing your data or example data to accompany your code? – jrcalabrese Nov 27 '22 at 18:41
  • You are overwriting the image on exactly the same place in the plot each time. What are you expecting to see? – Allan Cameron Nov 27 '22 at 20:58
  • When copying i have changed the names and offset so i get 2 pictures. But that doesnt help – NORSTATS. Nov 27 '22 at 21:20

1 Answers1

0

Briefly

You can combine fig and img side-by-side as follows:

image_append( c( fig, img ))

Reference

See the section, Combining, at https://rdrr.io/cran/magick/f/vignettes/intro.Rmd, also shown below:

require( magick )

bigdata <- image_read('https://jeroen.github.io/images/bigdata.jpg')
frink   <- image_read("https://jeroen.github.io/images/frink.png")

# Appending means simply putting the frames next to each other:
image_append(image_scale(img, "x200"))

# Use stack = TRUE to position them on top of each other:
image_append(image_scale(img, "100"), stack = TRUE)

# Composing allows for combining two images on a specific position:
# It also and flattens the image, losing information about which pixel came from
# which layer.

bigdatafrink <- image_scale(
    image_rotate(
      image_background( frink, "none" ), 300
    )
  , "x200"
)

image_composite(
    image_scale( bigdata, "x400" )
  , bigdatafrink
  , offset = "+180+100"
)

Karl Edwards
  • 305
  • 4