PedroA
PedroA

Reputation: 1925

plot table in R with lines connecting the points

although this might seem to be a very naive question I could not find any straight answer to this elsewhere, so I came here in search of help.

I have a tab file with four columns and I want to plot in R the relation between the 1st column and for example the 2nd one. The goal is to do a scatterplot like form but with smoothed lines connecting the consecutive points (I do not want to fit an abline to the graph), in a similar way that can be achieved in excel (scatter with smooth lines), with column1 in the x axis and column2 in the y axis. An example of the file can be like this:

881001  2   1   0
883001  0   0   0
885001  0   0   2
887001  0   0   1
889001  2   0   0
891001  7   0   0
893001  6   0   2
895001  0   0   3
897001  2   0   0
899001  2   0   0
901001  2   0   0
903001  5   0   0
905001  5   0   0
907001  3   0   0

In excel the graph would be something like this:

enter image description here

The problem is that every time I create a table like tis one I have to import it to excel and then create the graph. If there is any fast way just to check the graph on R would be great.

Thanks in advance.

Upvotes: 2

Views: 2314

Answers (1)

user1317221_G
user1317221_G

Reputation: 15441

here is a way:

# call your data dat
dat <- 
structure(list(V1 = c(881001L, 883001L, 885001L, 887001L, 889001L, 
891001L, 893001L, 895001L, 897001L, 899001L, 901001L, 903001L, 
905001L, 907001L), V2 = c(2L, 0L, 0L, 0L, 2L, 7L, 6L, 0L, 2L, 
2L, 2L, 5L, 5L, 3L), V3 = c(1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L), V4 = c(0L, 0L, 2L, 1L, 0L, 0L, 2L, 3L, 0L, 
0L, 0L, 0L, 0L, 0L)), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c(NA, 
-14L))


plot(dat$V1, dat$V2, main = "your plot")
lines(spline(dat$V1, dat$V2), col = 4)

enter image description here

Upvotes: 2

Related Questions