Here are some code examples used in the workshop activities, for reference when needed :-)

These examples use the following packages:
magick
gganimate (from github)
ggimage
nullabor
tidyverse
and assume that you’re working within a project.

getting images into R

We’ll use the R package magick to read images into R and do some exploration and manipulation of the image data. This example uses images from the CC0 image site pexels.com.

##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]   78   85   51   31   50   79   39   a9   53    73
## [2,]   69   a4   6f   88   ea   eb   95   ae   77    d5
## [3,]   2c   79   e1   81   93   82   85   93   5c    24
## [4,]   26   40   8f   6c   7f   8f   9b   bc   79    2a
## [5,]   12   15   21   40   5e   6a   86   dc   a2    27
## [6,]   0c   0e   12   20   33   42   4c   87   6b    1e
## [7,]   06   07   0c   13   1d   2b   2a   45   37    11

##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,]   58   66   44   33   32   81   3c   90   47    78
## [2,]   52   81   60   88   bb   d6   84   c0   7c    d8
## [3,]   21   5d   e0   7b   94   82   8a   a2   63    28
## [4,]   1d   32   8e   42   41   79   97   b6   70    28
## [5,]   11   12   1c   20   26   40   68   bd   86    24
## [6,]   0c   0d   10   15   1b   29   37   6a   53    1b
## [7,]   06   07   0b   0f   13   1c   1e   34   2a    0f
##  [1] 58 66 44 33 32 81 3c 90 47 78 52 81 60 88 bb d6 84 c0 7c d8 21 5d e0
## [24] 7b 94 82 8a a2 63 28 1d 32 8e 42 41 79 97 b6 70 28 11 12 1c 20 26 40
## [47] 68 bd 86 24 0c 0d 10 15 1b 29 37 6a 53 1b 06 07 0b 0f 13 1c 1e 34 2a
## [70] 0f

plots + images

We’ll add the use of R packages from the tidyverse, specifically readr, dplyr and ggplot2, as well as the packages ggimage to visualise data about movies with robot in their title using our own robot icons and gganimate to liven things up.

First, use the drawing tool to create your own robot icon and copy the URL link to your image. You’ll need to change this link in the code below :-)

clustering images

We’ll read in a random sample of 225 drawings of ducks from the Google Quick! Draw! dataset and see if we can group them into similar drawings using k-means clustering.

Download the duck images as a ZIP folder from here and save them into a folder within your project folder called “ducks”.

## [1] 0.9224833

pixelated animation

We’ll create images with text from scratch, take each pixel from an image and plot them individually, and then created some animated word text fun times!

la pièce de résistance

Lastly, let’s add a non-conventional use of the R package nullabor to create some sparkling lights for an Eiffel Tower mosaic made from Unsplash photos of Paris.

Download the Unsplash paris images as a ZIP folder from here and save them into a folder within your project folder called “paris”.

# read the image data for your tower
hex_data <- image_data(tower)
hex_colour <- paste0("#",hex_data, hex_data, hex_data)

# create a data frame from the pixels
# grab the coordinates then arrange from light
# to dark e.g. #ffffff then #000000
# if your tower is black, remove desc()
pixelsdf <- data.frame(hex = hex_colour) %>%
  mutate(x = (row_number() - 1)%%ncols + 1, y = ceiling(row_number()/ncols)) %>%
  arrange(desc(hex)) %>%
  mutate(id=row_number())

# how many white pixels (tower)
pixelsdftower <- pixelsdf %>%
  filter(hex=="#ffffff")

# arrange the paris images in order
# from darkest to lightest
# so in mosaic eiffel tower is black/dark
# take how many you need for the tower
towerpixels <- colours %>%
  arrange(light) %>%
  slice(1:nrow(pixelsdftower))

# the rest will be background
# arrange by hue to kinda group colours
backpixels <- colours %>%
  arrange(light) %>%
  slice((nrow(pixelsdftower)+1):nrow(.)) %>%
  sample_n(sample_size - nrow(pixelsdftower)) %>%
  arrange(desc(hue))

# create a reference data frame for mosaic
ref_colours <- bind_rows(towerpixels,backpixels) %>%
  mutate(id=row_number())

# join the ref_colours and pixelsdf (tower)
pixels <- pixelsdf %>%
  left_join(ref_colours, by="id")

# separate them out to use some transparency
# prob could incorporate this earlier!
pixels_tower <- pixels %>%
  slice(1:nrow(pixelsdftower))

pixels_background <- pixels %>%
  slice((nrow(pixelsdftower)+1):sample_size)

# first check of your creation!!
ggplot(data = pixels) +
  geom_tile(aes(x.x,y.x, fill=hex.y)) + 
  scale_fill_identity() +
  scale_x_continuous(limits=c(0, ncols+1)) +
  scale_y_reverse(limits=c(ncols+1, 0)) +
  theme_void() +
  theme(aspect.ratio=1)


Website and workshop materials developed by Anna Fergusson for the R-Ladies Auckland October 2018 Meetup.