是否可以在R Markdown报告中添加GIF?

时间:2019-11-13 12:37:15

标签: r r-markdown gif

这是我用来创建GIF图的代码-

# Libraries:
library(ggplot2)
library(gganimate)
library(giphyr)

# Creating the monthly dataframes

x_01=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(7601, 7950, 948)
                , date=rep('2017/01', 3))

x_02=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(7701, 7992, 981)
                , date=rep('2017/02', 3))

x_03=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(7819, 8058, 1022)
                , date=rep('2017/03', 3))

x_04=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(7887, 8090, 1040)
                , date=rep('2017/04', 3))

x_05=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8029, 8200, 1081)
                , date=rep('2017/05', 3))

x_06=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8092, 8353, 1108)
                , date=rep('2017/06', 3))

x_07=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8188, 8445, 1139)
                , date=rep('2017/07', 3))

x_08=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8247, 8605, 1182)
                , date=rep('2017/08', 3))

x_09=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8371, 8909, 1229)
                , date=rep('2017/09', 3))

x_10=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8427, 9040, 1252)
                , date=rep('2017/10', 3))

x_11=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8501, 9171, 1295)
                , date=rep('2017/11', 3))

x_12=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8532, 9218, 1354)
                , date=rep('2017/12', 3))

x_13=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8609, 9430, 1408)
                , date=rep('2018/01', 3))

x_14=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8803, 9633, 1465)
                , date=rep('2018/02', 3))

x_15=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(8970, 9936, 1522)
                , date=rep('2018/03', 3))

x_16=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9061, 10008, 1556)
                , date=rep('2018/04', 3))

x_17=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9115, 10057, 1622)
                , date=rep('2018/05', 3))

x_18=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9170, 10154, 1685)
                , date=rep('2018/06', 3))

x_19=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9229, 10238, 1744)
                , date=rep('2018/07', 3))

x_20=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9182, 10338, 1798)
                , date=rep('2018/08', 3))

x_21=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9257, 10490, 1866)
                , date=rep('2018/09', 3))

x_22=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9355, 10607, 1950)
                , date=rep('2018/10', 3))

x_23=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9365, 10684, 2023)
                , date=rep('2018/11', 3))

x_24=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9393, 10668, 2088)
                , date=rep('2018/12', 3))

x_25=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9442, 10788, 2153)
                , date=rep('2019/01', 3))

x_26=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9549, 11000, 2185)
                , date=rep('2019/02', 3))

x_27=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9606, 11060, 2263)
                , date=rep('2019/03', 3))

x_28=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9658, 11105, 2315)
                , date=rep('2019/04', 3))

x_29=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9709, 11147, 2377)
                , date=rep('2019/05', 3))

x_30=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9751, 11190, 2447)
                , date=rep('2019/06', 3))

x_31=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9811, 11246, 2505)
                , date=rep('2019/07', 3))

x_32=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9874, 11303, 2658)
                , date=rep('2019/08', 3))

x_33=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9931, 11438, 2864)
                , date=rep('2019/09', 3))

x_34=data.frame(Social=c("Twitter", "Facebook", "Instagram")
                , Followers=c(9993, 11520, 3000)
                , date=rep('2019/10', 3))

# Bind them into one
x_bind=rbind(x_01,
             x_02,
             x_03,
             x_04,
             x_05,
             x_06,
             x_07,
             x_08,
             x_09,
             x_10,
             x_11,
             x_12,
             x_13,
             x_14,
             x_15,
             x_16,
             x_17,
             x_18,
             x_19,
             x_20,
             x_21,
             x_22,
             x_23,
             x_24,
             x_25,
             x_26,
             x_27,
             x_28,
             x_29,
             x_30,
             x_31,
             x_32,
             x_33,
             x_34)

# Creating the index values for our final dataframe
index <- rep(1:34,each=3)

# Creating the final dataframe and rename the columns
df_x <- data.frame(index,x_bind[, 1], x_bind[, 2], x_bind[, 3])

names(df_x) <- c("Index", "Social", "Followers", "Dates")

df_x

#Plot and animate 

a <- ggplot(df_x, aes(x= Social, y= Followers, fill= Social)) +
  geom_bar(stat = "Identity") +
  labs(title = "Date: {closest_state}") +
  geom_text(aes(label = Followers, y = Followers),
            position = position_dodge(0.9), vjust = -1) +
  theme_classic() +
  transition_states(states = Dates, transition_length = 2, state_length = 2) +
  enter_fade() +
  exit_shrink() +
  ease_aes('sine-in-out')

a_gif <- animate(a, width = 700, height = 480)

a_gif

但是它不起作用,并显示此错误消息-

错误:只能合并相同类的工厂 另外:有24条警告(使用warnings()查看它们) 执行停止

我还尝试通过将图保存在本地目录中并将其读取到R Markdown报告中来进行此操作,但这也没有成功。

是否可以在R Markdown报告中添加GIF图?

0 个答案:

没有答案