原文鏈接:http:///?p=8003
演示數(shù)據(jù)集
library(gapminder)
head(gapminder)
## # A tibble: 6 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.`
靜態(tài)圖
p <- ggplot(
gapminder,
aes(x = gdpPercap, y=lifeExp, size = pop, colour = country)
) +
geom_point(show.legend = FALSE, alpha = 0.7) +
scale_color_viridis_d() +
scale_size(range = c(2, 12)) +
scale_x_log10() +
labs(x = "GDP per capita", y = "Life expectancy")
p
-
基本
狀態(tài)之間的過渡長度將設(shè)置為與它們之間的實際時間差相對應(yīng)。
標(biāo)簽變量:frame_time
。給出當(dāng)前幀所對應(yīng)的時間。
創(chuàng)建面板:
讓視圖跟隨數(shù)據(jù)在每幀中變化
逐步衰減
顯示原始數(shù)據(jù)作為背景
您可以根據(jù)需要顯示過去和/或?qū)淼脑紨?shù)據(jù)并設(shè)置其樣式。
-
靜態(tài)圖
p <- ggplot(
airquality,
aes(Day, Temp, group = Month, color = factor(Month))
) +
geom_line() +
scale_color_viridis_d() +
labs(x = "Day of Month", y = "Temperature") +
theme(legend.position = "top")
p
讓數(shù)據(jù)逐漸出現(xiàn)
按天顯示(x軸)
在數(shù)據(jù)的幾個不同階段之間進行轉(zhuǎn)換
數(shù)據(jù)準(zhǔn)備:
library(dplyr)
mean.temp <- airquality %>%
group_by(Month) %>%
summarise(Temp = mean(Temp))
mean.temp
## # A tibble: 5 x 2
## Month Temp
## <int> <dbl>
## 1 5 65.5
## 2 6 79.1
## 3 7 83.9
## 4 8 84.0
## 5 9 76.9`
創(chuàng)建平均溫度的條形圖:
p <- ggplot(mean.temp, aes(Month, Temp, fill = Temp)) +
geom_col() +
scale_fill_distiller(palette = "Reds", direction = 1) +
theme_minimal() +
theme(
panel.grid = element_blank(),
panel.grid.major.y = element_line(color = "white"),
panel.ontop = TRUE
)
p
transition_states():
enter_grow()+ enter_fade()
保存動畫
如果需要保存動畫以備后用,可以使用該anim_save()
功能。