gganimate动画
library(gganimate)
library(gapminder)
library(tidyverse)
#sudo apt install cargo
#install.packages("gifski")
data("gapminder")
glimpse(gapminder)
gapminder %>%
select(country, pop, year, continent) %>%
group_by(year) %>% # for each year we assign a rank
arrange(year, -pop) %>%
mutate(rank = 1:n()) %>% # assign ranking
filter(rank <= 10) ->
ranked_by_year
my_theme <- theme_classic(base_family = "Times") +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(axis.line.y = element_blank()) +
theme(legend.background = element_rect(fill = "linen")) +
theme(plot.background = element_rect(fill = "linen")) +
theme(panel.background = element_rect(fill = "linen"))
ggplot(data = ranked_by_year) +
aes(group = country, fill = continent) +
aes(xmin = 0 ,
xmax = pop / 1000000) +
aes(ymin = rank - .45,
ymax = rank + .45) +
scale_y_reverse() +
scale_x_continuous(
limits = c(-300, 1400),
breaks = c(0, 400, 800, 1200),
labels = c(0, 400, 800, 1200)) +
labs(fill = "") +
geom_rect(alpha = .7) +
labs(x = 'Population (millions)') +
aes(label = country, y = rank) +
geom_text(col = "gray13",
hjust = "right",
x = -50) +
labs(y = "") +
scale_fill_viridis_d(option = "magma",
direction = -1) +
geom_text(x = 1000 , y = -10,
family = "Times",
aes(label = as.character(year)),
size = 30, col = "grey18") +
my_theme ->
g
g + gganimate::transition_time(year)
# transition_*(): 定义动画是根据哪个变量进行”动”.
# view_*(): 定义标尺的位置.
# shadow_*(): 影子?定义点相继出现的方式.
# enter_*()/exit_*(): 定义新数据产生和旧数据删除的方式.
# ease_aes(): 美观定义(如何让整个动画看起来更舒适).
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
# Here comes the gganimate code
transition_states(
gear,
transition_length = 2,state_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop,
colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}',
x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
ggplot(mtcars) +
geom_boxplot(aes(factor(cyl), mpg)) +
transition_manual(gear)
ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, color=continent)) +
geom_point() +
scale_x_log10()+
labs(title = 'Year: {frame_time}',
x = 'GDP per capita', y = 'life expectancy') +
transition_time(year)
theme_set(theme_bw(base_size=16))
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
# Here comes the gganimate code
labs(title = '{previous_state}', x = 'GDP per capita', y = 'life expectancy') +
transition_states(
continent,
transition_length = 2,
state_length = 1
)
#动画保存
anim_save(filename = "test.gif", animation = last_animation())
anim_save(filename = "test.mp4", animation = last_animation())
#传染病动画
library(gapminder)
library(rgdal)
GD<- readOGR("Data/guangdong.shp")
data10 <- read.csv("Data/animation.csv",header = T)
p = ggplot(GD)+
geom_point(data= data10, aes(x = long, y = lat, colour=as.factor(type)), size=2) +
geom_polygon( aes(x = long, y = lat, group =group), size=0.4,
colour = "BLACK", fill = "transparent") +
coord_map(project="conic", lat0 = 30) +
theme_bw(base_size = 15) +
# Here comes the gganimate specific bits
lab(title = 'Disease in Guangdong Province in: {frame_time}', x = '', y = '') +
transition_time(onset_date) +
ease_aes('linear')
library(gapminder)
library(tidyverse)
#sudo apt install cargo
#install.packages("gifski")
data("gapminder")
glimpse(gapminder)
gapminder %>%
select(country, pop, year, continent) %>%
group_by(year) %>% # for each year we assign a rank
arrange(year, -pop) %>%
mutate(rank = 1:n()) %>% # assign ranking
filter(rank <= 10) ->
ranked_by_year
my_theme <- theme_classic(base_family = "Times") +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.y = element_blank()) +
theme(axis.line.y = element_blank()) +
theme(legend.background = element_rect(fill = "linen")) +
theme(plot.background = element_rect(fill = "linen")) +
theme(panel.background = element_rect(fill = "linen"))
ggplot(data = ranked_by_year) +
aes(group = country, fill = continent) +
aes(xmin = 0 ,
xmax = pop / 1000000) +
aes(ymin = rank - .45,
ymax = rank + .45) +
scale_y_reverse() +
scale_x_continuous(
limits = c(-300, 1400),
breaks = c(0, 400, 800, 1200),
labels = c(0, 400, 800, 1200)) +
labs(fill = "") +
geom_rect(alpha = .7) +
labs(x = 'Population (millions)') +
aes(label = country, y = rank) +
geom_text(col = "gray13",
hjust = "right",
x = -50) +
labs(y = "") +
scale_fill_viridis_d(option = "magma",
direction = -1) +
geom_text(x = 1000 , y = -10,
family = "Times",
aes(label = as.character(year)),
size = 30, col = "grey18") +
my_theme ->
g
g + gganimate::transition_time(year)
# transition_*(): 定义动画是根据哪个变量进行”动”.
# view_*(): 定义标尺的位置.
# shadow_*(): 影子?定义点相继出现的方式.
# enter_*()/exit_*(): 定义新数据产生和旧数据删除的方式.
# ease_aes(): 美观定义(如何让整个动画看起来更舒适).
ggplot(mtcars, aes(factor(cyl), mpg)) +
geom_boxplot() +
# Here comes the gganimate code
transition_states(
gear,
transition_length = 2,state_length = 1) +
enter_fade() +
exit_shrink() +
ease_aes('sine-in-out')
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop,
colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
facet_wrap(~continent) +
# Here comes the gganimate specific bits
labs(title = 'Year: {frame_time}',
x = 'GDP per capita', y = 'life expectancy') +
transition_time(year) +
ease_aes('linear')
ggplot(mtcars) +
geom_boxplot(aes(factor(cyl), mpg)) +
transition_manual(gear)
ggplot(gapminder, aes(gdpPercap, lifeExp, size=pop, color=continent)) +
geom_point() +
scale_x_log10()+
labs(title = 'Year: {frame_time}',
x = 'GDP per capita', y = 'life expectancy') +
transition_time(year)
theme_set(theme_bw(base_size=16))
ggplot(gapminder, aes(gdpPercap, lifeExp, size = pop, colour = country)) +
geom_point(alpha = 0.7, show.legend = FALSE) +
scale_colour_manual(values = country_colors) +
scale_size(range = c(2, 12)) +
scale_x_log10() +
# Here comes the gganimate code
labs(title = '{previous_state}', x = 'GDP per capita', y = 'life expectancy') +
transition_states(
continent,
transition_length = 2,
state_length = 1
)
#动画保存
anim_save(filename = "test.gif", animation = last_animation())
anim_save(filename = "test.mp4", animation = last_animation())
#传染病动画
library(gapminder)
library(rgdal)
GD<- readOGR("Data/guangdong.shp")
data10 <- read.csv("Data/animation.csv",header = T)
p = ggplot(GD)+
geom_point(data= data10, aes(x = long, y = lat, colour=as.factor(type)), size=2) +
geom_polygon( aes(x = long, y = lat, group =group), size=0.4,
colour = "BLACK", fill = "transparent") +
coord_map(project="conic", lat0 = 30) +
theme_bw(base_size = 15) +
# Here comes the gganimate specific bits
lab(title = 'Disease in Guangdong Province in: {frame_time}', x = '', y = '') +
transition_time(onset_date) +
ease_aes('linear')
评论
发表评论