From b75771d703835fc8d40ef8a76e0d48810fdca2e4 Mon Sep 17 00:00:00 2001 From: Andrea Manica Date: Fri, 20 Oct 2023 17:22:27 +0100 Subject: [PATCH] Update combining costs --- R/combineCosts.R | 53 ++++++++++++------------ R/setCosts.R | 74 ++++++++++++++++------------------ man/buffer.Rd | 34 ++++++++-------- man/closestNode.Rd | 17 ++++---- man/combineCosts.Rd | 4 +- man/dijkstra-methods.Rd | 15 ++++--- man/dropDeadEdges.Rd | 3 +- man/extractFromLayer.Rd | 12 +++--- man/findLand.Rd | 7 ++-- man/gData-class.Rd | 11 +++-- man/gGraph-class.Rd | 15 ++++--- man/geo.add.edges.Rd | 9 ++--- man/geo.change.attr.Rd | 15 ++++--- man/geoGraph-package.Rd | 13 +++--- man/getColors.Rd | 3 +- man/getCosts.Rd | 4 +- man/getEdges.Rd | 4 +- man/getNodesAttr.Rd | 1 - man/hgdp.Rd | 18 ++++----- man/isInArea.Rd | 10 ++--- man/makeGrid.Rd | 10 +++-- man/plot-gData.Rd | 19 +++++---- man/plot-gGraph.Rd | 19 +++++---- man/setCosts.Rd | 8 ++-- man/setDistCosts.Rd | 22 +++++----- man/worldgraph.Rd | 15 ++++--- man/zoom.Rd | 11 +++-- tests/testthat/test_setCosts.R | 5 ++- vignettes/geograph.Rmd | 2 +- 29 files changed, 208 insertions(+), 225 deletions(-) diff --git a/R/combineCosts.R b/R/combineCosts.R index fb96217..7bb563b 100644 --- a/R/combineCosts.R +++ b/R/combineCosts.R @@ -8,11 +8,11 @@ #' Note that costs are inversely proportional to connectivity between edges: #' the larger the cost associated to an edge, the lower the connectivity #' between the two concerned nodes.\cr -#' +#' #' Also note that 'costs' defined in \code{geoGraph} are equivalent to #' 'weights' as defined in \code{graph} and \code{RBGL} packages. -#' -#' @param x1 The firt gGraph (which will be used as a template to build the combined gGraph ) +#' +#' @param x1 The firt gGraph (which will be used as a template to build the combined gGraph) #' @param x2 The second gGraph from which costs will be combined #' @param method a character string indicating which method should be used to #' combined edge cost from the two gGraph. Currently available options are 'sum', @@ -27,8 +27,10 @@ #' data("worldgraph.40k") #' # new graph with custom cost function #' exp.cost <- function(x1, x2, cost.coeff) { -#' exp(-abs(x1 - x2) * cost.coeff) +#' exp(-abs(x1 - x2) * cost.coeff) #' } +#' # create a set of node costs +#' worldgraph.40k@nodes.attr$meanProd <- runif(n = 40962) #' new_costs_graph <- #' setCosts( #' worldgraph.40k, @@ -38,51 +40,50 @@ #' cost.coeff = 0.5 #' ) #' # combine costs from the original graph with the new costs -#' combine_costs_graph<- combineCosts(worldgraph.40k, new_costs_graph, method="sum") - +#' combine_costs_graph <- combineCosts(worldgraph.40k, new_costs_graph, method = "sum") ############### ## combineCosts ############### -combineCosts <- function(x1, x2, method=c("sum", "product", "function"), FUN=NULL,...){ +combineCosts <- function(x1, x2, method = c("sum", "product", "function"), FUN = NULL, ...) { ## some checks + argument handling - if(!is.gGraph(x1)) stop("x1 is not a valid gGraph object") - if(!is.gGraph(x2)) stop("x2 is not a valid gGraph object") - if(!hasCosts(x1)) stop("x1 is does not have costs; use setCosts to set the costs first") - if(!hasCosts(x2)) stop("x1 is does not have costs; use setCosts to set the costs first") + if (!is.gGraph(x1)) stop("x1 is not a valid gGraph object") + if (!is.gGraph(x2)) stop("x2 is not a valid gGraph object") + if (!hasCosts(x1)) stop("x1 is does not have costs; use setCosts to set the costs first") + if (!hasCosts(x2)) stop("x1 is does not have costs; use setCosts to set the costs first") method <- match.arg(method) ## get the edges and weights from teh two graphs myGraph1 <- getGraph(x1) edgeW1 <- edgeWeights(myGraph1) edgeL1 <- edgeL(myGraph1) - + myGraph2 <- getGraph(x2) edgeW2 <- edgeWeights(myGraph2) edgeL2 <- edgeL(myGraph2) - + # test that the two graphs have the same edges - if (!all(unlist(edgeL1)==unlist(edgeL2))){ + if (!all(unlist(edgeL1) == unlist(edgeL2))) { stop("the graphs differ in the edges they have") } - + newEdgeL <- list() - for(i in 1:length(edgeL1)){ + for (i in 1:length(edgeL1)) { newEdgeL[[i]] <- list() newEdgeL[[i]]$edges <- edgeL1[[i]]$edges - if (method=="sum"){ - newEdgeL[[i]]$weights <- edgeW1[[i]]+edgeW2[[i]] - } else if (method=="product"){ - newEdgeL[[i]]$weights <- edgeW1[[i]]*edgeW2[[i]] - } else if (method=="function"){ + if (method == "sum") { + newEdgeL[[i]]$weights <- edgeW1[[i]] + edgeW2[[i]] + } else if (method == "product") { + newEdgeL[[i]]$weights <- edgeW1[[i]] * edgeW2[[i]] + } else if (method == "function") { newEdgeL[[i]]$weights <- FUN(edgeW1[[i]], edgeW2[[i]], ...) } } - + names(newEdgeL) <- nodes(myGraph1) # items of the list must be named - - newGraph <- new("graphNEL", nodes=nodes(myGraph1), edgeL=newEdgeL) + + newGraph <- new("graphNEL", nodes = nodes(myGraph1), edgeL = newEdgeL) res <- x1 res@graph <- newGraph - + return(res) -} # end combineCosts \ No newline at end of file +} # end combineCosts diff --git a/R/setCosts.R b/R/setCosts.R index fe00575..4edf052 100644 --- a/R/setCosts.R +++ b/R/setCosts.R @@ -11,7 +11,7 @@ #' for the chosen attribute, which is associated to a costs (a friction). The #' cost of an edge is computed as a function (see argument \code{method}) of #' the costs of its nodes.\cr -#' +#' #' Note that costs are inversely proportional to connectivity between edges: #' the larger the cost associated to an edge, the lower the connectivity #' between the two concerned nodes.\cr @@ -55,35 +55,30 @@ #' x <- setCosts(x, attr.name = "habitat") #' plot(x, edges = TRUE) #' title("costs defined by habitat (land/land=1, other=100)") -#' -#' -setCosts <- function(x, attr.name=NULL, node.costs=NULL, method=c("mean", "product", "function"), FUN=NULL,...){ - ## some checks + argument handling - if(!is.gGraph(x)) stop("x is not a valid gGraph object") - method <- match.arg(method) - if ((method=="function") && (is.null(FUN))){ - stop("if method='function', FUN needs to be defined.") - } +#' +setCosts <- function(x, attr.name = NULL, node.costs = NULL, method = c("mean", "product", "function"), FUN = NULL, ...) { + ## some checks + argument handling + if (!is.gGraph(x)) stop("x is not a valid gGraph object") + method <- match.arg(method) + if ((method == "function") && (is.null(FUN))) { + stop("if method='function', FUN needs to be defined.") + } - ## assign costs to vertices - if(is.null(node.costs)){ # costs from a node attribute - nodeAttr <- unlist(getNodesAttr(x, attr.name=attr.name)) - if(!is.null(x@meta$costs)){ - if(!any(attr.name %in% colnames(x@meta$costs))) { - stop("attr.name is not documented in x@meta$costs.") - } - nodeCosts <- as.character(nodeAttr) - rules <- x@meta$costs - for(i in 1:nrow(x@meta$costs)){ - nodeCosts[nodeCosts==rules[i,attr.name]] <- rules[i,ncol(rules)] - } - nodeCosts <- as.numeric(nodeCosts) - } else stop("x@meta does not contain a 'costs' component.") - } else{ # cost directly provided - if(!is.numeric(node.costs)) stop("Provided 'node.costs' not numeric.") - node.costs <- rep(node.costs, length=length(getNodes(x))) # recycling node costs - nodeCosts <- node.costs - ## might add some more checks here... + ## assign costs to vertices + if (is.null(node.costs)) { # costs from a node attribute + nodeAttr <- unlist(getNodesAttr(x, attr.name = attr.name)) + if (!is.null(x@meta$costs)) { + if (!any(attr.name %in% colnames(x@meta$costs))) { + stop("attr.name is not documented in x@meta$costs.") + } + nodeCosts <- as.character(nodeAttr) + rules <- x@meta$costs + for (i in 1:nrow(x@meta$costs)) { + nodeCosts[nodeCosts == rules[i, attr.name]] <- rules[i, ncol(rules)] + } + nodeCosts <- as.numeric(nodeCosts) + } else { + stop("x@meta does not contain a 'costs' component.") } } else { # cost directly provided if (!is.numeric(node.costs)) stop("Provided 'node.costs' not numeric.") @@ -92,7 +87,6 @@ setCosts <- function(x, attr.name=NULL, node.costs=NULL, method=c("mean", "produ ## might add some more checks here... } - ## find costs of edges as a function of terminating vertices EL <- getGraph(x)@edgeL @@ -110,17 +104,17 @@ setCosts <- function(x, attr.name=NULL, node.costs=NULL, method=c("mean", "produ } } - ## method == function ## - if (method=="function"){ - for(i in 1:length(EL)){ - EL[[i]]$weights <- FUN(nodeCosts[i], nodeCosts[EL[[i]]$edges], ...) - } + ## method == function ## + if (method == "function") { + for (i in 1:length(EL)) { + EL[[i]]$weights <- FUN(nodeCosts[i], nodeCosts[EL[[i]]$edges], ...) } - - ## return result - newGraph <- new("graphNEL", nodes=getNodes(x), edgeL=EL) - res <- x - res@graph <- newGraph + } + + ## return result + newGraph <- new("graphNEL", nodes = getNodes(x), edgeL = EL) + res <- x + res@graph <- newGraph return(res) } # end setCosts diff --git a/man/buffer.Rd b/man/buffer.Rd index 82699b6..c7220ff 100644 --- a/man/buffer.Rd +++ b/man/buffer.Rd @@ -50,33 +50,33 @@ location they surround. #### gGraph example #### ## zoom in to an area -plot(worldgraph.10k, reset=TRUE) -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +plot(worldgraph.10k, reset = TRUE) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) ## identify one node -oneNodeXY <- c(getCoords(worldgraph.10k)[9299,1],getCoords(worldgraph.10k)[9299,2]) -points(oneNodeXY[1], oneNodeXY[2], col="red") +oneNodeXY <- c(getCoords(worldgraph.10k)[9299, 1], getCoords(worldgraph.10k)[9299, 2]) +points(oneNodeXY[1], oneNodeXY[2], col = "red") ## find some buffers buffer(worldgraph.10k, "9299", 100) # nothing around 100km buffer(worldgraph.10k, "9299", 500) -buf500km <- buffer(worldgraph.10k, "9299", 500, res="gGraph") -plot(buf500km, col.rules=buf500km@meta$buf.colors) -buf1000km <- buffer(worldgraph.10k, "9299", 1000, res="gGraph") -plot(buf1000km, col.rules=buf1000km@meta$buf.colors) +buf500km <- buffer(worldgraph.10k, "9299", 500, res = "gGraph") +plot(buf500km, col.rules = buf500km@meta$buf.colors) +buf1000km <- buffer(worldgraph.10k, "9299", 1000, res = "gGraph") +plot(buf1000km, col.rules = buf1000km@meta$buf.colors) #### gData example #### x <- hgdp[27:30] # retain a subset of hgdp -plot(x, reset=TRUE, col.g="lightgrey", pch.node=20) -buf.200 <- buffer(x, 200, res="gData") -buf.400 <- buffer(x, 400, res="gData") -buf.600 <- buffer(x, 600, res="gData") -buf.1000 <- buffer(x, 1000, res="gData") -points(buf.1000, col.node="black") -points(buf.600, col.node="yellow") -points(buf.400, col.node="gold") -points(buf.200, col.node="orange") +plot(x, reset = TRUE, col.g = "lightgrey", pch.node = 20) +buf.200 <- buffer(x, 200, res = "gData") +buf.400 <- buffer(x, 400, res = "gData") +buf.600 <- buffer(x, 600, res = "gData") +buf.1000 <- buffer(x, 1000, res = "gData") +points(buf.1000, col.node = "black") +points(buf.600, col.node = "yellow") +points(buf.400, col.node = "gold") +points(buf.200, col.node = "orange") title("Different buffers for a gData \n(100km, 200km, 500km)") } diff --git a/man/closestNode.Rd b/man/closestNode.Rd index f7dffca..fe340a7 100644 --- a/man/closestNode.Rd +++ b/man/closestNode.Rd @@ -68,33 +68,32 @@ that it is not possible to specify node attributes (\code{attr.names} and }} \examples{ - \dontrun{ ## interactive example ## -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## zooming in -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") ## click some locations -myNodes <- closestNode(worldgraph.10k,locator(), attr.name="habitat", attr.value="land") +myNodes <- closestNode(worldgraph.10k, locator(), attr.name = "habitat", attr.value = "land") myNodes ## here are the closestNodes -points(getCoords(worldgraph.10k)[myNodes,], col="red") +points(getCoords(worldgraph.10k)[myNodes, ], col = "red") } ## example with a gData object ## -myLoc <- list(x=c(3, -8, 11, 28), y=c(50, 57, 71, 67)) # some locations -obj <- new("gData", coords=myLoc) # new gData object +myLoc <- list(x = c(3, -8, 11, 28), y = c(50, 57, 71, 67)) # some locations +obj <- new("gData", coords = myLoc) # new gData object obj obj@gGraph.name <- "worldgraph.10k" # this could be done when creating obj -obj <- closestNode(obj, attr.name="habitat", attr.value="land") +obj <- closestNode(obj, attr.name = "habitat", attr.value = "land") ## plot the result (original location -> assigned node) -plot(obj, method="both", reset=TRUE) +plot(obj, method = "both", reset = TRUE) title("'x'=location, 'o'=assigned node") diff --git a/man/combineCosts.Rd b/man/combineCosts.Rd index c2725aa..925e3ac 100644 --- a/man/combineCosts.Rd +++ b/man/combineCosts.Rd @@ -7,7 +7,7 @@ combineCosts(x1, x2, method = c("sum", "product", "function"), FUN = NULL, ...) } \arguments{ -\item{x1}{The firt gGraph (which will be used as a template to build the combined gGraph )} +\item{x1}{The firt gGraph (which will be used as a template to build the combined gGraph)} \item{x2}{The second gGraph from which costs will be combined} @@ -44,6 +44,8 @@ data("worldgraph.40k") exp.cost <- function(x1, x2, cost.coeff) { exp(-abs(x1 - x2) * cost.coeff) } +# create a set of node costs +worldgraph.40k@nodes.attr$meanProd<-runif(n=40962) new_costs_graph <- setCosts( worldgraph.40k, diff --git a/man/dijkstra-methods.Rd b/man/dijkstra-methods.Rd index 3ebc6eb..dd0bfe6 100644 --- a/man/dijkstra-methods.Rd +++ b/man/dijkstra-methods.Rd @@ -89,28 +89,27 @@ In 'dijkstraBetween', paths are seeked all possible pairs of nodes between 'from' and 'to'. } \examples{ - \dontrun{ ## plotting world <- worldgraph.40k -par(mar=rep(.1,4)) -plot(world, reset=TRUE) +par(mar = rep(.1, 4)) +plot(world, reset = TRUE) ## check connectivity isConnected(hgdp) # must be ok ## Lowest cost path from an hypothetical origin -ori.coord <- list(33,10) # one given location long/lat -points(data.frame(ori.coord), pch="x", col="black", cex=3) # an 'x' shows the putative origin +ori.coord <- list(33, 10) # one given location long/lat +points(data.frame(ori.coord), pch = "x", col = "black", cex = 3) # an 'x' shows the putative origin ori <- closestNode(world, ori.coord) # assign it the closest node myPath <- dijkstraFrom(hgdp, ori) # compute shortest path ## plotting -plot(world,pch="") # plot the world -points(hgdp, lwd=3) # plot populations -points(data.frame(ori.coord), pch="x", col="black", cex=3) # add origin +plot(world, pch = "") # plot the world +points(hgdp, lwd = 3) # plot populations +points(data.frame(ori.coord), pch = "x", col = "black", cex = 3) # add origin plot(myPath) # plot the path } diff --git a/man/dropDeadEdges.Rd b/man/dropDeadEdges.Rd index 7d2ba00..2dc8d97 100644 --- a/man/dropDeadEdges.Rd +++ b/man/dropDeadEdges.Rd @@ -28,9 +28,8 @@ Dead nodes are nodes that are not connected to any other node, thus not having any role in the connectivity of a graph.\cr } \examples{ - \dontrun{ -plot(worldgraph.10k,reset=TRUE) +plot(worldgraph.10k, reset = TRUE) x <- dropDeadNodes(worldgraph.10k) plot(x) } diff --git a/man/extractFromLayer.Rd b/man/extractFromLayer.Rd index 08fb2ed..7eb940b 100644 --- a/man/extractFromLayer.Rd +++ b/man/extractFromLayer.Rd @@ -67,10 +67,9 @@ Nodes can be specified in different ways, including by providing a input formats. } \examples{ - \dontrun{ -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## see what info is available @@ -80,17 +79,16 @@ unique(worldshape@data$CONTINENT) ## retrieve continent info for all nodes ## (might take a few seconds) -x <- extractFromLayer(worldgraph.10k, layer=worldshape, attr="CONTINENT") +x <- extractFromLayer(worldgraph.10k, layer = worldshape, attr = "CONTINENT") x -table(getNodesAttr(x, attr.name="CONTINENT")) +table(getNodesAttr(x, attr.name = "CONTINENT")) ## subset Africa -temp <- getNodesAttr(x, attr.name="CONTINENT")=="Africa" +temp <- getNodesAttr(x, attr.name = "CONTINENT") == "Africa" temp[is.na(temp)] <- FALSE x <- x[temp] -plot(x, reset=TRUE) - +plot(x, reset = TRUE) } } diff --git a/man/findLand.Rd b/man/findLand.Rd index cc298ea..a513acd 100644 --- a/man/findLand.Rd +++ b/man/findLand.Rd @@ -55,8 +55,8 @@ Nodes can be specified either as a matrix of geographic coordinates, or as a ## create a new gGraph with random coordinates -myCoords <- data.frame(long=runif(1000,-180,180), lat=runif(1000,-90,90)) -obj <- new("gGraph", coords=myCoords) +myCoords <- data.frame(long = runif(1000, -180, 180), lat = runif(1000, -90, 90)) +obj <- new("gGraph", coords = myCoords) obj # note: no node attribute plot(obj) @@ -65,14 +65,13 @@ obj <- findLand(obj) obj # note: new node attribute ## define rules for colors -temp <- data.frame(habitat=c("land","sea"), color=c("green","blue")) +temp <- data.frame(habitat = c("land", "sea"), color = c("green", "blue")) temp obj@meta$color <- temp ## plot object with new colors plot(obj) - } \seealso{ \code{\link{extractFromLayer}}, to retrieve any information from a diff --git a/man/gData-class.Rd b/man/gData-class.Rd index 42deaf9..fcd8fb7 100644 --- a/man/gData-class.Rd +++ b/man/gData-class.Rd @@ -63,16 +63,15 @@ arguments: hgdp ## plot data -plot(worldgraph.40k, pch="") +plot(worldgraph.40k, pch = "") points(hgdp) ## subset and plot data -onlyNorth <- hgdp[hgdp@data$Latitude >0] # only northern populations - -plot(worldgraph.40k, reset=TRUE) -abline(h=0) # equator -points(onlyNorth, pch.node=20, cex=2, col.node="purple") +onlyNorth <- hgdp[hgdp@data$Latitude > 0] # only northern populations +plot(worldgraph.40k, reset = TRUE) +abline(h = 0) # equator +points(onlyNorth, pch.node = 20, cex = 2, col.node = "purple") } \seealso{ diff --git a/man/gGraph-class.Rd b/man/gGraph-class.Rd index dde6eab..ec782b8 100644 --- a/man/gGraph-class.Rd +++ b/man/gGraph-class.Rd @@ -65,10 +65,10 @@ new("gGraph") ## plotting the object -plot(rawgraph.10k, reset=TRUE) +plot(rawgraph.10k, reset = TRUE) ## zooming in -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") ## to play interactively with graphics, use: @@ -79,20 +79,19 @@ title("Europe") ## defining a new object restrained to visible nodes x <- rawgraph.10k[isInArea(rawgraph.10k)] -plot(x,reset=TRUE, edges=TRUE) +plot(x, reset = TRUE, edges = TRUE) title("x does just contain these visible nodes.") ## define weights for edges -x <- setCosts(x, attr.name="habitat", method="prod") -plot(x,edges=TRUE) +x <- setCosts(x, attr.name = "habitat", method = "prod") +plot(x, edges = TRUE) title("costs defined by habitat (land/land=1, other=100)") ## drop 'dead edges' (i.e. with weight 0) -x <- dropDeadEdges(x, thres=10) -plot(x,edges=TRUE) +x <- dropDeadEdges(x, thres = 10) +plot(x, edges = TRUE) title("after droping edges with null weight") - } \seealso{ Related classes are:\cr \% - \code{\linkS4class{graphNEL}} diff --git a/man/geo.add.edges.Rd b/man/geo.add.edges.Rd index 4d90b58..9197a72 100644 --- a/man/geo.add.edges.Rd +++ b/man/geo.add.edges.Rd @@ -33,21 +33,20 @@ edges (mode="points")\cr - select an area in which all edges from a reference graph are added (mode="area").\cr } \examples{ - \dontrun{ -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## zooming in -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") ## remove edges geo.remove.edges(worldgraph.10k) # points mode -geo.remove.edges(worldgraph.10k, mode="area") # area mode +geo.remove.edges(worldgraph.10k, mode = "area") # area mode ## add edges geo.add.edges(worldgraph.10k) # points mode -geo.add.edges(worldgraph.10k, mode="area") # area mode +geo.add.edges(worldgraph.10k, mode = "area") # area mode } } \seealso{ diff --git a/man/geo.change.attr.Rd b/man/geo.change.attr.Rd index 66e94b7..8c3eb7e 100644 --- a/man/geo.change.attr.Rd +++ b/man/geo.change.attr.Rd @@ -60,23 +60,22 @@ worldgraph.10k that are exclusively on land: this would be done by specifying \code{only.name="habitat"} and \code{only.value="land"}. } \examples{ - \dontrun{ -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## have to click here for an area ## all nodes are modified in the area -x <- geo.change.attr(worldgraph.10k, mode="area", attr.name="habitat", attr.value="fancy -habitat", newCol="pink") # modify selected area +x <- geo.change.attr(worldgraph.10k, mode = "area", attr.name = "habitat", attr.value = "fancy +habitat", newCol = "pink") # modify selected area -plot(x,reset=TRUE) # modification in the whole selected area +plot(x, reset = TRUE) # modification in the whole selected area ## have to click here for an area ## only nodes on land are modified -x <- geo.change.attr(x, mode="area", attr.name="habitat", attr.value="fancy2 -habitat", newCol="purple", only.name="habitat", only.value="land") +x <- geo.change.attr(x, mode = "area", attr.name = "habitat", attr.value = "fancy2 +habitat", newCol = "purple", only.name = "habitat", only.value = "land") -plot(x,reset=TRUE) # modification in the whole selected area +plot(x, reset = TRUE) # modification in the whole selected area } } diff --git a/man/geoGraph-package.Rd b/man/geoGraph-package.Rd index a01dd39..3b888b3 100644 --- a/man/geoGraph-package.Rd +++ b/man/geoGraph-package.Rd @@ -89,10 +89,10 @@ To cite geoGraph, please use the reference given by worldgraph.10k ## plotting the object -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## zooming in -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") ## to play interactively with graphics, use: @@ -103,20 +103,19 @@ title("Europe") ## defining a new object restrained to visible nodes x <- worldgraph.10k[isInArea(worldgraph.10k)] -plot(x,reset=TRUE, edges=TRUE) +plot(x, reset = TRUE, edges = TRUE) title("x does just contain these visible nodes.") ## define weights for edges -x <- setCosts(x, attr.name="habitat", method="prod") -plot(x,edges=TRUE) +x <- setCosts(x, attr.name = "habitat", method = "prod") +plot(x, edges = TRUE) title("connectivity defined by habitat (land/land=1, other=0)") ## drop 'dead edges' (i.e. with weight 0) x <- dropDeadEdges(x) -plot(x,edges=TRUE) +plot(x, edges = TRUE) title("after droping edges with null weight") - } \keyword{manip} \keyword{spatial} diff --git a/man/getColors.Rd b/man/getColors.Rd index 03da00a..7eca63f 100644 --- a/man/getColors.Rd +++ b/man/getColors.Rd @@ -55,8 +55,7 @@ worldgraph.10k # there is a node attribute 'habitat' worldgraph.10k@meta$color head(getNodes(worldgraph.10k)) -head(getColors(worldgraph.10k,res.type="vector", attr.name="habitat")) - +head(getColors(worldgraph.10k, res.type = "vector", attr.name = "habitat")) } \author{ diff --git a/man/getCosts.Rd b/man/getCosts.Rd index 41d103e..0d6df6d 100644 --- a/man/getCosts.Rd +++ b/man/getCosts.Rd @@ -66,8 +66,8 @@ connectivity there is between the couple of concerned nodes. }} \examples{ -head(getEdges(worldgraph.10k, res.type="matNames",unique=TRUE)) -head(getCosts(worldgraph.10k,res.type="vector",unique=TRUE)) +head(getEdges(worldgraph.10k, res.type = "matNames", unique = TRUE)) +head(getCosts(worldgraph.10k, res.type = "vector", unique = TRUE)) } diff --git a/man/getEdges.Rd b/man/getEdges.Rd index 0f9b22d..2e6dfa7 100644 --- a/man/getEdges.Rd +++ b/man/getEdges.Rd @@ -47,8 +47,8 @@ object using different possible outputs. example(gGraph) getEdges(x) -getEdges(x,res.type="matNames") -getEdges(x,res.type="matId") +getEdges(x, res.type = "matNames") +getEdges(x, res.type = "matId") } \seealso{ diff --git a/man/getNodesAttr.Rd b/man/getNodesAttr.Rd index 22167cf..737cea1 100644 --- a/man/getNodesAttr.Rd +++ b/man/getNodesAttr.Rd @@ -50,7 +50,6 @@ head(getNodesAttr(worldgraph.40k)) ## gData method getNodesAttr(hgdp) - } \seealso{ Most other accessors are documented in \linkS4class{gGraph} and diff --git a/man/hgdp.Rd b/man/hgdp.Rd index 8a0c986..32341f9 100644 --- a/man/hgdp.Rd +++ b/man/hgdp.Rd @@ -37,24 +37,24 @@ plot(hgdp) ## results from Handley et al. \dontrun{ ## Addis Ababa -addis <- list(lon=38.74,lat=9.03) -addis <- closestNode(worldgraph.40k,addis) # this takes a while +addis <- list(lon = 38.74, lat = 9.03) +addis <- closestNode(worldgraph.40k, addis) # this takes a while ## shortest path from Addis Ababa myPath <- dijkstraFrom(hgdp, addis) ## plot results -plot(worldgraph.40k, col=0) +plot(worldgraph.40k, col = 0) points(hgdp) -points(worldgraph.40k[addis], psize=3,pch="x", col="black") +points(worldgraph.40k[addis], psize = 3, pch = "x", col = "black") plot(myPath) ## correlations distance/genetic div. -geo.dist <- sapply(myPath[-length(myPath)],function(e) e$length) -gen.div <- getData(hgdp)[,"Genetic.Div"] -plot(gen.div~geo.dist) -lm1 <- lm(gen.div~geo.dist) -abline(lm1, col="blue") # this regression is wrong +geo.dist <- sapply(myPath[-length(myPath)], function(e) e$length) +gen.div <- getData(hgdp)[, "Genetic.Div"] +plot(gen.div ~ geo.dist) +lm1 <- lm(gen.div ~ geo.dist) +abline(lm1, col = "blue") # this regression is wrong summary(lm1) } diff --git a/man/isInArea.Rd b/man/isInArea.Rd index c8a5186..3f78143 100644 --- a/man/isInArea.Rd +++ b/man/isInArea.Rd @@ -89,10 +89,10 @@ the output are also available. }} \examples{ -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## zooming in -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") @@ -102,14 +102,14 @@ length(isInArea(worldgraph.10k)) sum(isInArea(worldgraph.10k)) head(which(isInArea(worldgraph.10k))) # which nodes are TRUE ? -head(isInArea(worldgraph.10k, res.type="integer")) # node indices +head(isInArea(worldgraph.10k, res.type = "integer")) # node indices -head(isInArea(worldgraph.10k, res.type="character")) # node names +head(isInArea(worldgraph.10k, res.type = "character")) # node names ## use isInArea to have a subset of visible nodes x <- worldgraph.10k[isInArea(worldgraph.10k)] -plot(x, reset=TRUE) +plot(x, reset = TRUE) } \author{ diff --git a/man/makeGrid.Rd b/man/makeGrid.Rd index c0057d2..73c57a8 100644 --- a/man/makeGrid.Rd +++ b/man/makeGrid.Rd @@ -39,18 +39,20 @@ curvature of the surface of the earth can be neglected. ## zoom in to a smaller area plot(worldgraph.10k) -geo.zoomin(c(-10,0, 50,54)) +geo.zoomin(c(-10, 0, 50, 54)) ## make a new gGraph newGraph <- makeGrid(1e3) newGraph <- findLand(newGraph) -newGraph@meta$colors <- data.frame(habitat=c("sea","land"), -color=c("blue","green")) +newGraph@meta$colors <- data.frame( + habitat = c("sea", "land"), + color = c("blue", "green") +) ## plot the new gGraph -plot(newGraph, reset=TRUE, edge=TRUE) +plot(newGraph, reset = TRUE, edge = TRUE) } \author{ diff --git a/man/plot-gData.Rd b/man/plot-gData.Rd index 7d7d08e..6b2675f 100644 --- a/man/plot-gData.Rd +++ b/man/plot-gData.Rd @@ -89,26 +89,25 @@ the \code{gData} plot.\cr \examples{ -myLoc <- list(x=c(3, -8, 11, 28), y=c(50, 57, 71, 67)) # some locations -obj <- new("gData", coords=myLoc) # new gData object +myLoc <- list(x = c(3, -8, 11, 28), y = c(50, 57, 71, 67)) # some locations +obj <- new("gData", coords = myLoc) # new gData object obj obj@gGraph.name <- "worldgraph.10k" -obj <- closestNode(obj, attr.name="habitat", attr.value="land") +obj <- closestNode(obj, attr.name = "habitat", attr.value = "land") ## plot the result (original location -> assigned node) -plot(obj, type="both",reset=TRUE) +plot(obj, type = "both", reset = TRUE) title("'x'=location, 'o'=assigned node") ## using different parameters -points(obj, type="both", pch.ori=2, col.ori="red", pch.nodes=20, col.nodes="pink") +points(obj, type = "both", pch.ori = 2, col.ori = "red", pch.nodes = 20, col.nodes = "pink") ## only nodes, fancy plot -plot(obj, col.nodes="red", cex=1, pch.node=20) -points(obj, col.nodes="red", cex=2) -points(obj, col.nodes="orange", cex=3) -points(obj, col.nodes="yellow", cex=4) - +plot(obj, col.nodes = "red", cex = 1, pch.node = 20) +points(obj, col.nodes = "red", cex = 2) +points(obj, col.nodes = "orange", cex = 3) +points(obj, col.nodes = "yellow", cex = 4) } \seealso{ diff --git a/man/plot-gGraph.Rd b/man/plot-gGraph.Rd index 5444c7f..f181cca 100644 --- a/man/plot-gGraph.Rd +++ b/man/plot-gGraph.Rd @@ -88,32 +88,31 @@ specify \code{reset=TRUE} as argument to \code{plot}. ## just the background -plot(worldgraph.10k,reset=TRUE,type="n") +plot(worldgraph.10k, reset = TRUE, type = "n") ## basic plot plot(worldgraph.10k) ## zooming and adding edges -geo.zoomin(list(x=c(90,150),y=c(0,-50))) -plot(worldgraph.10k, edges=TRUE) +geo.zoomin(list(x = c(90, 150), y = c(0, -50))) +plot(worldgraph.10k, edges = TRUE) ## display edges differently -plotEdges(worldgraph.10k, col="red", lwd=2) +plotEdges(worldgraph.10k, col = "red", lwd = 2) ## replot points with different color -points(worldgraph.10k, col="orange") +points(worldgraph.10k, col = "orange") ## mask points in the sea -inSea <- unlist(getNodesAttr(worldgraph.10k,attr.name="habitat"))=="sea" +inSea <- unlist(getNodesAttr(worldgraph.10k, attr.name = "habitat")) == "sea" head(inSea) -points(worldgraph.10k[inSea], col="white", sticky=TRUE) # this will stay +points(worldgraph.10k[inSea], col = "white", sticky = TRUE) # this will stay ## but better, only draw those on land, and use a fancy setup -par(bg="blue") -plot(worldgraph.10k[!inSea], bg.col="darkgreen", col="purple", edges=TRUE) - +par(bg = "blue") +plot(worldgraph.10k[!inSea], bg.col = "darkgreen", col = "purple", edges = TRUE) } \seealso{ diff --git a/man/setCosts.Rd b/man/setCosts.Rd index 1bd598e..8c90750 100644 --- a/man/setCosts.Rd +++ b/man/setCosts.Rd @@ -54,18 +54,18 @@ Also note that 'costs' defined in \code{geoGraph} are equivalent to } \examples{ -plot(rawgraph.10k, reset=TRUE) +plot(rawgraph.10k, reset = TRUE) ## zooming in -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") ## defining a new object restrained to visible nodes x <- rawgraph.10k[isInArea(rawgraph.10k)] ## define weights for edges -x <- setCosts(x, attr.name="habitat") -plot(x,edges=TRUE) +x <- setCosts(x, attr.name = "habitat") +plot(x, edges = TRUE) title("costs defined by habitat (land/land=1, other=100)") diff --git a/man/setDistCosts.Rd b/man/setDistCosts.Rd index 8e31a19..1005258 100644 --- a/man/setDistCosts.Rd +++ b/man/setDistCosts.Rd @@ -40,19 +40,19 @@ connectivity there is between the couple of concerned nodes. }} \examples{ -if(require(fields)){ -## load data -plot(rawgraph.10k,reset=TRUE) -geo.zoomin(list(x=c(110,150),y=c(-10,-40))) -plotEdges(rawgraph.10k) +if (require(fields)) { + ## load data + plot(rawgraph.10k, reset = TRUE) + geo.zoomin(list(x = c(110, 150), y = c(-10, -40))) + plotEdges(rawgraph.10k) -## compute costs -x <- rawgraph.10k[isInArea(rawgraph.10k)] -x <- setDistCosts(x) + ## compute costs + x <- rawgraph.10k[isInArea(rawgraph.10k)] + x <- setDistCosts(x) -## replot edges -plotEdges(x) # no big differences can be seen -head(getCosts(x)) + ## replot edges + plotEdges(x) # no big differences can be seen + head(getCosts(x)) } } diff --git a/man/worldgraph.Rd b/man/worldgraph.Rd index 5b91620..4ae5fc9 100644 --- a/man/worldgraph.Rd +++ b/man/worldgraph.Rd @@ -30,10 +30,10 @@ world, with respective resolutions of 10,242 and 40,962 vertices.\cr references.\cr 'worldgraph's are 'rawgraph's that have been modified manually to rectify -connectivity between edges at some places. The most noticable change is that +connectivity between edges at some places. The most noticeable change is that all edges involving sea vertices have been removed.\cr -'worldshape' is a shapefile of contries of the world (snapshot from 1994). +'worldshape' is a shapefile of countries of the world (snapshot from 1994). } \examples{ @@ -41,24 +41,24 @@ all edges involving sea vertices have been removed.\cr worldgraph.10k ## plotting the object -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) title("Hello world") ## zooming in -geo.zoomin(list(x=c(-12,45), y=c(33,75))) +geo.zoomin(list(x = c(-12, 45), y = c(33, 75))) title("Europe") -geo.zoomin(list(x=c(-12,2), y=c(50,60))) +geo.zoomin(list(x = c(-12, 2), y = c(50, 60))) plotEdges(worldgraph.10k) title("United Kingdom") ## zooming out # geo.zoomout() # needs clicking on device -geo.zoomin(list(x=c(-6,38), y=c(35,73))) +geo.zoomin(list(x = c(-6, 38), y = c(35, 73))) title("Europe") ## defining the subset of visible points x <- worldgraph.10k[isInArea(worldgraph.10k)] -plot(x,reset=TRUE, edges=TRUE) +plot(x, reset = TRUE, edges = TRUE) title("One subsetted object.") \dontrun{ @@ -66,7 +66,6 @@ title("One subsetted object.") geo.zoomin() } - } \references{ === On the construction of the graph ===\cr Randall, D. A.; diff --git a/man/zoom.Rd b/man/zoom.Rd index 0eda369..c5b66cf 100644 --- a/man/zoom.Rd +++ b/man/zoom.Rd @@ -50,16 +50,15 @@ Whenever clicking is needed, a right-click will stop the function. } \examples{ -plot(worldgraph.10k, reset=TRUE) +plot(worldgraph.10k, reset = TRUE) ## zooming in -x.ini <- c(-100,-60) -y.ini <- c(-30,30) -for(i in 0:3){ -geo.zoomin(list(x=x.ini + i*60, y=y.ini)) +x.ini <- c(-100, -60) +y.ini <- c(-30, 30) +for (i in 0:3) { + geo.zoomin(list(x = x.ini + i * 60, y = y.ini)) } - \dontrun{ ## going back geo.back() # you have to click ! diff --git a/tests/testthat/test_setCosts.R b/tests/testthat/test_setCosts.R index ad633f4..299d65f 100644 --- a/tests/testthat/test_setCosts.R +++ b/tests/testthat/test_setCosts.R @@ -6,6 +6,7 @@ test_that("arbitrary function to set costs", { exp.cost <- function(x1, x2, cost.coeff) { exp(-abs(x1 - x2) * cost.coeff) } + worldgraph.40k@nodes.attr$meanProd <- runif(graph::numNodes(getGraph(worldgraph.40k))) my_coeff <- 0.5 test_graph <- setCosts( @@ -15,7 +16,7 @@ test_that("arbitrary function to set costs", { FUN = exp.cost, cost.coeff = my_coeff ) - #now check that we have the right costs + # now check that we have the right costs sample_edge <- names(test_graph@graph@edgeData@data)[1] sample_nodes <- as.integer(strsplit(sample_edge, "|", fixed = TRUE)[[1]]) sample_meanProd <- worldgraph.40k@nodes.attr$meanProd[sample_nodes] @@ -23,4 +24,4 @@ test_that("arbitrary function to set costs", { test_graph@graph@edgeData@data[[1]]$weight, exp.cost(sample_meanProd[1], sample_meanProd[2], cost.coeff = my_coeff) ) -}) \ No newline at end of file +}) diff --git a/vignettes/geograph.Rmd b/vignettes/geograph.Rmd index cfe63a6..308f13d 100644 --- a/vignettes/geograph.Rmd +++ b/vignettes/geograph.Rmd @@ -11,7 +11,7 @@ vignette: > ```{r setup, echo=FALSE} -#knitr::opts_chunk$set(fig.width = 7, fig.height = 6) +# knitr::opts_chunk$set(fig.width = 7, fig.height = 6) options(digits = 4) ```