Geometric figure with data in R

Asked

Viewed 79 times

1

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

I tried that, but I can’t make the triangle and the boundaries of the following charts 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")

Thanks in advance.

  • Hello man, are several triangles, I want to randomly generate the coordinates and plot one over the other. For example, Gero 150 coordinates there plot 50 triangles one overlapping the other. (150 was just an example)

1 answer

0


I set up 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 fits 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

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.