1

I'm trying to plot columns of a matrix as vectors into one graph. Is there any plot function to achieve so?

  • 4
    You'll get plenty of good answers, but you need to give us some more details. Here's a good resource to help you help us: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Chase Jan 26 '19 at 00:52

2 Answers2

2

Since you say that you want to plot columns as vectors, I assume that your matrix has two rows. You want to draw an arrow from the origin to the x-y coordinates given by the column. You can do this by making a blank plot and then using the arrows function. Here is a simple example.

## Sample data
set.seed(234)
DAT = matrix(runif(8), nrow=2)

## Plotting
plot(NULL, xlim=c(0,1), ylim=c(0,1), xlab="X", ylab="Y")
arrows(rep(0,4), rep(0,4), DAT[1,], DAT[2,])

arrows

Is that what you are looking for?

G5W
  • 36,531
  • 10
  • 47
  • 80
0

It's difficult to know what you're looking for if you don't provide some minimum example, but supposing that you have a matrix with coordinates for starting and ending points, then this can be accomplished like this:

Please notice that instead of a matrix I start my example with a data.frame. The former can be converted to the later with the as.data.frame function.

# Create some dummy data
a <- data.frame(x_start = 1:3, y_start = 2:4, x_end = 3:5, y_end = 4:6)

# load required packages
require(ggplot2)
require(grid)

# plot the data:
ggplot(a, aes(x_start, y_start, xend = x_end, yend = y_end))+geom_segment(arrow = arrow())
PavoDive
  • 6,322
  • 2
  • 29
  • 55