rggplot2ggimage

Getting a "UnableToOpenBlob" error when attempting to create a graph with custom point images using geom_image in ggplot2


i'm trying to build a dotplot with an image diplayed at every data point instead of just a bland point. I have tried many methods of introducing the data to R, and this is the closest ive gotten. In my data sheet I have a column with either NY_Male, NY_Female, OK_Male, or OK_Female on every point corresponding to which image id like displayed. Below is my code, and the error message displayed.

#import library
library(ggplot2)
library(beeswarm)
library(dunn.test)
library(ggimage)
library(png)
library(RCurl)
library(grid)

#set working directory
setwd("/Users/Documents//R")

#import data
data<-read.csv("parasite3_master.csv")

# Image
URL1 = "https://firebasestorage.googleapis.com/v0/b/tick-67255.appspot.com/o/Amblyomma_americanum_tick_blue_male.png?alt=media&token=fe30dc09-5a07-4fbd-b64e-7d90a8275eef"
NY__Male = readPNG(getURLContent(URL1))

## Image 2
URL2 = "https://firebasestorage.googleapis.com/v0/b/tick-67255.appspot.com/o/Amblyomma_americanum_tick_blue.png?alt=media&token=1e44e074-107b-4933-a6f5-9b1e81a17e9e"
NY__Female = readPNG(getURLContent(URL2))

# Image 3
URL3 = "https://firebasestorage.googleapis.com/v0/b/tick-67255.appspot.com/o/Amblyomma_americanum_tick.png?alt=media&token=d8863d57-12d8-4eb1-b86b-00dd21b33f07"
OK__Female = readPNG(getURLContent(URL3))

# Image 4
URL4 = "https://firebasestorage.googleapis.com/v0/b/tick-67255.appspot.com/o/Amblyomma_americanum_tick_green_male.png?alt=media&token=75519f09-30a6-4daa-b058-bf2430cc46d7"
OK__Male = readPNG(getURLContent(URL4))

# Crop the mal image
#NY_Male = NY_Male [40:250,,]

## Turn images into raster grobs
NY_Male = rasterGrob(NY__Male)
NY_Female = rasterGrob(NY__Female)
OK_Male = rasterGrob(OK__Male)
OK_Female = rasterGrob(OK__Female)

attach(NY_Male)
attach(NY_Female)
attach(OK_Male)
attach(OK_Female)

#check work
View(data)
summary(data)

#create data object
attach(data)

#Paste Function to combine location and sex
Categories <- paste(Location, Sex, sep=" ")

#Water Balance charts
#plot QH
WBplot <- ggplot(data , 
                 aes(y = DT, x = Categories , fill = Sex)) + 
  stat_summary(fun=mean,geom="errorbar",aes(ymax=..y..,ymin=..y..)) +
  geom_dotplot(binaxis='y', stackdir='center', position=position_dodge(1), dotsize = .5)+
  geom_image(aes(image=Image, size=0.015))
  ylab("Dehydration Tolerance (%)") +
  ylim(5,40) +
  theme(axis.text = element_text(size = 12),
        axis.title = element_text(size = 15, face = "bold"),
        axis.title.x = element_blank(),
        strip.text.x = element_text(size = 15),
        strip.text.y = element_text(size = 15),
        legend.position = "top",
        panel.background = element_rect(fill = "white", colour = NA),
        panel.border = element_rect(fill = NA, colour = "black"),
        strip.background = element_rect(colour = "black"))
WBplot

Error message displayed:

`Error in `geom_image()`:
! Problem while converting geom to grob.
ℹ Error occurred in the 3rd layer.
Caused by error:
! rsession-arm64: UnableToOpenBlob `NY_Male': No such file or directory @ error/blob.c/OpenBlob/2924`

I have tried attaching the image, and many other erronious things, but have never gotten past the error pasted above.


Solution

  • We figured it out and had to change it to a local image while reformatting geom_image. See below.

    # Load libraries
    library(ggplot2)
    library(ggimage)
    
    # Set working directory
    setwd("/Users/Documents/R")
    
    # Import data
    data <- read.csv("parasite3_master.csv")
    
    # Define local file paths for the images
    file_path_NY_Male <- "path_to_NY_Male.png"
    file_path_NY_Female <- "path_to_NY_Female.png"
    file_path_OK_Male <- "path_to_OK_Male.png"
    file_path_OK_Female <- "path_to_OK_Female.png"
    
    # Create the plot
    WBplot <- ggplot(data, aes(y = DT, x = paste(Location, Sex), 
                     fill = Sex)) +
      stat_summary(fun = mean, geom = "errorbar",
                   aes(ymax = ..y.., ymin = ..y..)) +
      geom_image(
        aes(
          image = ifelse(
            Sex == "Male" & Location == "NY", file_path_NY_Male,
            ifelse(Sex == "Female" & Location == "NY", file_path_NY_Female,
                   ifelse(Sex == "Male" & Location == "OK", 
                          file_path_OK_Male, file_path_OK_Female))
          )
        ),
        size = 0.015,
        position = position_jitter(width = 0.2)
      ) +
      ylab("Dehydration Tolerance (%)") +
      ylim(5, 40) +
      theme(
        axis.text = element_text(size = 12),
        axis.title = element_text(size = 15, face = "bold"),
        axis.title.x = element_blank(),
        strip.text.x = element_text(size = 15),
        strip.text.y = element_text(size = 15),
        legend.position = "top",
        panel.background = element_rect(fill = "white", colour = NA),
        panel.border = element_rect(fill = NA, colour = "black"),
        strip.background = element_rect(colour = "black")
      )
    
    # Display the plot  
    
    WBplot