Skip to content

Instantly share code, notes, and snippets.

@daijiang
Forked from djj4tree/Data_animate_gganimate.R
Last active June 27, 2019 18:41
Show Gist options
  • Save daijiang/673bd8702388cbd6a9588947b0a2b159 to your computer and use it in GitHub Desktop.
Save daijiang/673bd8702388cbd6a9588947b0a2b159 to your computer and use it in GitHub Desktop.
Animate tree data collection gganimate R code
# snippet of code to animate data in order of collection
# a little bit of data to show how it works
df <- structure(list(sp = structure(c(2L, 2L, 3L, 1L, 1L, 3L, 1L, 3L,
2L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 1L, 2L, 3L, 3L, 1L, 3L, 3L, 1L,
3L, 3L, 3L, 3L, 1L, 3L, 1L, 1L, 3L, 2L, 3L, 2L, 1L, 1L, 2L, 2L,
1L, 3L, 1L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 3L, 2L,
3L, 3L, 2L, 2L, 3L, 2L, 1L, 1L, 3L, 2L, 3L), .Label = c("D",
"O", "P"), class = "factor"), dbh = c(19.36, 20.74, 10.13, 10.47,
10.34, 14.22, 10.17, 22.91, 6.58, 7.93, 14.44, 6.67, 20.93, 7.16,
12.2, 21.26, 13.77, 15.24, -0.04, 17.15, 22.83, 11.82, 16.43,
20.69, 30.45, 11.83, 8.95, 15.42, 5.81, 8.85, 21.18, 22.46, 8.43,
13.03, 19.16, 5.16, 13.07, 14.55, 16.06, 22.2, 17.8, 15.93, 15.42,
14.24, 12.22, 13.17, 15.74, 16.13, 18.02, 4.57, 9.55, 9.95, 19.61,
13.38, 12.04, 3.7, 16.78, 0.47, 6.25, 18.63, 5.77, 16.83, 11.05,
15.77, 13.63, 16.63, 16.42), x = c(0.2, 0.85, 1.13, 3.13, 3.65,
6.27, 8.94, 7.78, 6.55, 10.26, 10.26, 11.66, 10.8, 10.54, 10.82,
3.16, 8.94, 15.85, 16.5, 9.36, 18.82, 19.54, 16.93, 17.6, 13.01,
17.87, 7.69, 15.14, 13.83, 1.04, 9.55, 18.36, 8.88, 11.2, 13.33,
14.88, 4.06, 18.65, 13.89, 18.26, 10.41, 12.82, 8.71, 10.92,
9, 5.52, 4.92, 1.33, 1.67, 1.62, 1.11, -0.1, -5.57, -10.56, -16.92,
-16.45, -18.06, -18.74, -11.96, -18.27, -15.2, -11.13, -6.93,
-8, -3.26, -15.15, -3.38), y = c(7.62, 6.91, 6.76, 13.04, 9.26,
14.42, 20.55, 16.32, 13.13, 20.15, 20.15, 21.47, 19.1, 18.63,
16.35, 4.68, 11.44, 19.92, 18.98, 10.96, 19.49, 19.54, 16.64,
17.3, 12.35, 13.22, 5.49, 10.6, 8.31, 0.61, 4.15, 6.32, 1.65,
1.88, 1.52, -3.57, -1.52, -7.35, -7.23, -11.19, -8.28, -10.56,
-10.76, -13.97, -14.13, -15.58, -15.59, -7.19, -17.33, -16.85,
-18.18, -0.72, -18.21, -17.93, -22.06, -14.55, -13.85, -11.94,
-4.47, -5.24, 3.65, 2.67, 1.99, 5.1, 2.24, 15.41, 6.5), stat = c(19,
19, 19, 1, 19, 19, 19, 1, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 1, 19, 19, 19, 1, 19, 1,
19, 1, 19, 19, 1, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19)), class = "data.frame", row.names = c(NA, -67L))
# necessary packages
library(ggplot2)
library(gganimate)
p <- ggplot(df, aes(x=x, y=y, colour = sp, group = seq_along(x))) + # The key is the seq_along to go line by line
geom_point(size=df$dbh/5, shape=df$stat, show.legend = FALSE) + # Scale the point and choose point type (live versus dead)
scale_color_viridis_d(option = "D", direction = -1) + # pick your favorite color scheme
labs(x = "", y = "") +
gganimate::transition_reveal(cnt)
p1 <- animate(p, duration = 2) # duration determines how long it takes to animate in seconds
p1
# anim_save() function can save this as a gif along with the gifski package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment