# Unweighted Network

## Loading Data

Letâ€™s load libraries, stop and line data.

``````stops = read.csv("../crawler/output/stop.csv")
stops = unique(stops)
lines = read.csv("../crawler/output/linedetail.csv")``````

## Edge Construction

Now create edges by connecting adjacent stops in each line:

``````newedges = list()
k = 1
for (i in 1:nrow(lines)) {
line_name = as.character(lines\$cdk_id[i])
line_stops = strsplit(as.character(lines\$stop_list[i]), ";")[[1]]
for (j in 1:(length(line_stops)-1)){
newedges[[k]] = c(line_stops[j], line_stops[j+1], line_name)
k = k + 1
}
}``````

Then we can convert this edge list into a matrix with columns (V1, V2, Line):

``````matrix_edges = matrix(data=NA, nrow=length(newedges), ncol=3)

for (i in 1:length(newedges)){
matrix_edges[i, 1] = newedges[[i]][1]
matrix_edges[i, 2] = newedges[[i]][2]
matrix_edges[i, 3] = newedges[[i]][3]
}``````

Now we convert this matrix into a data frame with columns V1 (From), V2 (To) and Line (Edge label).

``````frame_edges = as.data.frame(matrix_edges, stringsAsFactors = TRUE, row.names=c("from", "to", "line"))
colnames(frame_edges) = c("V1","V2","Line")``````

## Constructing Graph

Now that we have edges and nodes, we can construct a graph object.

``````library("igraph")
g = graph.data.frame(frame_edges, TRUE, stops\$cdk_id)
V(g)\$Latitude = stops\$lat
V(g)\$Longitude = stops\$lon
V(g)\$size = 0.5
V(g)\$label = as.character(stops\$name)
V(g)\$labels = as.character(stops\$name)
E(g)\$label = as.character(frame_edges\$Line)``````

## Drawing the Map

Now create an aerial map:

``library("ggmap")``
``## Loading required package: ggplot2``
``````location = c(mean(stops\$lon), mean(stops\$lat))
map = get_map(location, maptype="watercolor", source="osm", zoom=10)``````
``## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=41.034869,28.991441&zoom=10&size=640x640&maptype=terrain&sensor=false``

Now draw the spatial map and plot nodes/edges on it:

``````library(popgraph)
p = ggmap(map)
p = p + geom_nodeset( aes(x=Longitude, y=Latitude, size=size, label=label), g)
p = p + geom_edgeset( aes(x=Longitude, y=Latitude, shape=label, label=label), g, color="yellow", alpha=0.3)
p + xlab("Longitude") + ylab("Latitude")``````
``## Warning: Removed 424 rows containing missing values (geom_point).``
``## Warning: Removed 1453 rows containing missing values (geom_segment).``