Skip to main content
AI in Production 2026 is now open for talk proposals.
Share insights that help teams build, scale, and maintain stronger AI systems.
items
Menu
  • About
    • Overview 
    • Join Us  
    • Community 
    • Contact 
  • Training
    • Overview 
    • Course Catalogue 
    • Public Courses 
  • Posit
    • Overview 
    • License Resale 
    • Managed Services 
    • Health Check 
  • Data Science
    • Overview 
    • Visualisation & Dashboards 
    • Open-source Data Science 
    • Data Science as a Service 
    • Gallery 
  • Engineering
    • Overview 
    • Cloud Solutions 
    • Enterprise Applications 
  • Our Work
    • Blog 
    • Case Studies 
    • R Package Validation 
    • diffify  

Creating an animated Christmas tree in R

Authors: Amieroh Abrahams & Osheen MacOscar

Published: December 24, 2024

tags: r, shiny

With Christmas tomorrow we have decided to create an animated Christmas Tree using {ggplot2}, {sf} and {gganimate}.

First we need a tree. To do this we have used an {sf} polygon where we pass in the coordinates of the Christmas tree as a list matrix to st_polygon. We can then use geom_sf to add this layer onto a ggplot object.

library(ggplot2)
library(gganimate)
library(sf)
tree_coords =
  list(
    matrix(
      c(-4, 0, 
        -2.22, 2, 
        -3.5, 2, 
        -1.5, 4, 
        -2.5, 4, 
        -0.8, 6, 
        -1.5, 6, 
        0, 8, 
        1.5, 6,
        0.8, 6, 
        2.5, 4,
        1.5, 4,
        3.5, 2,
        2.22, 2,
        4, 0,
        -4, 0),
      ncol=2, byrow=T
    )
  ) 

tree = st_polygon(tree_coords)

gg_tree = ggplot() + 
  geom_sf(aes(), data=tree) 

gg_tree
Christmas tree shape made with the sf and Ggplot2 R packages.

Okay, so now we have a tree shape. Now we need to make it a little more Christmassy by changing:

  • The color using: fill = "forestgreen", color = "darkgreen"
  • Adding the trunk: geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = -2, ymax = 0), fill = "saddlebrown", color = "sienna4")
  • Add a star on the top: geom_point(aes(x = 0, y = 8), color = "gold", shape = 8, size = 7, stroke = 3)
  • Remove the axis with: theme_void()
  • Set the border: coord_sf(xlim = c(-6, 6), ylim = c(-4, 10))
  • Add a Christmas message: annotate("text", x = 0, y = 9.5, label = "Merry Christmas \n From Jumping Rivers!", size = 6)

Now our tree looks like this:

gg_tree = ggplot() + 
  geom_sf(aes(), data=tree, fill = "forestgreen", color = "darkgreen") + 
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = -2, ymax = 0), fill = "saddlebrown", color = "sienna4") +
  geom_point(aes(x = 0, y = 8), color = "gold", shape = 8, size = 7, stroke = 3) +
  theme_void() +
  coord_sf(xlim = c(-6, 6), ylim = c(-4, 10)) +
  annotate("text", x = 0, y = 9.5, label = "Merry Christmas \n From Jumping Rivers!", size = 6) 

gg_tree
Green Christmas tree made with the Ggplot2 R package.

Next we need to use {sf} again to make some lights for the tree then {gganimate} to make the lights flash.

Placing the points within the boundaries of the tree was a trickier task than we expected until we fell upon st_sample which we can pass a polygon to and it’ll create some sample points within the boundaries. We also create a vector to colour the points.

points = st_sample(tree, 75)
colours = sample(c("red", "yellow", "blue"), 75, replace = TRUE)

gg_tree = ggplot() + 
  geom_sf(aes(), data=tree, fill = "forestgreen", color = "darkgreen") + 
  geom_sf(aes(), data=points, color = colours) +
  geom_rect(aes(xmin = -0.75, xmax = 0.75, ymin = -2, ymax = 0), fill = "saddlebrown", color = "sienna4") +
  geom_point(aes(x = 0, y = 8), color = "gold", shape = 8, size = 7, stroke = 3) +
  theme_void() +
  coord_sf(xlim = c(-6, 6), ylim = c(-4, 10)) +
  annotate("text", x = 0, y = 9.5, label = "Merry Christmas \n From Jumping Rivers!", size = 6) 
  
gg_tree
Christmas tree with lights made with the sf and Ggplot2 R packages.

We can now animate it to make the lights sparkle using transition_time and ease_aes:

gg_tree +
  transition_time(1:75) +
  ease_aes('linear')
Final Christmas tree GIF with sparkling lights.

Lastly, have a great Christmas and New Year from the Jumping Rivers team!


Jumping Rivers Logo

Recent Posts

  • Start 2026 Ahead of the Curve: Boost Your Career with Jumping Rivers Training 
  • Should I Use Figma Design for Dashboard Prototyping? 
  • Announcing AI in Production 2026: A New Conference for AI and ML Practitioners 
  • Elevate Your Skills and Boost Your Career – Free Jumping Rivers Webinar on 20th November! 
  • Get Involved in the Data Science Community at our Free Meetups 
  • Polars and Pandas - Working with the Data-Frame 
  • Highlights from Shiny in Production (2025) 
  • Elevate Your Data Skills with Jumping Rivers Training 
  • Creating a Python Package with Poetry for Beginners Part2 
  • What's new for Python in 2025? 

Top Tags

  • R (236) 
  • Rbloggers (182) 
  • Pybloggers (89) 
  • Python (89) 
  • Shiny (63) 
  • Events (26) 
  • Training (23) 
  • Machine Learning (22) 
  • Conferences (20) 
  • Tidyverse (17) 
  • Statistics (14) 
  • Packages (13) 

Authors

  • Amieroh Abrahams 
  • Aida Gjoka 
  • Shane Halloran 
  • Russ Hyde 
  • Myles Mitchell 
  • Keith Newman 
  • Gigi Kenneth 
  • Osheen MacOscar 
  • Tim Brock 
  • Sebastian Mellor 
  • Theo Roe 
  • Pedro Silva 
  • Colin Gillespie 

Keep Updated

Like data science? R? Python? Stan? Then you’ll love the Jumping Rivers newsletter. The perks of being part of the Jumping Rivers family are:

  • Be the first to know about our latest courses and conferences.
  • Get discounts on the latest courses.
  • Read news on the latest techniques with the Jumping Rivers blog.

We keep your data secure and will never share your details. By subscribing, you agree to our privacy policy.

Follow Us

  • GitHub
  • Bluesky
  • LinkedIn
  • YouTube
  • Eventbrite

Find Us

The Catalyst Newcastle Helix Newcastle, NE4 5TG
Get directions

Contact Us

  • hello@jumpingrivers.com
  • + 44(0) 191 432 4340

Newsletter

Sign up

Events

  • North East Data Scientists Meetup
  • Leeds Data Science Meetup
  • Shiny in Production
British Assessment Bureau, UKAS Certified logo for ISO 9001 - Quality management British Assessment Bureau, UKAS Certified logo for ISO 27001 - Information security management Cyber Essentials Certified Plus badge
  • Privacy Notice
  • |
  • Booking Terms

©2016 - present. Jumping Rivers Ltd