Can magick combine two images in R?

I keep getting the following errors in one of the textbook examples. Could someone please explain why I'm getting these messages and how to fix them? Thank you.


little.dummy<- image_resize(dummy,"103x186")

ggplot(iris, aes(x=Petal.Length, y=Petal.Width, color=Species))+
  geom_point(shape=1, size=4, color="black")

background<- image_background(iris_plot,"white")
image_composite(image=background, composite_image = image_flop(little.dummy),offset = "+615+200")

Error: The 'image' argument is not a magick image object.

Error in assert_image(image) : object 'iris_plot' not found

1 answer

  • answered 2022-01-13 06:33 Kat

    Welcome to Stack Overflow! If you want great answers quickly, please make this question reproducible. This includes sample code you've attempted, listing non-base R packages, any errors/warnings received, sample data (e.g., data.frame(x=...,y=...) , like the output from dput(head(dataObject))), and what type of output you are expecting. Check out these resources for great questions: making R reproducible, [minimal reproducible example] and tagging questions.

    I'm not sure what you are working with here; what book? what data?

    Since I don't have that information, I want to go through your code a bit and provide a working example of these functions.

    In the first line of code (after the libraries) you call for the image object dummy. What is that? Where did the object come from?

    Here is an example of that function with an image we both can access.

    (tiger <- image_read_svg('', 
                             width = 350))
    # # A tibble: 1 × 7
    #   format width height colorspace matte filesize density
    #   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
    # 1 PNG      350    350 sRGB       TRUE         0 72x72  

    enter image description here

    The parentheses around the entire call is a method to tell R to both print the image and create the object if you were not already aware.

    Now for the function image_resize(). Since I know that my original image is even (350 x 350), I chose to keep the aspect ratio the same.

    (little.dummy <- image_resize(tiger,"200x200"))
    # # A tibble: 1 × 7
    #   format width height colorspace matte filesize density
    #   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
    # 1 PNG      200    200 sRGB       TRUE         0 72x72   

    enter image description here

    You went on to create a plot and save it as a file named "Iris." Did you leave out the code where you read this image into R as an object? Where did the object iris_plot come from?

    I am going to guess that when you attempted to create the object background, it didn't work. That could be why you received the errors.

    That being said, what I might do here is create a temporary file to store the plot, then dump the temp file, because it looks like you just wanted the white background, not to save a white square to your computer.

    # create a temporary location to hold the image
    temp <- tempfile()
    # save the image to the temp location
    ggsave(plot = last_plot(), 
           filename = temp)
    # make the image an object to work with
    background <- image_read(temp)
    # remove the temp file

    Now I have the plot to create the background and composite with my tiger.

    (background <- image_background(background,

    (I'm not going to add an image of white.)

    I changed the offset because the tiger is square. I also added a border, so you could see it (white on white, ya know?). Your code works.

      image = background, 
      composite_image = image_flop(little.dummy),
        offset = "+300+100"
      ) %>% image_border("black", 
                         "4x4")) # 4 px border all the way around

    enter image description here

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum