动态图

转自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
image.png
p + transition_time(year) +
  labs(title = "Year: {frame_time}")
1.gif
p + facet_wrap(~continent) +
  transition_time(year) +
  labs(title = "Year: {frame_time}")
2.gif
p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  view_follow(fixed_y = TRUE)
view_follow.gif
p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_wake(wake_length = 0.1, alpha = FALSE)
shadow_wake.gif
p + transition_time(year) +
  labs(title = "Year: {frame_time}") +
  shadow_mark(alpha = 0.3, size = 0.5)
shadow_mark.gif
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
gganimate-line-plot-1.png
p + transition_reveal(Day)
let-data-gradually-appear.gif
p + 
  geom_point() +
  transition_reveal(Day)
let-data-gradually-appear-show-points.gif
p + 
  geom_point(aes(group = seq_along(Day))) +
  transition_reveal(Day)
let-data-gradually-appear-keep-points.gif
library(dplyr)
mean.temp <- airquality %>%
  group_by(Month) %>%
  summarise(Temp = mean(Temp))
mean.temp
## # A tibble: 5 x 2
##   Month  Temp
##    
## 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
image.png
p + transition_states(Month, wrap = FALSE) +
  shadow_mark()
transition_states.gif

你可能感兴趣的:(动态图)