library(tidyverse) reisijad=read_csv("http://www.tlu.ee/~jaagup/andmed/muu/estonia_reisijad.txt") library(gganimate) library(gifski) library(png) reisijad %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) reisijad %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% ggplot(aes(mehi, naisi, label=Country))+geom_text() reisijad %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% ggplot(aes(mehi, naisi, label=Country))+geom_text() reisijad %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% ggplot(aes(mehi, naisi, label=Country))+geom_text()+ transition_states(Country) reisijad %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% ggplot(aes(mehi, naisi, label=Country))+geom_point()+ transition_states(Country) jarjekord=reisijad %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% arrange(mehi) %>% .$Country reisijad %>% mutate(Country=factor(Country, jarjekord)) %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% ggplot(aes(mehi, naisi, label=Country))+geom_point()+ transition_states(Country, state_length=5, transition_length = 10)+ ggtitle("Kaader {frame}") reisijad %>% ggplot(aes(Sex))+ geom_bar()+ transition_time(Age, range = c(20, 80)) reisijad %>% ggplot(aes(Sex))+geom_bar() reisijad %>% filter(Age>40) %>% ggplot(aes(Sex))+geom_bar() reisijad %>% filter(Age<=40) %>% ggplot(aes(Sex))+geom_bar() saveGIF({ print(reisijad %>% filter(Age>40) %>% ggplot(aes(Sex))+geom_bar()) print(reisijad %>% filter(Age<=40) %>% ggplot(aes(Sex))+geom_bar()) }) saveGIF({ print(reisijad %>% filter(Age>40) %>% ggplot(aes(Sex))+geom_bar()) print(reisijad %>% filter(Age<=40) %>% ggplot(aes(Sex))+geom_bar()) }, movie.name="E:/jaagup/R/1001/video2.gif") saveGIF({ for(vanus in 10:60){ print(reisijad %>% filter(Age<=vanus) %>% ggplot(aes(Sex))+geom_bar()+ ylim(0, 700)+ggtitle(paste("kuni ", vanus))) } }, movie.name="E:/jaagup/R/1001/video2.gif") #Näidake tulpi vaid vanuse kümneaastases aknas # 0-10, 1-11, 2-12 saveGIF({ for(vanus in 10:max(reisijad$Age)){ print(reisijad %>% filter(Age<=vanus, Age>=vanus-10) %>% ggplot(aes(Sex))+geom_bar()+ ylim(0, 200)+ggtitle(paste(vanus-10, " kuni ", vanus))) } }, movie.name="E:/jaagup/R/1001/video2.gif", interval=0.5) saveGIF({ for(nr in 1:20){ print(ggplot()+xlim(0, 5)+ylim(0, 200)+ annotate("text", 2.5, 100, label="Sugu ja vanus", size=4)) } for(vanus in 10:max(reisijad$Age)){ print(reisijad %>% filter(Age<=vanus, Age>=vanus-10) %>% ggplot(aes(Sex))+geom_bar()+ ylim(0, 200)+ggtitle(paste(vanus-10, " kuni ", vanus))) } for(nr in 1:20){ print(reisijad %>% filter(Age<=vanus, Age>=vanus-10) %>% ggplot(aes(Sex))+geom_bar()+ ylim(0, 200)+ggtitle(paste(vanus-10, " kuni ", vanus))) } }, movie.name="E:/jaagup/R/1001/video2.gif", interval=0.1) #Riikide kaupa vanusevahemikus inimesi #Riigi nimi paikneb xy tasandil vastavalt meeste ja naiste arvule #sellest riigist. Andmeid vaadatakse läbi muutuva vanuseakna saveGIF({ for(vanus in 10:max(reisijad$Age)){ print(reisijad %>% filter(Age<=vanus, Age>=vanus-10) %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% ggplot(aes(mehi, naisi, label=Country))+geom_text()+ xlim(0, 100)+ylim(0, 100)+ggtitle(paste(vanus-10, " kuni ", vanus))) }}, movie.name="E:/jaagup/R/1001/video3.gif", interval=0.1 ) riigiti=reisijad %>% group_by(Country) %>% summarise(kogus=n()) %>% filter(kogus>10) saveGIF({ for(vanus in 20:max(reisijad$Age)){ print(reisijad %>% filter(Age<=vanus, Age>=vanus-20) %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% inner_join(riigiti) %>% mutate(mosa=mehi/kogus, nosa=naisi/kogus) %>% ggplot(aes(mosa, nosa, label=Country))+geom_text()+ xlim(0, 0.5)+ylim(0, 0.5)+ggtitle(paste(vanus-20, " kuni ", vanus))) }}, movie.name="E:/jaagup/R/1001/video4.gif", interval=0.5 ) vanus=50 reisijad %>% filter(Age<=vanus, Age>=vanus-10) %>% group_by(Country) %>% summarise(mehi=sum(Sex=="M"), naisi=sum(Sex=="F")) %>% inner_join(riigiti) %>% mutate(mosa=mehi/kogus, nosa=naisi/kogus)