Search code examples
rplotvectorgraph

Plot graph with values of vectors


I want to visualize the elements of my vectors in a graph. I want to generate a graph with a certain x- and y-axis and then put the values of my vectors as points into the graph. I also want different colors for the values of the different vectors. How do I do that?

For example: I have 10 elements in vector A and want to put those elements into the graph. The first Element of vector A has the y-value A[1] and the x-value 1. The second Element of vector A has the y-value A[2] and the x-value 2. Same with vector B.

vec1 = 1:10
vec2 = 1:10
for(idx in 1:10){
  vec1[idx] = runif(1, min=0, max=100)
  vec2[idx] = runif(1, min=0, max=100)
}
plot(vec1 and vec2) // How do I do this?

dput output for vec1: c(81.9624423747882, 45.583715592511, 56.2400584807619, 8.25600677635521, 82.0227505406365, 45.6240070518106, 68.7916911672801, 94.491201499477, 22.0095717580989, 4.29550902917981)

dput output for vec2: c(29.5684755546972, 68.0154771078378, 52.2058120695874, 2.48502977192402, 91.9532125117257, 24.7736480785534, 66.5003522532061, 79.014728218317, 47.9641782585531, 20.5593338003382)


Solution

  • Starting with

    vec1 = 1:10
    vec2 = 1:10
    for(idx in 1:10){
      vec1[idx] = runif(1, min=0, max=100)
      vec2[idx] = runif(1, min=0, max=100)
    }
    plot(vec1 and vec2) // How do I do this?
    

    Try this:

    plot( 1:20, c(vec1,vec2) , col=rep(1:2,10)  # just points
    lines( 1:20, c(vec1,vec2) )                 # add lines
    # if you wanted the same x's for both sequences the first argument could be 
    #      rep(1:10, 2)  instead of 1:20
    

    Note: Your set up code could have been just two lines (no loop):

      vec1 = runif(10, min=0, max=100)
      vec2 = runif(10, min=0, max=100)