Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用gganimate获得完整而非部分饼图_R_Animation_Ggplot2_Pie Chart_Gganimate - Fatal编程技术网

R 如何使用gganimate获得完整而非部分饼图

R 如何使用gganimate获得完整而非部分饼图,r,animation,ggplot2,pie-chart,gganimate,R,Animation,Ggplot2,Pie Chart,Gganimate,我在使用gganimate和ggplot制作动画饼图时遇到问题 我希望每年都有正常的馅饼,但我的产量完全不同 您可以使用mtcars查看代码示例: library(ggplot2) library(gganimate) #Some Data df<-aggregate(mtcars$mpg, list(mtcars$cyl,mtcars$carb), sum) colnames(df)<-c("X","Y","Z") bp<- ggplot(df, aes(x="",

我在使用
gganimate
ggplot
制作动画饼图时遇到问题

我希望每年都有正常的馅饼,但我的产量完全不同

您可以使用
mtcars
查看代码示例:

library(ggplot2)
library(gganimate)


#Some Data

df<-aggregate(mtcars$mpg, list(mtcars$cyl,mtcars$carb), sum)
colnames(df)<-c("X","Y","Z")

bp<- ggplot(df, aes(x="", y=Z, fill=X, frame=Y))+
geom_bar(width = 1, stat = "identity") + coord_polar("y", start=0)

gganimate(pie, "output.gif")
库(ggplot2)
库(gganimate)
#一些数据

dfggplot代码创建一个单一的堆叠条形图,其中
df
中的每一行都有一个部分。使用
coord_polar
这将成为一个饼图,数据框中的每一行都有一个楔形。然后,当您使用
gg_animate
时,每个帧仅包括与给定级别的
Y
相对应的楔块。这就是为什么每次只能得到完整饼图的一部分

相反,如果您希望为每个级别的
Y
创建一个完整的饼图,那么一个选项是为每个级别的
Y
创建一个单独的饼图,然后将这些饼图组合成GIF。下面是一个例子,其中有些虚假数据(我希望)与您的真实数据相似:

library(animation)

# Fake data
set.seed(40)
df = data.frame(Year = rep(2010:2015, 3), 
                disease = rep(c("Cardiovascular","Neoplasms","Others"), each=6),
                count=c(sapply(c(1,1.5,2), function(i) cumsum(c(1000*i, sample((-200*i):(200*i),5))))))

saveGIF({
  for (i in unique(df$Year)) {
    p = ggplot(df[df$Year==i,], aes(x="", y=count, fill=disease, frame=Year))+
      geom_bar(width = 1, stat = "identity") + 
      facet_grid(~Year) +
      coord_polar("y", start=0) 
    print(p)
  }
}, movie.name="test1.gif")

上面GIF中的馅饼大小都一样。但是,您也可以根据
年份的每个级别的
计数之和更改馅饼的大小(代码改编自):

如果我能编辑一下,虽然动画很酷(但饼图不酷,所以可能动画一堆饼图只是雪上加霜),但使用简单的静态线条图,数据可能更容易理解。例如:

ggplot(df, aes(x=Year, y=count, colour=disease)) +
  geom_line() + geom_point() +
  scale_y_continuous(limits=c(0, max(df$count)))

或者可能是这样:

ggplot(df, aes(x=Year, y=count, colour=disease)) +
  geom_line() + geom_point(show.legend=FALSE) +
  geom_line(data=df %>% group_by(Year) %>% mutate(count=sum(count)), 
            aes(x=Year, y=count, colour="All"), lwd=1) +
  scale_y_continuous(limits=c(0, df %>% group_by(Year) %>% 
                                summarise(count=sum(count)) %>% max(.$count))) +
  scale_colour_manual(values=c("black", hcl(seq(15,275,length=4)[1:3],100,65)))

谢谢!很不错的。谢谢你的推荐,但我需要我想要的动画饼图。我唯一担心的是动画包。我想直接用gganimate来做。但谢谢你的解决方案。我同意用动画制作一堆饼图,这只会增加伤害。我想你需要在写这篇文章之前多了解一点我在做什么。。。但当然,你什么都知道……对不起@Essberto,我只能猜测你没有写什么。饼图不是我的拿手好戏,你可以复制我的全部代码。我使用了
mtcars
来创建数据。也许我的英语不好,但在我的问题中,我要求的是第一个绘图的解决方案,而不是第二个绘图的解决方案。第二个是只展示它在一个级别上是如何工作的。。。我不知道你对我的工作有什么问题。你不知道我在用这些图形和数据做什么,以及它们将如何形成。但当然,你知道EveruthingOrry@Essberto我一开始其实没有听懂——可能是因为我的英语也不完美。我不是假装什么都知道,我只是想引导你让更多的人来帮助你:当我筛选你的问题时,我认为你对第二个只展示一年的情节有意见,而第一个看起来很不错,因为我无法复制第二个,所以我只留下了我的第一条评论。如果你觉得这很粗鲁,我真诚地道歉。
ggplot(df, aes(x=Year, y=count, colour=disease)) +
  geom_line() + geom_point(show.legend=FALSE) +
  geom_line(data=df %>% group_by(Year) %>% mutate(count=sum(count)), 
            aes(x=Year, y=count, colour="All"), lwd=1) +
  scale_y_continuous(limits=c(0, df %>% group_by(Year) %>% 
                                summarise(count=sum(count)) %>% max(.$count))) +
  scale_colour_manual(values=c("black", hcl(seq(15,275,length=4)[1:3],100,65)))