Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
agila5 opened this issue Jul 28, 2023 · 1 comment
Labels
feature 🎁 Request a new feature low priority 💤 Would be nice to have but not necessary

Comments

@agila5
Copy link
Collaborator

agila5 commented Jul 28, 2023

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

sfnetworks/R/plot.R

Lines 48 to 61 in 23a4125

dots = list(...)
pch_missing = is.null(dots$pch)
dots$pch = if (pch_missing) 20 else dots$pch
# Get geometries of nodes.
nsf = pull_node_geom(x)
# Plot the nodes.
do.call(plot, c(list(nsf), dots))
# If necessary, plot also the edges.
if (draw_lines) {
x = explicitize_edges(x)
esf = pull_edge_geom(x)
dots$add = TRUE
do.call(plot, c(list(esf), dots))
}

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 agila5 added feature 🎁 Request a new feature low priority 💤 Would be nice to have but not necessary labels Jul 28, 2023
@agila5
Copy link
Collaborator Author

agila5 commented Jul 31, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🎁 Request a new feature low priority 💤 Would be nice to have but not necessary
Projects
None yet
Development

No branches or pull requests

1 participant