I would like to rotate an image included in annotation_custom
in ggplot2.
For an animation with gganimate
, I would like to add images with specific angles to a line plot. Unfortunately, there is no angle
parameter in annotation_custom
.
library(tidyverse)
library(grid)
library(png)
gundf <- tibble(year = c(1999:2017),
deaths = c(28874, 28663, 29573, 30242, 30136, 29569, 30694, 30896,
31224, 31593, 31347, 31672, 32351, 33563, 33636, 33594,
36252, 38658, 39773))
# Download png from cl.ly/47216db435d3
bullet <- rasterGrob(readPNG("bullet.png"))
gundf %>%
ggplot(aes(x=year, y=deaths)) +
geom_line(size=1.2) +
mapply(function(x, y) {
annotation_custom(bullet, xmin = x-0.5,
xmax = x+0.5,
ymin = y-500,
ymax = y+500)
},
gundf$year, gundf$deaths) +
theme_minimal()
Result:
As can be seen in the plot, all bullets are horizontally aligned. I would like to rotate the bullets to correspond to the slope of the line. In the animation, the line should emerge as if a bullet is shot (which will be another problem since there are no aes
parameters in annotate_custom
).
Thanks in advance for your suggestions!