标签: gganimate

使用下降字母动画时,防止标题空间发生变化

我正在尝试使用一些月度数据制作动画gganimate.这些图表工作得很好,除了下降点(低于基线的字母,即g,j,p,q和y)的存在改变了标题占用的空间量.反过来,这会稍微移动标题的基线,这会减损动画.也就是说,当标题中有下降符时,标题会明显地"跳跃"一点.

一个例子:

myDF <-
  data.frame(
    Date = seq(as.Date("2015-01-15")
               , as.Date("2015-12-15")
               , "1 month")
    , x = 1:12
    , y = 1:12
  )

myDF$frame <-
  factor(format(myDF$Date, "%Y-%b")
         , levels = paste0("2015-", month.abb))

toAnimate <-
  ggplot(
    myDF
    , aes(x = x
          , y = y
          , frame = frame)
  ) +
  geom_point() +
  theme_gray()

gganimate::gganimate(toAnimate)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

使用旧版本gganimate的问题更为明显(并且不需要包含年份来演示),因为它移动了剧情而不是标题:

gganimate::gg_animate(toAnimate)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我可以通过使用所有大写字母(没有下行程序)来"解决"这个问题,但我并不特别喜欢所有大写的外观(特别是作为实际用例的较大标题的一部分).我也可以用已经有下降器的东西预先添加帧标题,例如,ggtitle("Timeperiod: ")我不想添加不相关的文本来解决这个问题(添加"Timeperiod:"就是我现在已经使用的东西).

我已经通过在帮助看themeggplot2,但我没有看到任何看起来像它会解决这个问题.

r ggplot2 gganimate

7
推荐指数
1
解决办法
153
查看次数

gganimate 改变比例(轴限制)

我想使用 来创建一个 gif gganimate,但我的轴范围在一帧中变化很大。这导致所有后续帧都被压缩。

ggplot2某些方面,有一个选项可以让scales="free". 有没有办法在 的每一帧中都有自由比例gganimate

下面是一个例子:

library(gapminder)
library(ggplot2)
library(gganimate)
theme_set(theme_bw())

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent,
                           frame = year)) +
  geom_point() +
  scale_x_log10()

gganimate(p)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

现在我们将其中一个数据点移动到某个极值。这会挤压所有后续未受影响帧中的点。

gapminder[1, "lifeExp"] <- 1000
gapminder[1, "gdpPercap"] <- 1e60

p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, 
                           frame = year)) +
  geom_point() +
  scale_x_log10()

gganimate(p)  # smooshed
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

r axes gganimate

6
推荐指数
1
解决办法
2071
查看次数

在使用带有tweenr的gganimate时绘制标题

我正在尝试从已经运行的数据集中添加年份标题tweenr.以来来自revolutionanalytics.com的示例

library(tidyverse)
library(tweenr)
library(gapminder)

gapminder_edit <- gapminder %>%
  arrange(country, year) %>%
  select(gdpPercap,lifeExp,year,country, continent, pop) %>%
  rename(x=gdpPercap,y=lifeExp,time=year,id=country) %>%
  mutate(ease="linear")

gapminder_tween <- tween_elements(gapminder_edit,
                              "time", "id", "ease", nframes = 150) %>%
  mutate(year = round(time), country = .group) %>%
  left_join(gapminder, by=c("country","year","continent")) %>%
  rename(population = pop.x)

gapminder_tween %>% arrange(country, .frame) %>% head()
#          x        y     time continent population .frame      .group year     country lifeExp   pop.y gdpPercap
# 1 779.4453 28.80100 1952.000      Asia    8425333      0 Afghanistan 1952 Afghanistan  28.801 8425333  779.4453
# 2 …
Run Code Online (Sandbox Code Playgroud)

animation r tween ggplot2 gganimate

6
推荐指数
1
解决办法
562
查看次数

如何保存使用 gganimate 包创建的 gif 帧

