R使用'绘制动画;对于循环';及上市

R使用'绘制动画;对于循环';及上市,r,ggplot2,lapply,gganimate,R,Ggplot2,Lapply,Gganimate,我想做的是制作一个动画,让标签随着列名的变化而变化。所以,我的数据是 EXTRACTION<- structure(list(TICKERS = c("SPY", "IVV", "VTI"), X2020.11.02 = c(0, 0, 0), X2020.11.04 = c(-0.0110832443075151, -0.0258415332395525, -0.028852983646287), X2020.11.08 =

我想做的是制作一个动画,让标签随着列名的变化而变化。所以,我的数据是

EXTRACTION<-
structure(list(TICKERS = c("SPY", "IVV", "VTI"), X2020.11.02 = c(0, 
0, 0), X2020.11.04 = c(-0.0110832443075151, -0.0258415332395525, 
-0.028852983646287), X2020.11.08 = c(-0.00678564274087334, -0.028592196288115, 
-0.0287979806647458), X2020.11.11...5 = c(0.00362433932387574, 
-0.0183931126967061, -0.0159248566046306), X2020.11.11...6 = c(0.0475566216272374, 
-0.0043522045803146, -0.00278232172594206), X2020.11.17 = c(0.0210585321899657, 
-0.0175562162191414, -0.00540261044582935), X2020.11.19 = c(0.0574666424876265, 
0.00762503019098637, 0.0215144350034273), X2020.11.24 = c(0.0327027084626714, 
-0.00126684622955087, 0.00988617332076935), X2020.11.29 = c(0.0346871743474724, 
-0.00331008153617796, 0.0140402133559614), X2020.12.07 = c(0.0264324000235459, 
-0.00860290128787633, 0.00821403534252774), X2020.12.08...12 = c(0.0285459458362687, 
-0.00675657470727886, 0.00932080453971507)), row.names = c("SPY", 
"IVV", "VTI"), class = "data.frame")
(我更改了行名以更方便地使用数据。 事实上,真正的列表有数百个标记,但为了便于理解,我将省略其他标记。)

我已经成功地为ggplot动画编写了一段代码

CC <- sub(".", "", colnames(EXTRACTION))

a <- data.frame(ticker=c("SPY"), values=EXTRACTION[c("SPY"),c(2)], frame=rep(CC[2],3))
#trying to extract the data of SPY on the right date.
b <- data.frame(ticker=c("SPY"), values=EXTRACTION[c("SPY"),c(3)], frame=rep(CC[3],3))
c <- data.frame(ticker=c("SPY"), values=EXTRACTION[c("SPY"),c(4)], frame=rep(CC[4],3))
.....so on
data<-rbind(a,b,c)

library(gifski)


myPlot <- ggplot(data, aes(x=ticker,y=values, fill = ticker))+geom_bar(stat='identity')+theme_bw()+
transition_states(frame, transition_length = 6, state_length = 2)+ ease_aes('sine-in-out')+
  labs(title = 'sector: {closest_state}')
                                                                                                      

animate(myPlot, duration = 5, fps = 20, width = 1000, height = 800, renderer = gifski_renderer())

我如何循环代码?提前谢谢。

您使用lappy的方式不正确。查看
lappy

lappy返回一个与X长度相同的列表,其中的每个元素都是将FUN应用于X的相应元素的结果

使用lappy和在
lappy
中声明的函数时会出现一些混淆

但是我认为这是
tidyverse
包中的函数非常适合的任务(以不同的格式重新排列数据集)

因此,我认为向您展示如何使用
tidyverse
更有用。看看他们的教程。这将是非常有用的

使用它们的功能,您只需几行代码即可完成所需:

library(tidyverse)

data <- EXTRACTION %>%
  # convert to long format
  # this will take the data in all columns (except `TICKERS`) and "stack"" them vertically
  # it will also create another column with the dates where each data came from
  tidyr::pivot_longer(-TICKERS) %>%
  # fix names of dates (remove 'X') 
  dplyr::mutate(name=gsub('X', '', name, fixed = TRUE)) %>%
  # rename columns to match yours
  dplyr::rename(ticker=TICKERS, values=value, frame=name)


myPlot <- ggplot(data, aes(x=ticker,y=values, fill = ticker)) + 
  geom_bar(stat='identity')+theme_bw()+
  transition_states(frame, transition_length = 6, state_length = 2)+ ease_aes('sine-in-out')+
  labs(title = 'sector: {closest_state}')
库(tidyverse)
数据%
#转换为长格式
#这将获取所有列中的数据(除了“TICKERS”)并垂直“堆叠”
#它还将创建另一列,其中包含每个数据的来源日期
tidyr::pivot_更长(-TICKERS)%>%
#确定日期名称(删除“X”)
dplyr::mutate(name=gsub('X','',name,fixed=TRUE))%>%
#重命名列以匹配您的列
dplyr::重命名(ticker=ticker,values=value,frame=name)

myPlot%
操作符是一个“管道操作符”。它获取左侧的内容,并在右侧的函数中用作参数。因此,
x%>%f(y)
变成了
f(x,y)
,因此您可以使用它重写多个操作。

不再使用
pivot\u
似乎非常有用。谢谢!
lapply(letters[1:3],function(x){x<-data.frame(ticker=c("SPY"),for(j in 2:4)
{values=EXTRACTION[c("SPY"),c(j)],frame=rep(CC[j],3))}})
a<- .........,c(2).... rep(CC[2].....
b<- ........,c(3).... rep(CC[3].....
c<- .........,c(4).... rep(CC[4].....
library(tidyverse)

data <- EXTRACTION %>%
  # convert to long format
  # this will take the data in all columns (except `TICKERS`) and "stack"" them vertically
  # it will also create another column with the dates where each data came from
  tidyr::pivot_longer(-TICKERS) %>%
  # fix names of dates (remove 'X') 
  dplyr::mutate(name=gsub('X', '', name, fixed = TRUE)) %>%
  # rename columns to match yours
  dplyr::rename(ticker=TICKERS, values=value, frame=name)


myPlot <- ggplot(data, aes(x=ticker,y=values, fill = ticker)) + 
  geom_bar(stat='identity')+theme_bw()+
  transition_states(frame, transition_length = 6, state_length = 2)+ ease_aes('sine-in-out')+
  labs(title = 'sector: {closest_state}')