IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Creating an animated Christmas tree in R

    The Jumping Rivers Blog发表于 2024-12-24 23:59:00
    love 0
    [This article was first published on The Jumping Rivers Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

    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!

    For updates and revisions to this article, see the original post

    To leave a comment for the author, please follow the link and comment on their blog: The Jumping Rivers Blog.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Continue reading: Creating an animated Christmas tree in R


沪ICP备19023445号-2号
友情链接