r - Overlay multiple lines from data frame with index column onto existing plot -
r - Overlay multiple lines from data frame with index column onto existing plot -
i have dataframe 3 columns, (id, lat, long), can build little section of next data:
df <- data.frame( id=c(1,1,2,2,2,2,2,2,3,3,3,3,3,3), lat=c(58.12550, 58.17426, 58.46461, 58.45812, 58.45207, 58.44512, 58.43358, 58.42727, 57.77700, 57.76034, 57.73614, 57.72411, 57.70498, 57.68453), long=c(-5.098068, -5.314452, -4.914108, -4.899922, -4.887067, -4.873312, -4.852384, -4.840817, -5.666568, -5.648711, -5.617588, -5.594681, -5.557740, -5.509405))
the id
column index column. rows same id
number have coordinates single line. in info frame id
number varies 1 through 7696. have 7696 lines plot.
each id
number relates individual separate line of lat
, long
coordinates. want overlay onto existing plot of these 7696 individual lines.
with illustration info above contains lat
& long
coordinates lines 1, 2, 3.
what best way overlay these lines onto existing plot, thinking maybe kind of loop?
using ggplot2
:
#dummy info df <- data.frame( id=c(1,1,2,2,2,2,2,2,3,3,3,3,3,3), lat=c(58.12550, 58.17426, 58.46461, 58.45812, 58.45207, 58.44512, 58.43358, 58.42727, 57.77700, 57.76034, 57.73614, 57.72411, 57.70498, 57.68453), long=c(-5.098068, -5.314452, -4.914108, -4.899922, -4.887067, -4.873312, -4.852384, -4.840817, -5.666568, -5.648711, -5.617588, -5.594681, -5.557740, -5.509405)) library(ggplot2) #plot ggplot(data=df,aes(lat,long,colour=as.factor(id))) + geom_line()
using base of operations r:
#plot blank with(df,plot(lat,long,type="n")) #plot lines for(i in unique(df$id)) with(df[ df$id==i,],lines(lat,long,col=i))
r plot
Comments
Post a Comment