Home > Blockchain >  is there a simple way to draw a graph in christmas tree farm in r
is there a simple way to draw a graph in christmas tree farm in r

Time:11-21

Is there a mathematical function or a way in which we can get a graph that will be in the form of a Christmas tree, like this?

enter image description here

thanks for your help

CodePudding user response:

Here's one of many options:

tree <- data.frame(x = c(-5, 5, 2, 4, 1.5, 3, 0, -3, -1.5, -4, -2, -5, 
                         -0.75, 0.75, 0.75, -0.75),
                   y = c(1, 1, 3, 3, 5, 5, 7, 5, 5, 3, 3, 1, 0, 0, 1, 1),
                   part = rep(c("branches", "trunk"), times = c(12, 4))) 

baubles <- data.frame(x = c(-1.9, -2.4, 0.5, -0.3, -0.2, -1.3, 0.5, 
                            1.2, -2.2, -1, 1.7, -1.4, -1.4, 0.4, 2.1, 0.4, 
                            -0.8, -3.3, 0.5, -2.2, -0.1, -1.5, 2, 3.9, 1.3, 
                            -1.7, 3.7, 2.8, 1, -0.1, 3.8, -2.9, -1.9, -1.7,
                            -2.6, -2.3, 0.9, 1, -0.4, 1.5, 1.8, -0.5, -1.4, 
                            -0.4, -0.5, -0.9, -1.7, 0.7, 1.6, 1.2, -0.4, 1, 
                            0.8, 2.3, -2.5, -2, -2.9, -1.4, -1.1, 0.2),
                      y = c(3, 3.3, 1.2, 4.4, 5.1, 5.2, 1.1, 6, 1.5, 2.4, 1.2,
                            5.4, 2.2, 3.4, 3.4, 3.8, 3.1, 1.2, 4.3, 
                            1.6, 2.4, 5.4, 4.5, 1.1, 1.3, 5, 1.5, 1.9, 1.7, 
                            5.4, 1.3, 1.1, 2.2, 4, 1.8, 2, 4.6, 1.1, 5.9, 4.4, 
                            2, 1.5, 2, 1.2, 5.3, 3.6, 3.5, 4.5, 5.8, 3, 2.7, 
                            5.3, 3.1, 1.7, 1.6, 2.8, 3.6, 2.2, 2.8,  1.7),
                      color = sample(c("white", "yellow", "red"), 60, TRUE))

library(ggplot2)

ggplot(tree, aes(x, y))   
  geom_polygon(aes(fill = part))  
  geom_point(data = baubles, aes(color = color), size = 4)  
  scale_fill_manual(values = c("green4", "brown4"), name = "Parts of tree")  
  scale_color_identity(guide = guide_legend(), labels = c("red bauble",
                       "white bauble", "yellow bauble"), name = "Decorations")  
  theme_minimal(base_size = 20)

enter image description here

Created on 2022-11-20 with reprex v2.0.2

  • Related