标签: gganimate

如何在Shiny中创建和显示动画GIF?

我可以将保存的文件作为图像加载但无法gganimate直接使用.渲染GIF的替代方法很难知道,但知道如何gganimate专门渲染将真正解决我的问题.

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

ui <- basicPage(
    plotOutput("plot1")
)

server <- function(input, output) {
    output$plot1 <- renderPlot({
        p = ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, color = continent, frame = year)) +
  geom_point() +
  scale_x_log10()

       gg_animate(p)

    })

}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

gif shiny gganimate

5
推荐指数
2
解决办法
5712
查看次数

gganimate 在 Windows 10 中渲染像素化

我必须使用 gganimate 渲染一些动画,但图像不好看。它们缺乏清晰度,边界像素化。有什么方法可以在 Windows 10 中获得更好的结果吗?

我的代码是:

library(gapminder)
library(ggplot2)
library(gganimate)
Cairo(600, 600, file="plot.png", type="png", bg="white")
ggplot(gapminder,aes(gdpPercap, 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() +
  facet_wrap(~continent) +
  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')
Run Code Online (Sandbox Code Playgroud)

我得到的是这样的:

在此输入图像描述

animation r ggplot2 gganimate

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

r gganimate:transition_reveal(更改标题)

我想使用 gganimate 创建一个 transition_reveal 动画,其中标题沿着当前显示的迭代变化。因此,开始时的标题将是“日期:2019-01-01”,然后逐渐适应动画并以显示“日期:2019-01-20”结束。

a <- seq(1,20,1)
b <- seq(1,40,2)
c <- seq(as.Date("2019-01-01"), as.Date("2019-01-20"), by="days")
db <- cbind(a,b,c) %>% as.data.frame()

p <- ggplot(db, aes(x = a, y = b)) +
  geom_path() +
  labs(title = "Date: {frame_time}") +
  transition_reveal(c)

animate(p, renderer = gifski_renderer(loop = FALSE), nframes = 90, fps = 30, height = 600, width = 1100, start_pause = 0)
Run Code Online (Sandbox Code Playgroud)

目前,我收到以下错误:

50: object 'fame_time' not found
Run Code Online (Sandbox Code Playgroud)

transition r gganimate

5
推荐指数
0
解决办法
2544
查看次数

gganimate 条形图:替换条形图时平滑过渡

我想用这个gganimate包创建一个动画条形图。条形图应包含 4 个条形,但应同时显示其中的三个条形。当一根柱线脱落而新柱线出现时,动画应该是平滑的(就像两个柱线在图中切换位置时一样)。

考虑以下示例:

# Set seed
set.seed(642)

# 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 = c(letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)],
                           letters[sample(1:4, 3)]))

# Load packages
library("gganimate")
library("ggplot2")

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

在此处输入图片说明

如果更换了一个酒吧,酒吧的颜色只是改变,没有任何平滑的动画(即新的酒吧应该从侧面飞入,替换的酒吧应该飞出)。

问题:我怎样才能顺利更换钢筋?

animation r bar-chart ggplot2 gganimate

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

如何在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
查看次数

动画排序的条形图:条形重叠的问题

我创建了一个动画的条形图,其中显示了一些球员的进球得分。在整个代码下面显示了我如何进入输出。

动画按预期工作。但是,具有相同值的条形重叠。

我想防止酒吧重叠。最好的情况是,得分最高的玩家要显示在同一等级的其他玩家之上。

在动画开始时平均得分的玩家顺序无关紧要。

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

df <- data.frame(Player = rep(c("Aguero", "Salah", "Aubameyang", "Kane"), 6), 
                 Team = rep(c("ManCity", "Liverpool", "Arsenal", "Tottenham"), 6), 
                 Gameday = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6),
                 Goals = c(0,1,2,0,1,1,3,1,2,1,3,2,2,2,4,3,3,2,4,5,5,3,5,6),
                 stringsAsFactors = F)

gap <- df %>%
  group_by(Gameday) %>%
  mutate(rank = min_rank(-Goals) * 1,
     Value_rel = Goals/Goals[rank==1],
     Value_lbl = paste0(" ", Goals)) %>%
  filter(rank <=10) %>%
  ungroup()

