在R中动画ggplot2堆积线图

时间:2015-01-19 16:39:37

标签: r animation plot ggplot2 linechart

我正在尝试在ggplot2中为叠加的折线图设置动画。

这是我想要制作动画的情节: enter image description here

这是生成类似情节的代码:

#Data
mydata <- data.frame(year=rep(1:6, times=4),
                 activity=as.factor(rep(c("research","coursework","clinical work","teaching"), each=6)),
                 time=c(40, 35, 40, 60, 85, 90,
                        50, 40, 10, 0, 5, 0,
                        5, 20, 20, 40, 10, 10,
                        5, 5, 30, 0, 0, 0))

mydata$activity <- ordered(mydata$activity, levels = c("research","clinical work","coursework","teaching"))

labels <- data.frame(activity=c("research","coursework","clinical work","teaching"),
                 xaxis=c(5, 1.8, 2.5, 2.97),
                 yaxis=c(25, 70, 48, 90))

#Plot
ggplot(mydata, aes(x=year, y=time, fill=activity)) +
  geom_area(stat="smooth", span=.35, color="black") +
  theme(legend.position = "none") +
  geom_text(data=labels, aes(x=xaxis, y=yaxis, label=activity)) +
  ggtitle("Time in Different Activities by Year in Program") +
  ylab("Percentage of Time") +
  xlab("Year in Program")

我正在寻找第一张显示所有轴和文字的图片。第二次迭代,我想逐渐从左到右揭示“研究”叠加线(包括颜色和边框)。第三次迭代,我想逐步从左到右揭示“临床工作”的叠加线。第四,“课程”叠加线。最后,“教学”叠加线。

理想情况下,输出格式非常流畅(没有锯齿状跳跃),并且与PowerPoint兼容。

2 个答案:

答案 0 :(得分:3)

这是一个基于R的解决方案。它保存了可以在演示文稿中迭代的单个数字(.png)。

或者,您可以使用ImageMagick创建动画(例如转换为.gif)http://www.imagemagick.org/

#Data
mydata <- data.frame(year=rep(1:6, times=4),
                     activity=as.factor(rep(c("research","coursework","clinical work","teaching"), each=6)),
                     time=c(40, 35, 40, 60, 85, 90,
                            50, 40, 10, 0, 5, 0,
                            5, 20, 20, 40, 10, 10,
                            5, 5, 30, 0, 0, 0))

#order the activities and then the dataframe
mydata$activity <- ordered(mydata$activity, levels = c("research","clinical work","coursework","teaching"))
mydata <- mydata[order(mydata$activity),]

#labels
labels <- data.frame(activity=c("research","coursework","clinical work","teaching"),
                     xaxis=c(5, 1.8, 2.5, 2.97),
                     yaxis=c(25, 70, 48, 90))

#creates a function to draws a plot for each activity
draw.stacks<-function(leg){
  int <- leg*6
  a<-ggplot(data=mydata[1:int,], aes(x=year, y=time, fill=activity))+
    geom_area(stat="smooth", span=.35, color="black") +
    theme_bw()+
    scale_fill_discrete(limits = c("research","clinical work","coursework","teaching"), guide="none")+
    theme(panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()) +
    coord_cartesian(xlim=c(1,6),ylim=c(0,100))+
    geom_text(data=labels, aes(x=xaxis, y=yaxis, label=activity)) +
    ggtitle("Time in Different Activities by Year in Program") +
    ylab("Percentage of Time") +
    xlab("Year in Program")  
  print(a)
}


# save individual png figures
for (i in 0:4) {
png(paste("activity", i, "png", sep="."))
  draw.stacks(i)
dev.off()
}

答案 1 :(得分:1)

很抱歉引入非程序员解决方案,但我会简单地为每次迭代生成绘图,将它们放在功率点(一张幻灯片上的一个绘图),并使用一些奇特的幻灯片过渡效果(我尝试了随机条)对你的例子有影响,看起来不错)。

如果您决定寻找基于R的解决方案,可以查看animate包(请参阅战略僵尸模拟示例here)。