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

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

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
