Pictograph in R

Hi. I would like to do a pictograph in R. Suppose my employee count went from 1 in 2012 to 2 in 2013 (numbers in hundreds), and I went to create a column graph with one image in the 2012 column and the same image twice in the 2013 column as if it were a column graph. Suppose I have the image. How do I place the image on the graph?

If you want an image to experiment on, with the ability to resize, here is Frink:
library(magick)
frink <- image_read("https://jeroen.github.io/images/frink.png")
image_info(frink)
image_scale(frink, geometry = "200") # resize proportionally to width: 200px
image_scale(frink, geometry = "x200") # resize proportionally to height: 200px

1 Like

Is that 3 frinks in total, arranged

   X
X  X

Yes. Can we do this with Frinks?

Here you go

library(magick)
#> Linking to ImageMagick 6.9.12.93
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

# Example image
frink <- image_read("https://jeroen.github.io/images/frink.png")
white_layer <- image_blank(width = image_info(frink)$width,
                                         height = image_info(frink)$height,
                                         color = "white")
ghost   <- image_composite(frink,white_layer)
singlet <- c(ghost,frink)
doublet <- c(frink, frink)
left    <- image_append(image_scale(singlet, "100"), stack = TRUE)
right   <- image_append(image_scale(doublet, "100"), stack = TRUE)
triplet <- c(left,right)
frinks  <- image_append(image_scale(triplet, "100"), stack = FALSE)
frinks

Created on 2023-12-25 with reprex v2.0.2

Technocrat,
Well done! ChatGPT was unable to do this.
Can we place this inside an x-axis and y-axis, and label the x-axis 2022 and 2023?

I see there is a image_border(frink, "black", "10x5"), but that adds a top border and a right border that I don't want.

Here's a sketch, needing fiddling to get the image placement right, using scale_x_discrete() to adjust the x-axis and theming of the ggproto base item to get the rest of the annotation to taste.

library(ggplot2)
library(grid)
library(magick)
#> Linking to ImageMagick 6.9.12.93
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

# Example image
frink <- image_read("https://jeroen.github.io/images/frink.png")
white_layer <- image_blank(width = image_info(frink)$width,
                           height = image_info(frink)$height,
                           color = "white")
ghost   <- image_composite(frink,white_layer)
singlet <- c(ghost,frink)
doublet <- c(frink, frink)
left    <- image_append(image_scale(singlet, "100"), stack = TRUE)
right   <- image_append(image_scale(doublet, "100"), stack = TRUE)
triplet <- c(left,right)
frinks  <- image_append(image_scale(triplet, "100"), stack = FALSE)

base_layer <- data.frame(Year = 2022:2023)
ggplot(base_layer,aes(Year,)) +
  xlab("Year") +
  theme_minimal()
grid.raster(frinks)

Created on 2023-12-26 with [reprex v2.0.2]

Thank you. I would think there is a scale_x_discrete that would get the x limits as 2 integers, but I can't find it. Also, can we get a straight line x axis and y axis?

library(ggplot2)
library(grid)
library(magick)
#> Linking to ImageMagick 6.9.12.93
#> Enabled features: cairo, fontconfig, freetype, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fftw, ghostscript, x11


# Example image
frink <- image_read("https://jeroen.github.io/images/frink.png")

white_layer <- image_blank(width = image_info(frink)$width,
                           height = image_info(frink)$height,
                           color = "white")
ghost   <- image_composite(frink,white_layer)
singlet <- c(ghost,frink)
doublet <- c(frink, frink)
left    <- image_append(image_scale(singlet, "100"), stack = TRUE)
right   <- image_append(image_scale(doublet, "100"), stack = TRUE)
triplet <- c(ghost,left,ghost,right)
frinks  <- image_append(image_scale(triplet, "100"), stack = FALSE)
base_layer <- data.frame(Year = 2022:2023,
                         Confusion = c("Low","High"))
ggplot(base_layer,aes(Year,Confusion)) +
  xlab("Year") +
  xlim(c(2021,2024)) +
  ylab("Confusion") +
  ylim(c("Low","High")) +
  theme_minimal() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank())
grid.raster(frinks,y=.6,height = 0.75)

Created on 2023-12-26 with [reprex v2.0.2]

1 Like

This works for me. Thank you.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.