小编Sma*_*Sma的帖子

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

我想旋转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 …

r ggplot2 gganimate

5
推荐指数
1
解决办法
199
查看次数

标签 统计

gganimate ×1

ggplot2 ×1

r ×1