当 x 轴为日期时,将图像插入 ggplot + gganimate
insert image into ggplot + gganimate when x axis are dates
我想使用 gganimate 为一些数据制作动画。以他们的 github 页面为例,我对其进行了一些更改以反映我的情况。 X 轴是日期,我希望所有帧的徽标都位于同一位置。
可重现代码:
library(magick)
library(gapminder)
library(ggplot2)
library(rsvg)
library(gganimate)
tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width = 400)
(p <- ggplot(gapminder, aes(year, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
annotation_raster(tiger, ymin = 75, ymax = 100, xmin = 1965, xmax = 2005) )
# here the animate part (not needed just for ilustrative purposes)
p + labs(title = 'Year: {frame_time}', x = 'Year', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
问题是当 x 轴不是日期时,我可以在任何图表中绘制徽标。
我怀疑这个问题与日期类型有关,但目前还没有成功。
您的问题似乎与您为 x 轴调用的对数刻度有关。它采用你的年份向量——它不是日期,而是一个 4 位整数——并对它应用对数转换……正如@camille 指出的那样,这意味着当你调用 animation_raster
坐标时( xmin / xmax) 不在绘图网格内。
这是一个通过将数据框中的年份更改为日期格式来合并日期的解决方案。它还在 geoms 后面分层图像并以其原始比例呈现,即。 1x1。
library(magick)
library(gapminder)
library(ggplot2)
library(rsvg)
library(gganimate)
library(lubridate)
tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width =
400)
xmin <- ymd("1965/01/01")
xmax <- ymd("2005/01/01")
ymin <- 30
height <- round(as.numeric(xmax-xmin)/356, 0)
ymax <- ymin + height
gapminder %>%
mutate(year = ymd(year, truncated = 2L)) %>%
ggplot() +
aes(year, lifeExp, size = pop, colour = country) +
annotation_raster(tiger, ymin = ymin, ymax = ymax, xmin = xmin, xmax =
xmax) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}', x = 'Year', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear') +
anim_save("animated_tiger_timeseries.gif")
产生这个...
这是您要找的吗?
我想使用 gganimate 为一些数据制作动画。以他们的 github 页面为例,我对其进行了一些更改以反映我的情况。 X 轴是日期,我希望所有帧的徽标都位于同一位置。
可重现代码:
library(magick)
library(gapminder)
library(ggplot2)
library(rsvg)
library(gganimate)
tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width = 400)
(p <- ggplot(gapminder, aes(year, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
annotation_raster(tiger, ymin = 75, ymax = 100, xmin = 1965, xmax = 2005) )
# here the animate part (not needed just for ilustrative purposes)
p + labs(title = 'Year: {frame_time}', x = 'Year', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
问题是当 x 轴不是日期时,我可以在任何图表中绘制徽标。
我怀疑这个问题与日期类型有关,但目前还没有成功。
您的问题似乎与您为 x 轴调用的对数刻度有关。它采用你的年份向量——它不是日期,而是一个 4 位整数——并对它应用对数转换……正如@camille 指出的那样,这意味着当你调用 animation_raster
坐标时( xmin / xmax) 不在绘图网格内。
这是一个通过将数据框中的年份更改为日期格式来合并日期的解决方案。它还在 geoms 后面分层图像并以其原始比例呈现,即。 1x1。
library(magick)
library(gapminder)
library(ggplot2)
library(rsvg)
library(gganimate)
library(lubridate)
tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width =
400)
xmin <- ymd("1965/01/01")
xmax <- ymd("2005/01/01")
ymin <- 30
height <- round(as.numeric(xmax-xmin)/356, 0)
ymax <- ymin + height
gapminder %>%
mutate(year = ymd(year, truncated = 2L)) %>%
ggplot() +
aes(year, lifeExp, size = pop, colour = country) +
annotation_raster(tiger, ymin = ymin, ymax = ymax, xmin = xmin, xmax =
xmax) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
labs(title = 'Year: {frame_time}', x = 'Year', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear') +
anim_save("animated_tiger_timeseries.gif")
产生这个...
这是您要找的吗?