13

I'm trying to compare different NBA rookies across different stats, and I thought the graph would look great if I could add the player's face at the end of the graph like in the r/dataisbeautiful graphs. My code is currently this:

a3 %>%
  ggplot(aes(x = reorder(Player,
                         PPM),
             y = PPM)) +
  geom_bar(stat = "identity",
           aes(fill = Player)) +
  geom_text(aes(label = PPM), size = 3, position = position_dodge(width = 1),
            hjust = -0.1) +
  coord_flip() +
  theme_minimal() +
  xlab("Player") +
  ylab("Points Per Minute") +
  theme(legend.position = "none")

This is what my graph currently looks likelike

Pedro Guizar
  • 357
  • 2
  • 9
  • 2
    Have you seen this blog post, looks quite relevant: https://jcarroll.com.au/2019/08/13/ggtext-for-images-as-x-axis-labels/ – Ben Nov 03 '19 at 03:58
  • 2
    The `ggtext` package seems to allow this: https://github.com/clauswilke/ggtext#markdown-in-theme-elements – Jon Spring Nov 03 '19 at 04:01
  • Does this answer your question? [Including images on axis label in an animated ggplot2](https://stackoverflow.com/questions/54973129/including-images-on-axis-label-in-an-animated-ggplot2) – tjebo Nov 03 '19 at 13:04

1 Answers1

13

You didn't provide a reprex, so I need to make something up. I would probably do it like this.

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

data <- tibble(
  count = c(5, 6, 6, 4, 2, 3),
  animal = c("giraffe", "elephant", "horse", "bird", "turtle", "dog"),
  image = list(
    image_read_svg("http://steveharoz.com/research/isotype/icons/giraffe.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/elephant.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/horse.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/bird.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/turtle.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/dog.svg")
  )
)

ggplot(data, aes(animal, count, fill = animal, image = image)) +
  geom_isotype_col(
    img_height = grid::unit(1, "null"), img_width = NULL,
    ncol = 1, nrow = 1, hjust = 1, vjust = 0.5
  ) +
  coord_flip() +
  guides(fill = "none") +
  theme_minimal()

Created on 2019-11-03 by the reprex package (v0.3.0)

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104
  • Thanks, this worked great! I wanted to ask if it was possible to display two images on the same bar on here (I'm assuming by messing with the hjust value) by having something like this: ggplot(data, aes(animal, count, fill = animal, image = image & x)) – Pedro Guizar Nov 09 '19 at 20:32
  • Please post a separate top-level question for this. – Claus Wilke Nov 09 '19 at 20:37
  • Just did @Claus Wilke https://stackoverflow.com/questions/58793147/how-to-insert-several-images-into-a-bar-graph-using-ggtextures-and-ggplot – Pedro Guizar Nov 10 '19 at 21:36
  • This is very useful. Is there a plan to get ggtextures on CRAN? – stevec Apr 25 '20 at 10:17
  • 2
    No. There is now ggpattern which is much more powerful. https://github.com/coolbutuseless/ggpattern – Claus Wilke Apr 26 '20 at 16:05
  • There is any way to position the images outside the bar (at the end bar)? – André WZ Jul 07 '21 at 02:27