library(sf)
<- st_read("data/ne_10m_rivers_lake_centerlines") multilines_sf
library(tidyverse)
# Cast MULTILINESTRING to LINESTRING
<- multilines_sf %>%
linestrings_sf st_cast("LINESTRING",
do_split = TRUE) %>%
# Ensure splitting into LINESTRINGs, not POINTs
filter(st_geometry_type(geometry) == "LINESTRING")
library(sfnetworks)
# Convert to sfnetwork
<- sfnetworks::as_sfnetwork(linestrings_sf, directed = FALSE) network
# Extract edges as an sf object
<- network %>%
edges_sf activate("edges") %>%
st_as_sf()
%>% names edges_sf
%>%count(featurecla) edges_sf
# Extract nodes as an sf object
<- network %>%
nodes_sf activate("nodes") %>%
st_as_sf()
%>%names nodes_sf
<- sfnetworks::as_sfnetwork(network) gr
library(ggraph)
ggraph(gr, 'sf') +
geom_edge_sf(aes(color = name_en),
show.legend = F) +
geom_node_sf(size = 0.3)
ggraph(gr, 'sf') +
geom_edge_sf(aes(color = scalerank),
show.legend = T) +
geom_node_sf(size = 0.3)
ggraph(gr, 'sf') +
geom_edge_sf(aes(color = dissolve),
show.legend = F) +
geom_node_sf(size = 0.1)
<- ggraph(gr, 'sf') +
map geom_edge_sf(aes(color = featurecla),
show.legend = T) +
scale_edge_color_manual(values = c("red","navy","#50c8c6","brown")) +
::theme_map()
ggthemes
map
+
map # add a north arrow
::annotation_north_arrow(location = "br") +
ggspatial# add a title and a caption
labs(title = "Rivers Lines on World Map",
caption = "Source: Natural Earth | #30DayMapChallenge 2024 Day2 | @fgazzelloni") +
# adjust the appearance of the plot
theme(text = element_text(family = "Courier"),
plot.title = element_text(size = 16,
color = "grey20", face = "bold"),
plot.caption = element_text(size = 8,
color = "grey20", face = "bold"),
plot.background = element_rect(fill = NA, color = NA),
panel.background = element_rect(fill = NA, color = NA))