Geometric figure with data in R

1

I have 3 points and would like to form a triangle with them, then overlap another triangle with 3 more points and so on.

I tried this, but I can not make the triangle and the limits of the following graphs are ignored.

a = matrix(c(rnorm(6)), ncol = 2)
b = matrix(c(rnorm(6)), ncol = 2)
d = matrix(c(rnorm(6)), ncol = 2)
plot(a[,1]~a[,2],pch = 16)
par(new = T)
plot(b[,1]~b[,2], axes = F, ann = F, pch = 16, col = "red")
par(new = T)
plot(d[,1]~d[,2], axes = F, ann = F, pch = 16, col = "green")

Thank you in advance.

    
asked by anonymous 29.03.2016 / 08:49

1 answer

0

I've put together two functions, one to create the random coordinates of a triangle and one to plot the n triangles of random coordinates you want. The code is pretty rough, but I think it caters to what you wanted.

code

# Pacote que a gente vai usar, muito bom pra gerar gráficos
require(ggplot2)

# função para gerar coordernadas aleatórias do triângulo
geraTriangulo <- function() data.frame(x = rnorm(3), y = rnorm(3))

# função para plotar o triângulo
plotTriangulo <- function(n) {
  g = ggplot()
  for(i in 1:n){
    g <- g + geom_polygon(data = geraTriangulo(), aes(x = x, y = y), 
      colour="black", fill = NA)
  }
  print(g)
}

example

#input
> plotTriangulo(200) #criará 200 triângulos aleatórios sobrepostos

#output

    
29.03.2016 / 18:39