Home > Enterprise >  How to make icons in geom_pictogram in r start at 0
How to make icons in geom_pictogram in r start at 0

Time:01-05

I am working on making a pictogram using r where the categories are along the x axis and the amount of the thing that belongs in each category is represented by the number of icons. I would like to leave the ticks on the y axis to make it easy to see how many of the thing there are, but the icons start a little above 0 and finish a little above the real value, giving the appearance that the value is higher than it really is.

data <- data.frame(
  x = c('John', 'James', 'Jeff', 'Joe', 'Jake'),
  ht = c(72, 71, 73, 69, 66),
  icon = rep('rocket', 5)
)

ggplot(data, aes(label= x,
                 values = ht,
                 color=icon))  
  geom_pictogram(n_rows=5, make_proportional=FALSE, size=5, flip=TRUE)  
  facet_wrap(~x, nrow = 1, strip.position = "bottom")  
  scale_x_discrete()   
  scale_y_continuous(labels=function(x) x * 5, # multiplyer should be same as n_rows
                     expand = c(0,0),
                     limits = c(0,20))  
  scale_label_pictogram(
    name = NULL,
    values = c(
      'rocket' = 'rocket' 
    ))  
  theme(legend.position = "none") 

This results in a good approximation of what I'm looking for, but without adjusting the alignment of the icons and the y-axis ticks/labels, it doesn't work.

Pictograms put side-by-side with facet_wrap

I have been unable to find any way to move the y-axis ticks/labels higher or to shift the icons lower, either of which would work for this purpose. I have considered removing the y-axis ticks/labels and labeling the amount of the data just above each set of icons, but have been unable to get annotate or geom_text to work. I expect there's something simple I'm missing, but have no idea what it is at this point. I've also tried making this chart with echarts4r and waffle, though I've run into different issues with each that led me back to geom_pictogram.

CodePudding user response:

One option would be to use geom_text with stat="waffle". Doing so allows to shift the icons aka labels via position_nudge:

library(ggplot2)
library(waffle)

ggplot(data, aes(
  label = x,
  values = ht,
  color = icon
))  
  geom_text(
    stat = "waffle", n_rows = 5, make_proportional = FALSE, size = 5, flip = TRUE,
    family = "Font Awesome 5 Free",
    position = position_nudge(y = -.9), vjust = 0
  )  
  facet_wrap(~x, nrow = 1, strip.position = "bottom")  
  scale_x_discrete()  
  scale_y_continuous(
    labels = function(x) x * 5,
    expand = c(0, 0),
    limits = c(0, 20)
  )  
  scale_label_pictogram(
    name = NULL,
    values = c(
      "rocket" = "rocket"
    )
  )  
  theme(legend.position = "none")

enter image description here

  • Related