luukvdmeer / sfnetworks

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

Extend plot.sfnetwork to include the possibility of specifying different args for nodes and edges #246

Open agila5 opened 1 year ago

agila5 commented 1 year ago

Is your feature request related to a problem? Please describe. I think it might be nice if we slightly change plot.sfnetwork to include the possibility of defining different args to plot nodes and edges. Currently, we simply pass the same dots argument to both plotting routines

https://github.com/luukvdmeer/sfnetworks/blob/23a4125030178f5c42ebb48d2547052a31a3cdea/R/plot.R#L48-L61

which implies that, for example, in the following situation the nodes are coloured in a "weird" way (since the vector 1:3 is recycled to c(1:3, 1) to match the number of nodes. That also explains why two nodes are coloured as black):

library(sfnetworks)
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
l1 <- st_linestring(rbind(c(0, 0), c(1, 0)))
l2 <- st_linestring(rbind(c(1, 0), c(1, 1)))
l3 <- st_linestring(rbind(c(1, 1), c(2, 1)))
l <- st_sfc(l1, l2, l3)
sfn <- as_sfnetwork(l)
plot(sfn, col = 1:3, lwd = 2, cex = 2)

Created on 2023-07-28 with reprex v2.0.2

Describe the solution you'd like Add the possibility to specify certain arguments just for the nodes or just for the edges (or maybe it's already possible but I'm not sure how...). I'm also not sure about the interface but I was thinking about something like

plot.sfnetwork = function(x, draw_lines = TRUE, node_args = list(), edge_args = list(), ...) {
  # TODO
}

where c(node_args, dots) are the arguments passed to the plotting routine for the nodes and c(edge_args, dots) are the arguments passed to the plotting routine for the edges.

Describe alternatives you've considered Just build the plot manually layer by layer.

agila5 commented 1 year ago

Another idea in this regard would be to copy the approach adopted by plot.igraph:

The first method is to supply named arguments to the plotting commands: plot.igraph, tkplot or rglplot. Parameters for vertices start with prefix ‘vertex.’, parameters for edges have prefix ‘edge.’, and global parameters have no prefix. Eg. the color of the vertices can be given via argument vertex.color, whereas edge.color sets the color of the edges. layout gives the layout of the graphs.