Reputation: 4205
I have three groups (a, b, c) with associated values, that I want to plot as bar chart.
Using ggplot, I would like to insert a different image (in png format) in each bar. So in bar a, I want to insert the image 'a.png', in bar b, the image 'b.png' and in c the image 'c.png'
df = data.frame(
group = c('a', 'b', 'c'),
value = 1:3)
ggplot(df, aes(group, value)) +
geom_col()
The few posts I found are not helpful or very old.
Inserting an image to ggplot outside the chart area
https://www.reddit.com/r/rstats/comments/6qh4kt/how_to_make_ggplot_not_beautiful_insert_pictures/
Do you have an idea?
Thanks
Upvotes: 2
Views: 1738
Reputation: 124473
One option is to use ggimage
. Source: https://guangchuangyu.github.io/pkgdocs/ggimage.html. Try this:
library(ggplot2)
library(ggimage)
library(dplyr)
set.seed(1234)
img <- list.files(system.file("extdata", package="ggimage"),
pattern="png", full.names=TRUE)
df = data.frame(
group = c('a', 'b', 'c'),
value = 1:3,
image = sample(img, size=3, replace = TRUE)
) %>%
mutate(value1 = .5 * value)
ggplot(df, aes(group, value)) +
geom_col() +
geom_image(aes(image=image, y = value1), size=.2)
Created on 2020-03-19 by the reprex package (v0.3.0)
Upvotes: 3