0

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.

library(tidyverse)
library(magick)

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

ggplot(iris, aes(x=Petal.Length, y=Petal.Width, color=Species))+
  geom_point(size=4)+
  scale_color_manual(values=c("grey0","grey65","grey100"))+
  geom_point(shape=1, size=4, color="black")
  
ggsave("iris",device="png",scale=.8)

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
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
John
  • 1

1 Answers1

2

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('http://jeroen.github.io/images/tiger.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(), 
       device="png", 
       scale=.8, 
       filename = temp)

# make the image an object to work with
background <- image_read(temp)

# remove the temp file
unlink(temp)

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

(background <- image_background(background,
                                "white"))

(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_composite(
  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

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kat
  • 15,669
  • 3
  • 18
  • 51
  • I did use the Iris data, I even used the exact plotting code you used, that's why I didn't include it. I don't have whatever you stored in `dummy`, though. That is why there is a tiger on top of the plot background. If you want to know exactly why you received the error you received, then you need to post the code you used leading up to that point. Your question needs to be reproducible. Check out these resources on asking questions: [making R reproducible](https://stackoverflow.com/q/5963269) and [tagging questions](https://stackoverflow.com/tags/r/info). – Kat Jan 16 '22 at 15:44