-1

For example, I have a txt file consist of data as below. I read this txt file using read.table("C/".../file.txt")I would like to just plot the value row by row into a line using R. I just want to see how each row of data looks like. This is a GPS coordinate data where V1 and V3 are the latitude and V2 and V4 are longitude.

   V1        V2    V3        V4
39.9847  116.3184 39.98468 116.3184
39.9847  116.3184 39.98468 116.3184
39.98469 116.3184 39.9845  116.3137
39.98462 116.3143 39.98364 116.2993

Thank you.

Alex
  • 41
  • 6
  • Hi, meaning use plot(x,y, type="l") for first point then second point and connecting them with line then add NA and restart plot? – Alex Mar 23 '16 at 16:09
  • Welcome to SO, Alvin. Please do always provide a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610), ready to copy-paste-run. – lukeA Mar 23 '16 at 16:19

1 Answers1

0

If I understand you correctly, you could do

set.seed(1)
(m <- matrix(sample(1:10, 4*4, T), ncol = 4, dimnames = list(NULL, c("x1", "y1", "x2", "y2"))))
#      x1 y1 x2 y2
# [1,]  3  3  7  7
# [2,]  4  9  1  4
# [3,]  6 10  3  8
# [4,] 10  7  2  5
library(ggplot2)
ggplot(as.data.frame(m), aes(x=x1, y=y1, xend=x2, yend=y2)) + 
  geom_segment()

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thank you for the help, this is something that I want. Actually I have stored all my point into a matrix of 4 column and about 100 row. So what I have to do is to change ggplot(as.data.frame(df), aes(x=x1, y=y1, xend=x2, yend=y2)) + geom_segment() ? – Alex Mar 23 '16 at 16:21
  • In theory, you don't have to change anything. In practice, the result probably won't tell you much in terms of visualization. It's best you #1 edit your post, #2 add `dput(yourdata)`, and #3 try to explain what exactly the result should look like. – lukeA Mar 23 '16 at 16:24
  • Ok, my question is very easy as stated above, I just need to draw all the line out based on the data I have. The result you shown is valid for me. I'm still new to R, so I'm not so sure the command in doing the work you do on top. My matrix name is x, and the data for x1 is from column 1 of the matrix x, y1 from column2, x2 from column 4 and y2 from column 5 – Alex Mar 23 '16 at 16:34
  • I know what you mean, but "very easy" does not count much here; please get used to SO good practices, which means: always provide a minimal reproducible example. (I might cost you a bit more time, but all readers benefit). Have a look at http://stackoverflow.com/questions/tagged/r and compare posts with positive ratings vs posts with negative/no ratings. – lukeA Mar 23 '16 at 16:37
  • I'm sorry to ask, may I know how I can upload my dataset here? – Alex Mar 24 '16 at 00:49
  • As I said: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610 - you can probably use `dput`. – lukeA Mar 24 '16 at 10:50