# This script illustrates a Git Practice as part of the RDM workshop on Reproducible Scientific Computing and Data Analysis.
# It generates an image (robot drawing) and saves the figure as png file.

library(ggplot2)

robot <- ggplot() +
  
  # Antenna
  annotate("segment", x = 0, xend = 0,
           y = 2, yend = 2.5,
           linewidth = 2.5, color = "#526477") +
  annotate("point", x = 0, y = 2.5,
           size = 8, color = "#FFB84D") +
  annotate("point", x = 0, y = 2.5,
           size = 3, color = "#FFF1B8") +
  
  # Head 
  annotate("rect", xmin = -1, xmax = 1,
           ymin = 0.5, ymax = 2,
           fill = "#C9A0DC", color = "#526477",  # Colors: Sky blue DCEAF2 or purple C9A0DC
           linewidth = 1.5) +
  
  # Body 
  annotate("rect", xmin = -0.8, xmax = 0.8,
           ymin = -1, ymax = 0.5,
           fill = "#C9A0DC", color = "#526477", # Colors: Sky blue DCEAF2 or purple C9A0DC
           linewidth = 1.4) +
  
  # Arms 
  annotate("segment", x = -0.8, xend = -1.15,
           y = 0.15, yend = -0.15,
           linewidth = 10, color = "#526477") +
  annotate("segment", x = 0.8, xend = 1.15,
           y = 0.15, yend = -0.15,
           linewidth = 10, color = "#526477") +
  
  # Elbow joints
  annotate("point", x = c(-1.15, 1.15),
           y = c(-0.15, -0.15),
           size = 8, color = "#526477") +
  
  # Forearms
  annotate("segment", x = -1.15, xend = -1.32,
           y = -0.15, yend = -0.55,
           linewidth = 9, color = "#526477") +
  annotate("segment", x = 1.15, xend = 1.32,
           y = -0.15, yend = -0.55,
           linewidth = 9, color = "#526477") +
  
  # Hands
  annotate("point", x = c(-1.32, 1.32),
           y = c(-0.55, -0.55),
           size = 10, color = "#F2A65A") +
  
  # Legs
  annotate("segment", x = -0.4, xend = -0.45,
           y = -1, yend = -1.5,
           linewidth = 11, color = "#526477") +
  annotate("segment", x = 0.4, xend = 0.45,
           y = -1, yend = -1.5,
           linewidth = 11, color = "#526477") +
  
  # Feet
  annotate("rect", xmin = -0.72, xmax = -0.27,
           ymin = -1.72, ymax = -1.48,
           fill = "#526477", color = "#526477",
           linewidth = 1) +
  annotate("rect", xmin = 0.27, xmax = 0.72,
           ymin = -1.72, ymax = -1.48,
           fill = "#526477", color = "#526477",
           linewidth = 1) +
  
  # Chest panel
  annotate("rect", xmin = -0.43, xmax = 0.43,
           ymin = -0.75, ymax = -0.05,
           fill = "#F5F8FA", color = "#526477",
           linewidth = 1) +
  
  # Panel lights
  annotate("point", x = c(-0.22, 0, 0.22),
           y = c(-0.2, -0.2, -0.2),
           size = 4, color = c("#FF6B6B", "#FFD166", "#06D6A0")) +
  
  # Ears
  annotate("rect", xmin = -1.12, xmax = -0.98,
           ymin = 1.05, ymax = 1.48,
           fill = "#F2A65A", color = "#526477",
           linewidth = 1) +
  annotate("rect", xmin = 0.98, xmax = 1.12,
           ymin = 1.05, ymax = 1.48,
           fill = "#F2A65A", color = "#526477",
           linewidth = 1) +
  
  # Face screen
  annotate("rect", xmin = -0.78, xmax = 0.78,
           ymin = 0.78, ymax = 1.82,
           fill = "#526477", color = "#526477",
           linewidth = 1) +
  
  # Eyes
  annotate("point", x = c(-0.38, 0.38),
           y = c(1.42, 1.42),
           size = 7, color = "#80F2D0") +
  
  # Smile
  annotate("path",
           x = seq(-0.27, 0.27, length.out = 60),
           y = 1.12 - 0.10 *
             sin(seq(0, pi, length.out = 60)),
           color = "#80F2D0", linewidth = 1.5) +
  
  coord_fixed(xlim = c(-1.8, 1.8),
              ylim = c(-2, 3),
              expand = FALSE) +
  theme_void() #+

  # Add a hat
#    annotate("polygon",
#             x = c(-0.7, 0, 0.7),
#             y = c(2, 2.85, 2),
#             fill = "#E4D96F",
#             color = "#526477",
#             linewidth = 1) + 
#    annotate("rect",
#             xmin = -1, xmax = 1,
#             ymin = 2.0, ymax = 2.15,
#             fill = "#E4D96F", color = "#526477",
#             linewidth = 1.2)
  
robot

ggsave(
  filename = file.path(getwd(), "robot_antenna.png"),
  plot = robot,
  width = 8,
  height = 6,
  dpi = 300
)