我将使用 gapminder 数据作为示例。假设我创建了这个动画:

library(gapminder)
library(ggplot2)
theme_set(theme_bw())
p <- ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = 
continent, frame = year)) +
  geom_point() +
  scale_x_log10()

library(gganimate)

gganimate(p)

gganimate(p, "output.gif")
Run Code Online (Sandbox Code Playgroud)

现在,我想访问构成 gif 的单个图像(帧)。有没有办法在 gganimate 中做到这一点,还是我需要使用动画包?

r gganimate

6
推荐指数
1
解决办法
5142
查看次数

使用 ggplot 绘制 shapefile 和 gganimate 动画

样本数据

library(raster)
library(ggplot2)

my.shp <- getData('GADM', country = 'FRA', level = 1)
plot(my.shp)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果我想使用 ggplot 绘制此数据:

my.shp_f <- fortify(my.shp, region = "ID_1")
ggplot(data = my.shp_f, aes(long, lat, group = group)) + geom_polygon(fill = "grey80")
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

问题一:为什么行政边界消失了?

问题 2: 我有另一个数据框,其中包含每个行政区划从第 1 天到第 365 天的 2 年日降雨量数据。

rain.data <- data.frame(ID_1 = rep(my.shp@data$ID_1, each = 2 * 365),
                        year = rep(rep(1981:1982, each = 365), times = 2),
                        day = rep(1:365, times = 4),
                        rain = sample(1:20, replace = T, 2 * 365 * …
Run Code Online (Sandbox Code Playgroud)

r shapefile ggplot2 rgdal gganimate

6
推荐指数
1
解决办法
1575
查看次数

在gganimate中有许多(> 50)个状态的问题

我正在尝试使用gganimate覆盖90年的数据集来创建GIF ,即我希望GIF运行90个州/年.然而,似乎gganimate只能处理不到50个州.

所以这是一个例子:

library(tidyverse)
# devtools::install_github('thomasp85/gganimate')
library(gganimate)

df = expand.grid(  x = 1,
                   y = c(2,3),
                year = 1670:1760) %>% mutate( z = 0.03* year,
                                              u = .2 * year)
Run Code Online (Sandbox Code Playgroud)

这一切都运作49年:

ggplot(data=df %>% filter(., year %in% 1670:1719) , aes()) + 
  geom_point( aes(x = x, y = y, fill = z, size = u), shape = 21 ) + 
  labs( title = 'Year: {closest_state}') +
  enter_appear() +
  transition_states(year, transition_length = 1, state_length = 2) 
Run Code Online (Sandbox Code Playgroud)

例1

然而,当我包括50(或更多)年时,它变得奇怪:

ggplot(data=df %>% …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

6
推荐指数
1
解决办法
893
查看次数

GGanimate:带有数值的 geom_text 以十进制数而不是整数进行动画处理

Data <- data.frame(Time = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
                   Group = c("A", "B", "C", "A", "B", "C", "A", "B", "C"),
                   Value = c(20, 10, 15, 20, 20, 20, 30, 25, 35))
Run Code Online (Sandbox Code Playgroud)

我在Time.

library(ggplot2)
library(gganimate)

p <- ggplot(Data, aes(Group, Value)) +
     geom_col(position = "identity") +
  geom_text(aes(label = Value), vjust = -1) +
     coord_cartesian(ylim = c(0, 40)) +
     transition_time(Time)
p 
Run Code Online (Sandbox Code Playgroud)

上面的代码很好地生成了条形转换的动画,但是 geom_text 中的变化还有很多不足之处,因为 geom_text 补间/转换具有 > 10 个小数位。理想情况下,我希望 geom_text 数值在转换时保持为整数,或者以某种方式控制舍入程度。

编辑:更改Value为整数类型无济于事。

在此处输入图片说明

r ggplot2 gganimate

6
推荐指数
1
解决办法
1750
查看次数

通过 gganimate 的动画条形图:view_follow 和 coord_flip 的冲突

我想用这个gganim包制作一个动画条形图。条形图的坐标应通过coord_flip(即水平条)翻转,并且 x 轴应根据条的长度灵活设置通过view_follow

考虑以下示例数据:

# Create example data
df <- data.frame(ordering = c(rep(1:3, 2), 3:1, rep(1:3, 2)),
                 year = factor(sort(rep(2001:2005, 3))),
                 value = round(runif(15, 0, 100)),
                 group = rep(letters[1:3], 5))
Run Code Online (Sandbox Code Playgroud)

如果我创建一个没有 的动画条形图coord_flip,一切正常:

library("gganimate")
library("ggplot2")

# Create animated ggplot without coord_flip
ggp <- ggplot(df, aes(x = ordering, y = value)) +
  geom_bar(stat = "identity", aes(fill = group)) +
  transition_states(year, transition_length = 2, state_length = 0) +
  view_follow(fixed_x = TRUE) # +
  # coord_flip() …
Run Code Online (Sandbox Code Playgroud)

r bar-chart ggplot2 axis-labels gganimate

6
推荐指数
1
解决办法
676
查看次数

如何在gganimate中为geom_point使用您自己的图像?

我正在尝试将我自己的图像用于geom_point,我可以读取这些内容。我知道geom_point允许您通过简单地编写shape = 243来选择许多形状(超过300种),但是我想要我自己的图像(例如徽标)。

当我没有指定color = factor(Name)时,它可以按预期工作。当我确实指定线条的颜色时,图像将变为纯色。我希望这条线是彩色的,所以有什么办法解决吗?谢谢!

library(gganimate)
library(gifski)
library(png)
library(ggimage)


Step  <- 1:50
Name  <- rep("A",50)
Image <- rep(c("https://jeroenooms.github.io/images/frink.png"),50)
Value <- runif(50,0,10)
Final <- data.frame(Step, Name, Value, Image)

a <- ggplot(Final, aes(x = Step, y = Value, group = Name, color = factor(Name))) + 
  geom_line(size=1) + 
  geom_image(aes(image=Image)) +
  transition_reveal(Step) + 
  coord_cartesian(clip = 'off') + 
  theme_minimal() +
  theme(plot.margin = margin(5.5, 40, 5.5, 5.5)) +
  theme(legend.position = "none") 

options(gganimate.dev_args = list(width = 7, height = 6, units = 'in', res=100))
animate(a, nframes …
Run Code Online (Sandbox Code Playgroud)

animation r image ggplot2 gganimate

6
推荐指数
1
解决办法
60
查看次数

使用gganimate和view_follow和geom_tile时如何摆脱由coord_flip引起的轴闪烁?

假设我们有这个带有缩放比例的条形图竞赛x-axis。从这个考虑的代码完全回答@乔恩春季和添加(有生线之前)非常最后一行:

library(tidyverse)
library(gganimate)
library(gapminder)
theme_set(theme_classic())

gap <- gapminder %>%
    filter(continent == "Asia") %>%
    group_by(year) %>%
    # The * 1 makes it possible to have non-integer ranks while sliding
    mutate(rank = min_rank(-gdpPercap) * 1) %>%
    ungroup()

p <- ggplot(gap, aes(rank, group = country, 
                     fill = as.factor(country), color = as.factor(country))) +
    geom_tile(aes(y = gdpPercap/2,
                  height = gdpPercap,
                  width = 0.9), alpha = 0.8, color = NA) +

    # text in x-axis (requires clip = "off" in …
Run Code Online (Sandbox Code Playgroud)

r flicker ggplot2 gganimate

6
推荐指数
1
解决办法
246
查看次数

标签 统计

gganimate ×10

r ×10

ggplot2 ×8

animation ×2

axes ×1

axis-labels ×1

bar-chart ×1

flicker ×1

image ×1

rgdal ×1

shapefile ×1

tween ×1