使用ggplot链接散点图中的兴趣点

时间:2016-01-26 01:52:36

标签: r ggplot2 scatter-plot edges

我正在尝试创建散点图,然后在散点图中的点之间添加线条。我可以使用import csv with open('C:/Users/User/OneDrive/Documents/Price.csv','r') as csvinput: with open('C:/Users/User/OneDrive/Documents/Date.csv', 'w') as csvoutput: writer = csv.writer(csvoutput, lineterminator='\n') reader = csv.reader(csvinput) all = [] row = next(reader) for row in reader: row.append(row[1]) all.append(row) writer.writerows(all) plot来实现此功能,如下所示:

segments

我想实现相同类型的想法,但使用ggplot2。我的理由是,我可能想在情节中添加美学,而ggplot2允许这超过set.seed(10) xvar = runif(10, 0, 1) yvar = runif(10, 0, 1) start = c(1, 1, 1, 9) end = c(2, 4, 6, 10) plot(xvar, yvar) segments(xvar[start], yvar[start], xvar[end],yvar[end], col= 'blue') 。我尝试了以下的变体:

plot

但无济于事。任何指针都非常感谢!

1 个答案:

答案 0 :(得分:3)

我们的想法是使用geom_line并为每个细分定义组(gr):

dat <- data.frame(xvar = xvar, yvar = yvar)    
dat2 <- cbind(dat[c(start, end), ], gr = 1:length(start))

ggplot(dat, aes(x = xvar, y = yvar)) + geom_point(shape = 20, size = 1) +
  geom_line(aes(x = xvar, y = yvar, group = gr), data = dat2)

enter image description here