Christmas tree

So it is Christmas and I want a tree. I found this new package experimental ggfx and I thought it would be nice to use it for the lights. Then some time later I found (through twitter) this other repo about building trees.

So here I am building my own christmas tree!

The very basics

We start with installations:

devtools::install_github('thomasp85/ggfx')

We’ll build our tree with triangles:

library("ggplot2")
ggplot(data.frame(x = c(-5, 5, 0), y = c(0, 0, 5))) +
  geom_polygon(aes(x, y), fill = "green") +
  theme_void()

To make it easier we use some functions for the triangles:

iso_triangle <- function(wide, height, xvertix, yvertix) {
  y <- c(rep(yvertix-height, 2), yvertix)
  x <- c(xvertix+wide/2, xvertix-wide/2, xvertix)
  data.frame(x, y)
}

square <- function(length, x_bottom_right, y_bottom_right) {
  x <- rep(c(x_bottom_right-length, x_bottom_right), 2)
  y <- rep(c(y_bottom_right, y_bottom_right+length), each = 2)
  data.frame(x, y)[c(1, 3, 4, 2), ]
}

And off to build a tree:

tree <- ggplot() +
  aes(x, y) +
  geom_polygon(data = square(0.5, 0.25, 0.25), fill = "brown") +
  geom_polygon(data = iso_triangle(10, 4, 0, 4.5), fill = "darkgreen") + 
  geom_polygon(data = iso_triangle(9, 4, 0, 4.5), fill = "lightgreen") + 
  geom_polygon(data = iso_triangle(8, 3, 0, 6), fill = "green") +
  geom_polygon(data = iso_triangle(7, 3, 0, 6), fill = "darkgreen") +
  geom_polygon(data = iso_triangle(6, 2.5, 0, 7), fill = "lightgreen") + 
  geom_polygon(data = iso_triangle(5, 2.5, 0, 7), fill = "green") + 
  geom_polygon(data = iso_triangle(4, 1.5, 0, 7.5), fill = "darkgreen") +
  geom_polygon(data = iso_triangle(3, 1.5, 0, 7.5), fill = "lightgreen") +
  geom_polygon(data = iso_triangle(2, 1.5, 0, 8), fill = "green")
tree

The tree looks good enough, but if you disagree or want some more realistic trees see this challenge. Very funny to play with it (but I didn’t manage to build my tree with it :/). Anyway, now we have the tree and we need to turn them to a glowing tree!!

Decorating the tree

Now it is time to decorate and turn on the lights with the help of ggfx:

library("ggfx")
tree + 
  with_blur(geom_point(data =data.frame(x = 0, y = 8), size = 3, col = "yellow"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = c(2, -3), y = c(1.5, 1)), size = 3, col = "darkgreen"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = c(2, -3), y = 2.5+c(1, 1.2)), size = 3, col = "purple"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = -5, y = 0.5), size = 3, col = "red"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = 5, y = 0.5), size = 3, col = "orange"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = -2.5, y = 4), size = 3, col = "red"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = 3, y = 3.5), size = 3, col = "black"), sigma = 2, stack = TRUE) + 
  with_blur(geom_point(data =data.frame(x = c(-1, 1.25), y = c(5, 5.5)), size = 3, col = "orange"), sigma = 2, stack = TRUE) + 
  theme_void() +
  theme(plot.background = element_rect(fill = "black")) +
  NULL

And now we have our Christmas tree decorated!

Happy Christmas!!

Reproducibility

## ─ Session info ───────────────────────────────────────────────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 3.6.1 (2019-07-05)
##  os       Ubuntu 19.10                
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       Europe/Madrid               
##  date     2019-12-19                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────────────────────────────────────────────
##  package     * version    date       lib source                         
##  assertthat    0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                 
##  blogdown      0.17       2019-11-13 [1] CRAN (R 3.6.1)                 
##  bookdown      0.16       2019-11-22 [1] CRAN (R 3.6.1)                 
##  cli           2.0.0      2019-12-09 [1] CRAN (R 3.6.1)                 
##  colorspace    1.4-1      2019-03-18 [1] CRAN (R 3.6.0)                 
##  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                 
##  digest        0.6.23     2019-11-23 [1] CRAN (R 3.6.1)                 
##  dplyr         0.8.3      2019-07-04 [1] CRAN (R 3.6.0)                 
##  evaluate      0.14       2019-05-28 [1] CRAN (R 3.6.0)                 
##  fansi         0.4.0      2018-10-05 [1] CRAN (R 3.6.0)                 
##  farver        2.0.1      2019-11-13 [1] CRAN (R 3.6.1)                 
##  ggfx        * 0.0.0.9000 2019-12-18 [1] Github (thomasp85/ggfx@4345c5e)
##  ggplot2     * 3.2.1      2019-08-10 [1] CRAN (R 3.6.0)                 
##  glue          1.3.1      2019-03-12 [1] CRAN (R 3.6.0)                 
##  gtable        0.3.0      2019-03-25 [1] CRAN (R 3.6.0)                 
##  htmltools     0.4.0      2019-10-04 [1] CRAN (R 3.6.1)                 
##  knitr         1.26       2019-11-12 [1] CRAN (R 3.6.1)                 
##  labeling      0.3        2014-08-23 [1] CRAN (R 3.6.0)                 
##  lazyeval      0.2.2      2019-03-15 [1] CRAN (R 3.6.0)                 
##  lifecycle     0.1.0      2019-08-01 [1] CRAN (R 3.6.0)                 
##  magick        2.2        2019-08-26 [1] CRAN (R 3.6.0)                 
##  magrittr      1.5        2014-11-22 [1] CRAN (R 3.6.0)                 
##  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.6.0)                 
##  pillar        1.4.2      2019-06-29 [1] CRAN (R 3.6.0)                 
##  pkgconfig     2.0.3      2019-09-22 [1] CRAN (R 3.6.0)                 
##  purrr         0.3.3      2019-10-18 [1] CRAN (R 3.6.0)                 
##  R6            2.4.1      2019-11-12 [1] CRAN (R 3.6.1)                 
##  ragg          0.1.3      2019-08-28 [1] CRAN (R 3.6.1)                 
##  Rcpp          1.0.3      2019-11-08 [1] CRAN (R 3.6.1)                 
##  rlang         0.4.2      2019-11-23 [1] CRAN (R 3.6.1)                 
##  rmarkdown     2.0        2019-12-12 [1] CRAN (R 3.6.1)                 
##  scales        1.1.0      2019-11-18 [1] CRAN (R 3.6.1)                 
##  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                 
##  stringi       1.4.3      2019-03-12 [1] CRAN (R 3.6.0)                 
##  stringr       1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                 
##  systemfonts   0.1.1      2019-07-01 [1] CRAN (R 3.6.1)                 
##  tibble        2.1.3      2019-06-06 [1] CRAN (R 3.6.0)                 
##  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.6.0)                 
##  withr         2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                 
##  xfun          0.11       2019-11-12 [1] CRAN (R 3.6.1)                 
##  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.6.0)                 
## 
## [1] /home/lluis/R/x86_64-pc-linux-gnu-library/3.6
## [2] /usr/local/lib/R/site-library
## [3] /usr/lib/R/site-library
## [4] /usr/lib/R/library
Avatar
Lluís Revilla Sancho
Bioinformatician

Bioinformatician with interests in functional enrichment, data integration and transcriptomics.

Related