luukvdmeer / sfnetworks

Tidy Geospatial Networks in R
https://luukvdmeer.github.io/sfnetworks/
Other
347 stars 20 forks source link

Customize default `sfnetworks` plot method #68

Closed loreabad6 closed 4 years ago

loreabad6 commented 4 years ago

Hello! On the map made by the sfnetworks package, is it possible to remove those black dots ?? to make similar to the map made by stplanr

Originally posted by @JovaniSouza in https://github.com/luukvdmeer/sfnetworks/issues/67#issuecomment-656264708

NOTE: moved this into new issue and hid from #67

loreabad6 commented 4 years ago

@agila5 reply:

Hi @JovaniSouza, I don't think that it's possible to remove the "nodes" from the default plot of a sfnetwork object (@loreabad6 is that right?).

You can always extract only the edges and plot them, for example:

# install package
remotes::install_github("luukvdmeer/sfnetworks", ref = "develop")

# load package
library(sf)
library(sfnetworks)
library(tmap)

# load data
download.file("https://github.com/ropensci/stplanr/releases/download/0.6.1/Example.zip", "Example.zip")
unzip("Example.zip")
roads = st_read("Example/Roads/Roads.shp", quiet = TRUE)
points = st_read("Example/Points/Points.shp", quiet = TRUE)
roads_trf = st_transform(roads, st_crs(points))

# calculate the sfNetwork object
net = as_sfnetwork(roads_trf, directed = FALSE) %>%
  activate("edges") %>%
  dplyr::mutate(weight = edge_length())

# routing
from = c(-49.95058, -24.77502)
to = c(-49.91084, -24.75200)
p1 = sf::st_as_sf(data.frame(x = from[1], y = from[2]), coords = c("x", "y"), crs = sf::st_crs(net))
p2 = sf::st_as_sf(data.frame(x = to[1], y = to[2]), coords = c("x", "y"), crs = sf::st_crs(net))

r = tidygraph::convert(net, to_spatial_shortest_paths, p1, p2)
r2 = tidygraph::convert(net, to_spatial_shortest_paths, points[3, ], points[4, ])

# plot
tm_shape(net %>% activate(edges) %>% st_as_sf()) +
  tm_lines() + 
tm_shape(rbind(p1, p2)) + 
  tm_dots(col = "red", size = 0.5) + 
tm_shape(r %>% activate(edges) %>% st_as_sf()) + 
  tm_lines(col = "red", lwd = 3) + 
tm_shape(points[c(3, 4), ]) + 
  tm_dots(col = "blue", size = 0.5) + 
tm_shape(r2 %>% activate(edges) %>% st_as_sf()) + 
  tm_lines(col = "blue", lwd = 3)

Created on 2020-07-10 by the reprex package (v0.3.0)

loreabad6 commented 4 years ago

Hi @JovaniSouza! The plot method for sfnetworks is a very basic one for a fast visualization. Internally it merges the edges (LINESTRING) and nodes (POINT) into a GEOMETRYCOLLECTION. So, it takes the plotting defaults of sf. Some plot parameters like col change the edges and the nodes, but others only work on one of them. So for example if you don't want to plot the nodes, you can do the following:

library(sfnetworks)
net = roxel %>% as_sfnetwork()

plot(net, cex = 0)

which should give you what you wanted.

But, if you want to do more fancy plotting, then you can follow @agila5 suggestions :)

JovaniSouza commented 4 years ago

Thank you so much @agila5 and @loreabad6

JovaniSouza commented 4 years ago

@agila5 , could you please test the code you made above for the road shapefile on this site (https://drive.google.com/file/d/1RNYWyjJhXHxR4mnvfWdQPRnKrDthatne/view?usp=sharing)? There are a few more roads than the roads file you tested. But, I couldn't get it to run. Thanks again!

agila5 commented 4 years ago

Hi @JovaniSouza, sorry but I cannot open the file on google drive since it says it's private. btw: probably it's easier if you write me an email (a.gilardi5@campus.unimib.it) with code and data since actually that question is offtopic here

Robinlovelace commented 4 years ago

@agila5 you can access the data here:

download.file("https://github.com/ropensci/stplanr/releases/download/0.6.1/Example.zip", "Example.zip")