Little useless-useful R functions – Making boxplot from a picture

[This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Generating violin boxplots from image. Yes, why not. 🙂

We create a raster image from a picture and calculating the ratio of the pixels on the scale of grayscale. The more the darker colour is represented in the pixels, the bigger the value. And this value is converted into the vector of values. And each vector is represneted as a violin boxplot.

##########################################
# 
# Converting JPG and plots raster using horizontal violins
#
# Series:
# Little Useless-useful R functions #33
# Created: January 29, 2022
# Author: Tomaz Kastrun
# Blog: tomaztsql.wordpress.com
# V.1.0
###########################################

# Libraries
pkg <- c("dplyr", "tidyr", "ggplot2", "magick", "stringr",
         "forcats", "viridis", "grid", "purrr","hrbrthemes")
lapply(pkg, require, character.only = TRUE)

img <- magick::image_read("image/appleLogo.jpg")
img <- img %>% 
  image_quantize(max=2, colorspace = 'gray', dither=TRUE) %>%
  image_scale(geometry = geometry_size_pixels(width=50, height=15, preserve_aspect=FALSE))

# Image manipulation
mat <- t(1L - 1L * (img[[1]][1,,] > 180))
mat_df <-data.frame(mat)

# Transpose  data
dff <- data.frame(x = NULL, y = NULL)
for (i in 1:nrow(mat_df)) {
  for (j in 1:ncol(mat_df)){
    if (mat_df[i,j] == 1){
      d <- data.frame(x=i, y=j)
      dff <<- rbind(dff, d)
 }}}

# Creating factors
dff$x <- str_pad(as.character(dff$x), 3, pad = "0")

# Reversing order
df <- dff %>%
  mutate(x = fct_rev(fct_reorder(x,y))) %>%
  purrr::map_df(rev)

df2 <- dff %>% mutate(x = fct_rev(fct_reorder(x,y)))
df3 <- data.frame(x = as.character(df2$x), y = df$y)

vp <- df3 %>%
  ggplot( aes(x=x, y=y, fill=x, color=y)) +
  geom_violin(width=1.5, size=0.2) +
  scale_fill_viridis(discrete=TRUE) +
  scale_color_viridis(discrete=TRUE) +
  theme_void() 

print(vp, vp=viewport(angle=-90))  # flip and change graph orientation

With the library imager, I will also prepare an additional post no better calculation of greyscale and converting them to boxplots.

As always, code is available in at the Github in same Useless_R_function repository. Check Github for future updates.

Happy R-coding and stay healthy!“

To leave a comment for the author, please follow the link and comment on their blog: R – TomazTsql.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)