igraph的中国/世界地图可视化—用leaflet实现

/*********地图上的图可视化************/
#中国地图绘制
library(raster)
library(igraph)
china <- getData(‘GADM’, country=’CN’, level=1)
df<-data.frame(“from” = c(“Zhengzhou”, “Beijing”), “to”= c(“Beijing”, “Zhengzhou”))
meta <- data.frame(“name”=c(“Zhengzhou”, “Beijing”),  “lon”=c(113.42,116.28),  “lat”=c(34.44,39.54))
g <- graph.data.frame(df, directed=T, vertices=meta)
lo <- as.matrix(meta[,2:3])
plot(china)
plot(g, layout=lo, add = TRUE, rescale = FALSE)
 

 

#世界地图绘制
//解决缺包的问题
apt-get install libpng-dev
ln -s /root/anaconda2/lib/libpng16.so.16 /usr/local/lib/R/lib/
ln -s /root/anaconda2/lib/libpng16.so.16.17.0 /usr/local/lib/R/lib/
R
install.packages(“png”)
install.packages(“leaflet”)

library(“igraph”)
df<-data.frame(“from” = c(“Lyon”, “Toulouse”, “Paris”, “Marseille”), 
               “to”= c(“Paris”, “Paris”, “Marseille”, “Toulouse”))
meta <- data.frame(“name”=c(“Lyon”, “Toulouse”, “Paris”, “Marseille”), 
                   “lon”=c(-4.850000, 1.444209, 2.352222, 5.36978),  
                   “lat”=c(45.750000, 43.604652, 48.856614, 43.296482))

g <- graph.data.frame(df, directed=FALSE, vertices=meta)
lo <- layout.norm(as.matrix(meta[,2:3]))
plot.igraph(g, layout=lo,vertex.size = 60,
            vertex.color=”red”,
            vertex.frame.color= “white”,
            vertex.label.color = “white”,
            vertex.label.family = “sans”,
            edge.width=2,  
            edge.color=”black”)

library(sp)
gg <- get.data.frame(g, “both”)
vert <- gg$vertices
coordinates(vert) <- ~lon+lat
edges <- gg$edges
edges <- lapply(1:nrow(edges), function(i) {
  as(rbind(vert[vert$name == edges[i, “from”], ], 
           vert[vert$name == edges[i, “to”], ]), 
     “SpatialLines”)
})
for (i in seq_along(edges)) {
  edges[[i]] <- spChFIDs(edges[[i]], as.character(i))
}
edges <- do.call(rbind, edges)
library(leaflet)
leaflet(vert) %>% addTiles() %>% addMarkers(data = vert) %>% addPolylines(data = edges)

df<-data.frame(“from” = c(“Paris”, “Zhengzhou”, “Tokyo”, “San Fransico”, “New York”, “Berlin”, “London”, “Sydney”, “Sao Paulo”, “Moscow”, “Johannesburg”, “North Pole”, “South Pole”), 
               “to”= c(“Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”, “Zhengzhou”))
meta <- data.frame(“name”=c(“Zhengzhou”, “Paris”, “Tokyo”, “San Fransico”, “New York”, “Berlin”, “London”, “Sydney”, “Sao Paulo”, “Moscow”, “Johannesburg”, “North Pole”, “South Pole”), 
                “lon”=c(113.42, 2.352222, 139.8, -122.26, -74.00, 13.20, 0.07, 151.17, -46.38, 37.37, 27.54, 0, 0), 
                “lat”=c(34.44, 48.856614, 36.2, 37.46, 40.43, 52.31, 51.30, -33.55, -23.34, 55.45, -26.08, 90, -90))

参考:http://gis.stackexchange.com/questions/172606/how-to-use-igraph-with-leaflet-for-r

转载自:https://blog.csdn.net/huxuanlai/article/details/60138956

You may also like...