如何在ggplot中旋转自定义注释?

Sma*_*Sma 5 r ggplot2 gganimate

我想旋转annotation_customggplot2中包含的图像.

对于动画gganimate,我想添加具有特定角度的图像到线图.不幸的是,没有angle参数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()
Run Code Online (Sandbox Code Playgroud)

结果:

图片来自cl.ly/b83aefc7a7fa/gunplot.png

从图中可以看出,所有子弹都是水平对齐的.我想旋转子弹以对应线的斜率.在动画中,线条应该像子弹一样出现(由于没有aes参数,这将是另一个问题annotate_custom).

提前感谢您的建议!

Mik*_*kko 2

您可以使用magick包来旋转 png 文件:

library(magick)

bullet <- magick::image_read("bullet.png")

## To remove white borders from the example png
bullet <- magick::image_background(bullet, "#FF000000")

## Create angle column
gundf$angle <- seq(0,360, length.out = nrow(gundf))

## Plot
gundf %>% 
  ggplot(aes(x=year, y=deaths)) + 
  geom_line(size=1.2) +
  mapply(function(x, y, angle) {
    annotation_custom(rasterGrob(magick::image_rotate(bullet, angle)),
                              xmin = x-0.5, 
                              xmax = x+0.5, 
                              ymin = y-500, 
                              ymax = y+500)
                         },
    gundf$year, gundf$deaths, gundf$angle) + 
  theme_minimal()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

至于您关于使子弹跟随线的问题,请参阅此答案的评论。使对象具有与 ggplot2 中的线条相同的斜率是很棘手的,因为您需要知道绘图区域的纵横比(据我所知,目前没有在任何地方打印信息)。您可以通过使用定义的宽高比将绘图制作为文件(pdf 或 png)来解决此问题。然后,您可以使用 @Andrie ( ) 中的方程180/pi * atan(slope * aspect ratio)来代替我在示例中使用的方程。可能存在轻微的不匹配,您可以尝试使用常量进行调整。此外,在数据集中的每个点之间线性插入一个点可能是一个好主意,因为现在您正在绘制斜率变化的项目符号。在动画中这样做效果会很差。相反,在斜率恒定的情况下绘制子弹可能会更容易。