2

I am looking for a R package in order to visualize quantities and shares using icons. For instance, 21 female and 4 male participants in a study could be visualized by 21 female icons and 4 male icon aligned in one line or across five lines like in this figure.

enter image description here

So far I've tried to use the icons library which enables the use of fontawesome icons. However this icons cannot be plotted in a loop. See https://github.com/mitchelloharawild/icons

user3072843
  • 308
  • 3
  • 13
  • I'm voting to close as off-topic, since questions shouldn't just be about package recommendations. However, you can do this with the waffle package – camille Sep 10 '21 at 14:43
  • 1
    I described a clear problem to be solved in R and I reported what ways I have already tried to solve the problem. This is not off-topic. The waffle package is a valid answer. Thanks. – user3072843 Sep 10 '21 at 14:45
  • For me this is not a duplicate of https://stackoverflow.com/questions/2181902/how-to-use-an-image-as-a-point-in-ggplot because the images are not placed randomly arround the plane but arranged in a so called waffle plot. – danlooo Sep 10 '21 at 14:46
  • 1
    Maybe if there were some code it wouldn't be just a rec question, but right now you're essentially saying "I want to make something like this figure" and then asking for a package to do that. Help section: "Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – camille Sep 10 '21 at 14:56
  • Whats wrong about asking for an efficient way to solve a specified problem? I thought a package could exist but I did not exclude code solutions. I do not see that a question like this could attract opinionated answers. – user3072843 Sep 12 '21 at 13:30

2 Answers2

2

There is the package ggwaffle which can be installed as following:

install.packages("devtools")
devtools::install_github("liamgilbey/ggwaffle")

Then you can do:

library(tidyverse)
library(ggwaffle)
library(emojifont)

data <- tibble(dog = rep("a", 4) %>% c(rep("b", 21)))
waffle_data <- waffle_iron(data, aes_d(group = dog), rows = 5) %>% mutate(label = fontawesome('fa-paw'))

ggplot(waffle_data, aes(x, y, color = group)) + 
  geom_text(aes(label=label), family='fontawesome-webfont', size=4) +
  coord_equal() + 
  theme_waffle()

Created on 2021-09-10 by the reprex package (v2.0.1)

danlooo
  • 10,067
  • 2
  • 8
  • 22
0

Here is a way:

library(xtable)
library(fontawesome)
library(htmltools)

dat <- data.frame(
  c(
    fa("dog", width = "2em", height = "2em"), 
    fa("cat", width = "2em", height = "2em")
  ),
  c(
    fa("dog", width = "2em", height = "2em"), 
    fa("cat", width = "2em", height = "2em", fill = "red")
  )
)

tbl <- print(xtable(dat), type = "html", 
             sanitize.text.function = function(x) x,
             include.rownames = FALSE,
             include.colnames = FALSE,
             print.results = FALSE)

browsable(HTML(tbl))

enter image description here

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225