[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

Implications and Future Developments of Creating Animated Objects in R

The article discusses the process of creating an animated Christmas tree using R programming language’s libraries: {ggplot2}, {sf}, and {gganimate}. The process is conducted through a series of code demonstrative stages starting from creating a Christmas tree shape to adding final elements like a star on the tree top, and then putting on some lights to make it share the festive glow.

The Potential Future Developments

The technique discussed in the article might have deeper implications than it initially seems. The process shows developers how they can use R to create animated visuals, in this case, a glowing Christmas tree. This approach shows promise in expanding towards a more comprehensive animation graphics creation.

There is potential for similar concepts to be applied in visualising different types of data, transforming static graphs into interactive or dynamic displays. R libraries such as {ggplot2}, {sf}, and {gganimate} could be used not only to animate a logo or a greeting card but also complex geo-spatial data or time series.

Long-term implications

The transformation of complex data into dynamic visuals can significantly affect the way businesses perceive data and make data-driven decisions. Interactive and dynamic visuals can make it easier to understand trends and changes over time, relationships, and patterns. In addition, they could potentially enhance the quality of internal presentations, business reports, and public communications.

Actionable advice

Considering the above-mentioned implications, here are some steps businesses and developers can take:

  1. Leverage animation in data visualization: Businesses might consider investing in creating animated visualizations to communicate complex data in a simple, more engaging way to not only their internal staff but also clients and stakeholders.
  2. Incorporate R in the tech stack: Considering the flexibility and applicability of R in data visualization, it might be beneficial to incorporate R in your tech stack.
  3. Invest in upskilling: Businesses may contemplate investing in upskilling their data analysis teams in R, and libraries such as {ggplot2}, {sf}, and {gganimate}.
  4. Create interactive reports: Lastly, businesses can use animated visualizations to create interactive and dynamic reports, which can be a game-changer in presenting insights from data analysis.

Read the original article