How to make a cross from a matrix in R

Asked

Viewed 225 times

1

I have the following matrix:

      x y
A = | 2 0 |
    | 3 0 |
    | 1 2 |
    | 1 3 |
    | 2 1 |
    | 2 2 |
    | 2 3 |
    | 2 4 |
    | 3 2 |

How can I make a Cross as in the following figure when plotting this matrix on a graph: inserir a descrição da imagem aqui

I’m trying using the Plot() function for line graph

1 answer

1

The data are not in the correct order and are incomplete, in the question there are only 9 lines when 12 are needed, as many as the points of the figure. I include the corrected data at the end.

Here are two ways to draw the cross. The first only with R base.

plot(0, type = "n", xlim = range(dados$x), ylim = range(dados$y))
segments(x0 = dados$x, y0 = dados$y,
         x1 = c(dados$x[-1], dados$x[1]), 
         y1 = c(dados$y[-1], dados$y[1]))

inserir a descrição da imagem aqui

The second with the package ggplot2.

library(ggplot2)

ggplot(dados, aes(x, y)) +
  geom_segment(aes(xend = c(x[-1], x[1]), yend = c(y[-1], y[1]))) +
  theme_bw()

inserir a descrição da imagem aqui

In both the trick is the same, define as segment end points the points (x2, y2), (x3, y3), etc., (x1, y1).

Dice.

dados <- read.table(text = "
x y
2 0 
3 0 
3 2 
4 2
4 3 
3 3
3 4
2 4
2 3
1 3
1 2
2 2 
", header = TRUE)

Browser other questions tagged

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