x,y曲线图使用R和某些点内的绘制线

时间:2014-12-21 15:16:15

标签: r

我已经解决了TSP问题,现在我有两个输出文件。

File1中。包含所有城市的(x,y)值。

我使用以下代码绘制了这些点:

d = read.table('city_data.txt',  sep=" ", col.names=c("cityX", "cityY"));

plot(d$cityX,d$cityY);

File2:包含连接的节点n序列,假设25-> 22-> 4-> 21 ...........

文件city_data.txt中的

row1代表节点1的x,y值,row2代表节点的价值.....

我如何画25-> 22-> 4-> 21 ...............

1 个答案:

答案 0 :(得分:0)

这是一个有效的例子:

### Create a sample input data.frame
d <- read.csv(text=
'cityX,cityY
1.0,1.0
2.0,3.0
3.0,2.0
2.0,1.0
') 

# this line just add a row to close the TSP "tour" 
# (i.e. the salesman returning to the first city) 
# if you don't want it, just remove this line
d <- rbind(d,d[1,])
###

### PLOT 1 - LINES BETWEEN CITIES
# plot the cities and the lines connecting them
plot(x=d$cityX,y=d$cityY,type='b',xlab='X',ylab='Y')
###


### PLOT 2 - ARROWS BETWEEN CITIES
# plot the cities
plot(x=d$cityX,y=d$cityY,type='p',xlab='X',ylab='Y')
# plot the arrows connecting them
arrows(x0=head(d$cityX,-1),
       y0=head(d$cityY,-1),
       x1=tail(d$cityX,-1),
       y1=tail(d$cityY,-1))
###

第1页:

PLOT 1

第2页:

PLOT 2