动态图
2019-08-16 本文已影响0人
Whuer_deng
转自gganimate: How to Create Plots with Beautiful Animation in R
library(ggplot2)
library(gganimate)
theme_set(theme_bw())
library(gapminder)
head(gapminder)
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

p + transition_time(year) +
labs(title = "Year: {frame_time}")

p + facet_wrap(~continent) +
transition_time(year) +
labs(title = "Year: {frame_time}")

p + transition_time(year) +
labs(title = "Year: {frame_time}") +
view_follow(fixed_y = TRUE)

p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_wake(wake_length = 0.1, alpha = FALSE)

p + transition_time(year) +
labs(title = "Year: {frame_time}") +
shadow_mark(alpha = 0.3, size = 0.5)

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

p + transition_reveal(Day)

p +
geom_point() +
transition_reveal(Day)

p +
geom_point(aes(group = seq_along(Day))) +
transition_reveal(Day)

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
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

p + transition_states(Month, wrap = FALSE) +
shadow_mark()
