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

Fixes network layouts where x or y are constant #32

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions R/fortify-network.R
Expand Up @@ -126,8 +126,8 @@ fortify.network <- function(model, data = NULL,
names(nodes) = c("x", "y")

# rescale coordinates
nodes$x = scale(nodes$x, center = min(nodes$x), scale = diff(range(nodes$x)))
nodes$y = scale(nodes$y, center = min(nodes$y), scale = diff(range(nodes$y)))
nodes$x = scale2(nodes$x)
nodes$y = scale2(nodes$y)

# import vertex attributes
for (y in network::list.vertex.attributes(x)) {
Expand Down
14 changes: 14 additions & 0 deletions R/utilities.R
Expand Up @@ -35,3 +35,17 @@ theme_facet <- function(base_size = 12, base_family = "", ...) {
...
)
}

#' A new scale function
#'
#' Appropriately handles the case for scaling if a column is constant
#' @param xx The value to scale
scale2 <- function(xx){
s <- diff(range(xx))
if(s==0){
res <- rep(0.5, length.out=length(xx))
}else{
res <- scale(xx, center = min(xx), scale = s)
}
return(res)
}