Skip to content

Commit

Permalink
removed deprecated fcn
Browse files Browse the repository at this point in the history
ggplot2::fortify() has been deprecated. replaced with a function to avoid including package `sf` as a dependency
  • Loading branch information
embruna committed Aug 14, 2024
1 parent 1e1dbb2 commit e8011a5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
40 changes: 34 additions & 6 deletions R/plot_net_address.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,31 @@ plot_net_address <- function(data,
# )
)

world_map_sub <- world_map
#world_map_sub <- ggplot2::fortify(world_map)

sf_convert <- function(map) {
do.call("rbind",
lapply(map@polygons, function(x) {
do.call("rbind",
lapply(seq_along(x@Polygons), function(i) {
df <- setNames(as.data.frame(x@Polygons[[i]]@coords), c("long", "lat"))
df$order <- 1:nrow(df)
df$hole <- x@Polygons[[i]]@hole
df$piece <- i
df$id <- x@ID
df$group <- paste(x@ID, i, sep = '.')
df
}))
})
)
}

world_map_sub <- sf_convert(world_map) # and now this instead of fortify()
# world_map_sub <- ggplot2::fortify(world_map) # fortify() was deprecated
# world_map_sub <- sf::st_as_sf(world_map) st_as_sf() is an alternative,
# but the output includes all the polygons needed to map in a list-column.
# This function converts the map `st` object to a data frame like fortify().


if (mapRegion != "world") {
world_map_sub <- world_map[which(world_map$continent == mapRegion &
world_map$TYPE != "Dependency"), ]
Expand All @@ -178,10 +201,15 @@ plot_net_address <- function(data,
# world_map@data, by = "id")

## calculate min and max for plot
latmin <- world_map_sub@bbox["y", "min"]
latmax <- world_map_sub@bbox["y", "max"]
longmin <- world_map_sub@bbox["x", "min"]
longmax <- world_map_sub@bbox["x", "max"]
latmin <- min(world_map.df$lat)
latmax <- max(world_map.df$lat)
longmin <- min(world_map.df$lon)
longmax <- max(world_map.df$lon)
# latmin <- world_map_sub@bbox["y", "min"]
# latmin <- world_map_sub@bbox["y", "min"]
# latmax <- world_map_sub@bbox["y", "max"]
# longmin <- world_map_sub@bbox["x", "min"]
# longmax <- world_map_sub@bbox["x", "max"]

if (mapRegion == "Australia"){
longmin <- 100
Expand Down
33 changes: 29 additions & 4 deletions R/plot_net_country.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#' @export plot_net_country
#' @importFrom network %v%

# data<-BITR_geocode
plot_net_country <- function(data,
lineResolution = 10,
mapRegion = "world",
Expand Down Expand Up @@ -137,7 +138,7 @@ plot_net_country <- function(data,
requireNamespace(package = "network", quietly = TRUE)

vertex_names <- (linkages_countries_net %v% "vertex.names")
#convert to tibble to use case_when
# convert to tibble to use case_when
# vertex_names<-as_tibble(vertex_names)


Expand Down Expand Up @@ -334,12 +335,36 @@ plot_net_country <- function(data,
}
## Create the world outlines:
world_map@data$id <- rownames(world_map@data)
world_map.points <- ggplot2::fortify(world_map)
# world_map.points <- ggplot2::fortify(world_map) # fortify() was deprecated
# world_map.points <- sf::st_as_sf(world_map) st_as_sf() is an alternative,
# but the output includes all the polygons needed to map in a list-column.
# This function converts the map `st` object to a data frame like fortify().

sf_convert <- function(map) {
do.call("rbind",
lapply(map@polygons, function(x) {
do.call("rbind",
lapply(seq_along(x@Polygons), function(i) {
df <- setNames(as.data.frame(x@Polygons[[i]]@coords), c("long", "lat"))
df$order <- 1:nrow(df)
df$hole <- x@Polygons[[i]]@hole
df$piece <- i
df$id <- x@ID
df$group <- paste(x@ID, i, sep = '.')
df
}))
})
)
}

world_map.points <- sf_convert(world_map) # and now this instead of fortify()


world_map.df <- merge(world_map.points,
world_map@data, by = "id", all = TRUE)
world_map@data, by = "id", all = TRUE)
world_map.df <- world_map.df[!is.na(world_map.df$lat), ]
# world_map.df <- dplyr::full_join(world_map.points,
# world_map@data, by = "id")
# world_map@data, by = "id")

## calculate min and max for plot
latmin <- world_map_sub@bbox["y", "min"]
Expand Down

0 comments on commit e8011a5

Please sign in to comment.