-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge commit '8d8787cce038bcf9c787d913f92add7af7e255a7'
- Loading branch information
Showing
67 changed files
with
501 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#' Draw edges as LINESTRINGs in geographical space | ||
#' | ||
#' This geom is equivalent in functionality to [ggplot2::geom_sf()] for `LINESTRING` | ||
#' geometries and allows for plotting of edges in their geographical space in | ||
#' different colours, linetypes and widths. | ||
#' | ||
#' @section Aesthetics: | ||
#' `geom_edge_sf` understand the following aesthetics. | ||
#' | ||
#' - alpha | ||
#' - colour | ||
#' - linetype | ||
#' - filter | ||
#' | ||
#' @inheritParams ggplot2::geom_sf | ||
#' | ||
#' @param mapping Set of aesthetic mappings created by [ggplot2::aes()] | ||
#' or [ggplot2::aes_()]. By default geometry is mapped to the geometry in | ||
#' the edge data. | ||
#' | ||
#' @author Lorena Abad | ||
#' | ||
#' @family geom_edge_* | ||
#' | ||
#' @examples | ||
#' if (require("sfnetworks", quietly = TRUE)) { | ||
#' gr <- sfnetworks::as_sfnetwork(roxel) | ||
#' ggraph(gr, 'sf') + geom_edge_sf() | ||
#' } | ||
#' | ||
#' @export | ||
#' | ||
geom_edge_sf <- function(mapping = NULL, data = get_sf_edges(), | ||
position = 'identity', show.legend = NA, ...) { | ||
mapping <- complete_edge_aes(mapping) | ||
mapping <- aes_intersect(mapping, aes(geometry = geometry)) | ||
c( | ||
layer_sf( | ||
geom = GeomEdgeSf, data = data, mapping = mapping, stat = StatFilterSf, | ||
position = position, show.legend = show.legend, inherit.aes = FALSE, | ||
params = expand_edge_aes(list2(na.rm = FALSE, ...)) | ||
), | ||
coord_sf(default = TRUE) | ||
) | ||
} | ||
#' @rdname get_edges | ||
get_sf_edges <- function(){ | ||
function(layout) { | ||
edges <- sf::st_as_sf(attr(layout, "graph"), "edges") | ||
attr(edges, 'type_ggraph') <- 'edge_ggraph' | ||
edges | ||
} | ||
} | ||
|
||
#' @rdname ggraph-extensions | ||
#' @format NULL | ||
#' @usage NULL | ||
#' @export | ||
GeomEdgeSf = ggproto("GeomEdgeSf", GeomSf, | ||
draw_panel = function(data, panel_params, coords) { | ||
names(data) <- sub('edge_', '', names(data)) | ||
names(data)[names(data) == 'width'] <- 'linewidth' | ||
GeomSf$draw_panel(data, panel_params, coords) | ||
}, | ||
draw_key = GeomEdgePath$draw_key, | ||
default_aes = aes( | ||
edge_colour = 'black', edge_width = 0.5, edge_linetype = 1, | ||
edge_alpha = NA | ||
), | ||
rename_size = FALSE | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#' Show nodes as POINTs in geographical space | ||
#' | ||
#' This geom is equivalent in functionality to [ggplot2::geom_sf()] for `POINT` | ||
#' geometries and allows for plotting of nodes in their geographical space in | ||
#' different shapes, colours and sizes. | ||
#' | ||
#' @section Aesthetics: | ||
#' `geom_node_sf` understand the following aesthetics. | ||
#' | ||
#' - alpha | ||
#' - colour | ||
#' - shape | ||
#' - size | ||
#' - filter | ||
#' | ||
#' @inheritParams ggplot2::geom_sf | ||
#' | ||
#' @param mapping Set of aesthetic mappings created by [ggplot2::aes()] | ||
#' or [ggplot2::aes_()]. By default geometry is mapped to the geometry in | ||
#' the node data. | ||
#' | ||
#' @author Lorena Abad | ||
#' | ||
#' @family geom_node_* | ||
#' | ||
#' @examples | ||
#' library(tidygraph) | ||
#' | ||
#' if (require("sfnetworks", quietly = TRUE)) { | ||
#' gr <- sfnetworks::as_sfnetwork(roxel) | ||
#' ggraph(gr, 'sf') + | ||
#' geom_node_sf(aes(color = centrality_betweenness())) | ||
#' } | ||
#' | ||
#' @export | ||
#' | ||
geom_node_sf <- function(mapping = NULL, data = get_sf_nodes(), | ||
position = 'identity', show.legend = NA, ...) { | ||
mapping <- aes_intersect(mapping, aes(geometry = geometry)) | ||
c( | ||
layer_sf( | ||
geom = GeomSf, data = data, mapping = mapping, stat = StatFilterSf, | ||
position = position, show.legend = show.legend, inherit.aes = FALSE, | ||
params = list2(na.rm = FALSE, ...) | ||
), | ||
coord_sf(default = TRUE) | ||
) | ||
} | ||
|
||
#' @rdname get_nodes | ||
get_sf_nodes <- function(){ | ||
function(layout) { | ||
nodes <- sf::st_as_sf(layout) | ||
attr(nodes, 'type_ggraph') <- 'node_ggraph' | ||
nodes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#' Place nodes on their geographical space | ||
#' | ||
#' This layout is built for objects of class `sfnetwork` and is meant to | ||
#' plot a graph on its geographical space, by extracting its X and Y coordinates | ||
#' | ||
#' @param graph An sfnetwork object | ||
#' @param circular ignored | ||
#' | ||
#' @importFrom tidygraph as_tbl_graph | ||
#' | ||
#' @return A data.frame with the columns `x`, `y`, `circular`. | ||
#' | ||
#' @family layout_tbl_graph_* | ||
layout_tbl_graph_sf <- function(graph, circular = FALSE) { | ||
# Check the presence of sf. | ||
check_installed("sf") | ||
# Check if network is an sfnetwork | ||
if (!inherits(graph, "sfnetwork")) { | ||
cli::cli_abort("The 'sf' layout needs an {.cls sfnetwork} graph.") | ||
} | ||
# Extract X and Y coordinates from the nodes | ||
graph <- activate(graph, "nodes") | ||
x <- sf::st_coordinates(graph)[,"X"] | ||
y <- sf::st_coordinates(graph)[,"Y"] | ||
# Create layout data frame | ||
nodes <- data_frame0(x = x, y = y, circular = FALSE) | ||
# Convert to tbl_graph to 'unstick' geometry column | ||
extra_data <- as_tibble(as_tbl_graph(graph), active = "nodes") | ||
nodes <- combine_layout_nodes(nodes, extra_data) | ||
attr(nodes, 'graph') <- graph | ||
nodes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.