R 设置轴值(在动画中)

R 设置轴值(在动画中),r,ggplot2,gganimate,R,Ggplot2,Gganimate,如何在动画期间停止Y轴的更改 我画的图表是在 这个想法是制作一张每年人口和收入的动态热图。问题是y轴有时会跳到包含0或不包含最高值。如何牢固地设置轴值?我知道这一定是个常见的问题,但我找不到答案 重新创建它的代码是 library(gapminder) library(ggplot2) library(devtools) install_github("dgrtwo/gganimate") library(gganimate) library(dplyr) mydata <- dplyr

如何在动画期间停止Y轴的更改

我画的图表是在

这个想法是制作一张每年人口和收入的动态热图。问题是y轴有时会跳到包含0或不包含最高值。如何牢固地设置轴值?我知道这一定是个常见的问题,但我找不到答案

重新创建它的代码是

library(gapminder)
library(ggplot2)
library(devtools)
install_github("dgrtwo/gganimate")
library(gganimate)

library(dplyr)
mydata <- dplyr::select(gapminder, country,continent,year,lifeExp,pop,gdpPercap)
#bin years into 5 year bins
mydata$lifeExp2 <- as.integer(round((mydata$lifeExp-2)/5)*5)

mydata$income <- cut(mydata$gdpPercap,  breaks=c(0,250,500,750,1000,1500,2000,2500,3000,3500,4500,5500,6500,7500,9000,11000,21000,31000,41000, 191000),
                             labels=c(0,250,500,750,1000,1500,2000,2500,3000,3500,4500,5500,6500,7500,9000,11000,21000,31000,41000))

sizePer <- mydata%>%
    group_by(lifeExp2, income, year)%>%
    mutate(popLikeThis = sum(pop))%>%
  group_by(year)%>%
      mutate(totalPop = sum(as.numeric(pop)))%>%
    mutate(per = (popLikeThis/totalPop)*100)

sizePer$percent <- cut(sizePer$per,  breaks=c(0,.1,.3,1,2,3,5,10,20,Inf),
                             labels=c(0,.1,.3,1,2.0,3,5,10,20))

saveGIF({
    for(i in c(1997,2002,2007)){
        print(ggplot(sizePer %>% filter(year == i),
     aes(x = lifeExp2, y = income)) +
      geom_tile(aes(fill = percent)) +
                  theme_bw()+
                    theme(legend.position="top", plot.title = element_text(size=30, face="bold",hjust = 0.5))+
                    coord_cartesian(xlim = c(20,85), ylim = c(0,21)) +
  scale_fill_manual("%",values = c("#ffffcc","#ffeda0","#fed976","#feb24c","#fd8d3c","#fc4e2a","#e31a1c","#bd0026","#800026"),drop=FALSE)+
annotate(x=80, y=3, geom="text", label=i, size = 6) +
                  annotate(x=80, y=1, geom="text", label="@iamreddave", size = 5) +
   ylab("Income") +   # Remove x-axis label
                  xlab("Life Expenctancy")+
                  ggtitle("Worldwide Life Expectancy and Income")          

        )}
}, interval=0.7,ani.width = 900, ani.height = 600)
库(gapminder)
图书馆(GG2)
图书馆(devtools)
安装github(“dgrtwo/gganimate”)
库(gganimate)
图书馆(dplyr)
mydata%
变异(totalPop=sum(作为.数字(pop)))%>%
变异(per=(popLikeThis/totalPop)*100)
sizePer$percent%过滤器(年份==i),
aes(x=寿命支出2,y=收入))+
地砖(aes(填充率=百分比))+
主题_bw()+
主题(legend.position=“top”,plot.title=元素\文本(size=30,face=“bold”,hjust=0.5))+
坐标笛卡尔(xlim=c(20,85),ylim=c(0,21))+
比例填充手册(“%”,值=c(“#ffffcc”、“#ffeda0”、“#fed976”、“#feb24c”、“#fd8d3c”、“#fc4e2a”、“#e31a1c”、“#bd0026”、“#800026”、“drop=FALSE)+
注释(x=80,y=3,geom=“text”,label=i,size=6)+
注释(x=80,y=1,geom=“text”,label=“@iamreddave”,size=5)+
ylab(“收入”)+#移除x轴标签
xlab(“生活体验”)+
ggtitle(“全球预期寿命和收入”)
)}
},间距=0.7,ani.宽度=900,ani.高度=600)
解决方案:


向ggplot调用添加
scale\u y\u discrete(drop=F)
。在评论中由@bdemarest回答。

尝试将
scale\u y\u discrete(drop=F)
添加到ggplot调用中?因为收入是一个因素&水平“0”并不是每年都出现。这是有效的。谢谢你的帮助!