p <- ggplot(gap, aes(rank, group = Player, stat = "identity",
                 fill = as.factor(Player), color = as.factor(Player))) +
  geom_tile(aes(y = Goals/2,
            height = Goals,
            width …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

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

在动画ggplot2中的轴标签上包含图像

我创建了一个动画的条形图,显示玩家得分(虚构)的目标。

请参见示例的复制数据:

df <- data.frame(Player = rep(c("Aguero", "Salah", "Aubameyang", "Kane"), 6),
                 Team = rep(c("ManCity", "Liverpool", "Arsenal", "Tottenham"), 6), 
                 Gameday = c(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6),
                 Goals = c(0,1,2,0,1,1,3,1,2,1,3,2,2,2,4,3,3,2,4,5,5,3,5,6),
                 stringsAsFactors = F)
Run Code Online (Sandbox Code Playgroud)

以下动画条形图由以下代码创建。

在此处输入图片说明

# loading required
library(tidyverse)
library(gganimate)
library(png)
Run Code Online (Sandbox Code Playgroud)

编辑: 我想为每个玩家添加以下图标:

icon1.png <- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player1.png')
icon2.png <- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player2.png')
icon3.png <- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player3.png')
icon4.png <- image_read('https://raw.githubusercontent.com/sialbi/examples/master/player4.png')

gap <- df %>%
  group_by(Gameday) %>%
  mutate(rank = min_rank(-Goals) * 1,
         Value_rel = Goals/Goals[rank==1],
         Value_lbl = paste0(" ", Goals)) %>%
  filter(rank <=10) %>%
  ungroup()

gap %>%
  group_by(Player) %>%
  arrange(Gameday) %>%
  mutate(prev.rank = …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 gganimate

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

使用滑动窗口对ggplot时间序列图进行动画处理

我正在寻找在不损失分辨率的情况下为长时间序列图制作动画的方法。我希望视图在数据之间“平移”,以显示从头到尾的滑动子集。

假设我有以下内容:

  library(ggplot2)
  library(dplyr)
  library(gganimate)

  df <- as.data.frame(cumsum(rnorm(1:10000))) %>% rename(y = 1)
  df <- mutate(df, seq = seq(1, 10000, by = 1))

  ggplot(df, aes(x = seq, y = y)) + 
    geom_line()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想创建一个动画来显示更多细节,方法是一次只关注数据的一部分,然后从头到尾滑动。想象一下通过放大镜放大该系列,同时在下方滑动图...这就是我要达到的效果。有可能通过gganimate吗?如果没有,有什么建议吗?

r time-series ggplot2 gganimate

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

将 gganimate 与华夫饼图结合使用

我一直在玩这个waffle包,并试图让它与gganimate.

作为mpg示例,我创建了华夫饼图来显示每个模型的数量class。然后,我想使用 gganimate 依次显示每个制造商按类别划分的模型图表。我可以用来facet_wrap()同时显示所有制造商的图表,但希望能够循环浏览它们。

当我尝试将 gganimate 应用于华夫饼图时,出现错误:

mapply(FUN = f, ..., SIMPLIFY = FALSE) 中的错误:零长度输入不能与非零长度输入混合

我不确定是否waffle与 不兼容gganimate,或者我是否做错了什么。

这是代码:

library(tidyverse)
library(waffle)
library(gganimate)

data("mpg")
d <- mpg

d$class <- as.factor(d$class)
d$manufacturer <- as.factor(d$manufacturer)

plot <- d %>% count(manufacturer, class) %>%
  ggplot(aes(fill = class, values = n)) +
  geom_waffle(color = "white",
              size = .75,
              n_rows = 4) +
  ggthemes::scale_fill_tableau(name = NULL) +
  coord_equal() +
  theme_minimal() +
  theme(panel.grid = element_blank(), …
Run Code Online (Sandbox Code Playgroud)

animation r ggplot2 gganimate waffle-chart

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

使用 gganimate 为地图制作动画需要使用哪个transition_*?

我想制作一张美国各县的动画分区统计图,其中包含一段时间内确诊的 COVID-19 病例数(是的,又一个冠状病毒图)。是三天数据选择的链接(欢迎建议更永久的托管位置)。以下是创建静态地图的代码(注释过滤器以包含所有日期):

library(tidyverse)
library(gganimate)
library(ggmap)
library(maps)
library(scales)

p <- part_data %>%
  filter(date == as.Date("2020-03-30")) %>%
  ggplot(aes(x = long, y = lat, group = group)) +
  geom_polygon(aes(fill = confirmed_new), color = "grey70", size = 0.05) + 
  geom_path(data = state_map, colour = "black") +
  coord_map() +
  scale_fill_distiller(trans = "log10", direction = 1, palette = "YlOrRd", na.value = "white", limits = c(1, 1E4), labels = comma)
Run Code Online (Sandbox Code Playgroud)

这给出了这个非常漂亮的情节: 地图图

但是我现在如何制作一个随日期移动的动画呢?我试过

p +
  transition_time(date)
Run Code Online (Sandbox Code Playgroud)

p + 
  transition_states(date)
Run Code Online (Sandbox Code Playgroud)

但在这两种情况下,R 似乎在渲染进度条出现之前就冻结了,过了一会儿我收到了错误Error: cannot allocate …

ggplot2 gganimate

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