简体   繁体   中英

Plotting Line Segments from Coordinates

I want to plot line segments. Each line has a starting point (x1/y1) and ending point (x2/y2). I am following this Answer , but it does not work. I just get an empty plot:

空图,没有线段

Here the commands I used:

data <- matrix(1:12, ncol = 4, dimnames = list(c("a","b","c"), c("x1","y1","x2","y2")))
plot(NA, xlim=c(0,15), ylim=c(0,15), xlab="x", ylab="y")
segments(data["x1"], data["y1"], data["x2"], data["y2"])

data gives:

  x1 y1 x2 y2
a  1  4  7 10
b  2  5  8 11
c  3  6  9 12

Later I plan to load data from a text file ( csv ). But for this first test I'm just using this generated nonsensical numbers.

Since I don't get error messages, I'm stuck here. Which basic error have I made? Thank you for your help.

I use:

R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Platform: x86_64-pc-linux-gnu (64-bit)

As StupidWolf says in the comments, there are missing commas for the matrix. Correct is:

segments(data[,"x1"], data[,"y1"], data[,"x2"], data[,"y2"]) 

But: You can load csv data directly into a data frame and then plot it. Matrix not needed.

在此处输入图像描述

Sample data in a file called data.csv :

x1,y1,x2,y2
1,1,5,7
6,4,12,10
8,2,3,9

Commands:

data=read.csv("data.csv", sep=",")
plot(NA, xlim=c(0,15), ylim=c(0,15), xlab="x", ylab="y")
segments(data$x1, data$y1, data$x2, data$y2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM