@@ -166,19 +166,20 @@ setMethod("GraphSpace", signature(g = "ANY"),
166166 error = function (e ) NULL
167167 )
168168 g <- attr(g , " graph" )
169+ } else if (inherits(g , " tbl_graph" )) {
170+ g <- tidygraph :: as.igraph(g )
169171 }
170172
171- if (inherits(g , " igraph" )) {
172- class(g ) <- " igraph"
173- } else {
173+ if (! inherits(g , " igraph" )) {
174174 rlang :: abort(
175175 message = c(
176176 " x" = " Input 'g' must inherit from the 'igraph' class." ,
177- " i" = paste(" Received an object of class:" , paste(class(g ), collapse = " /" )),
178- " *" = " Did you mean to use `tidygraph::as_tbl_graph()`?"
177+ " i" = " Input must be an 'igraph', 'tbl_graph', or 'layout_ggraph'."
179178 )
180179 )
181180 }
181+ class(g ) <- " igraph"
182+
182183 if (! is.null(layout )){
183184 .validate_gs_args(" numeric_mtx" , " layout" , layout )
184185 if (ncol(layout ) != 2 ) {
@@ -232,6 +233,9 @@ setMethod("GraphSpace", signature(g = "data.frame"),
232233
233234 # Initialize a graph using 'coord' as vertices, with no edges
234235 g <- make_empty_graph(n = nrow(coord ), directed = FALSE )
236+
237+ # If 'coord' has no row names (a common case for a plain data.frame),
238+ # sequential names will be later assigned in the validation functions
235239 if (! is.null(rownames(coord ))){
236240 V(g )$ name <- rownames(coord )
237241 }
@@ -266,12 +270,12 @@ setMethod("GraphSpace", signature(g = "data.frame"),
266270# ' @param xlab The title for the 'x' axis of a 2D-image space.
267271# ' @param ylab The title for the 'y' axis of a 2D-image space.
268272# ' @param font.size A single numeric value passed to ggplot themes.
269- # ' @param bg.color A single color for background.
273+ # ' @param bg.colour A single color for background.
270274# ' @param add.labels A logical value indicating whether to plot vertex labels.
271275# ' @param node.labels A vector of vertex names to be highlighted in the graph
272276# ' space. This argument overrides 'add.labels'.
273277# ' @param label.size A size argument passed to \code{\link[ggplot2]{geom_text}}.
274- # ' @param label.color A color passed to \code{\link[ggplot2]{geom_text}}.
278+ # ' @param label.colour A color passed to \code{\link[ggplot2]{geom_text}}.
275279# ' @param add.image A logical value indicating whether to add a background
276280# ' image, when one is available (see \code{\link{GraphSpace}}).
277281# ' @param raster A logical value indicating whether to rasterize the main plot.
@@ -319,9 +323,9 @@ setMethod("GraphSpace", signature(g = "data.frame"),
319323setMethod ("plotGraphSpace ", "GraphSpace",
320324 function (gs , theme = " th0" , xlab = " Graph coordinates 1" ,
321325 ylab = " Graph coordinates 2" , font.size = 1 ,
322- bg.color = " grey95" , add.labels = FALSE ,
326+ bg.colour = " grey95" , add.labels = FALSE ,
323327 node.labels = NULL , label.size = 3 ,
324- label.color = " grey20" , add.image = TRUE ,
328+ label.colour = " grey20" , add.image = TRUE ,
325329 raster = FALSE , dpi = 300 , dev = " cairo_png" ) {
326330
327331 gs <- updateGraphSpace(gs )
@@ -330,10 +334,10 @@ setMethod("plotGraphSpace", "GraphSpace",
330334 .validate_gs_args(" singleString" , " xlab" , xlab )
331335 .validate_gs_args(" singleString" , " ylab" , ylab )
332336 .validate_gs_args(" singleNumber" , " font.size" , font.size )
333- .validate_gs_colors(" singleColor" , " bg.color " , bg.color )
337+ .validate_gs_colors(" singleColor" , " bg.colour " , bg.colour )
334338 .validate_gs_args(" singleLogical" , " add.labels" , add.labels )
335339 .validate_gs_args(" singleNumber" , " label.size" , label.size )
336- .validate_gs_colors(" singleColor" , " label.color " , label.color )
340+ .validate_gs_colors(" singleColor" , " label.colour " , label.colour )
337341 .validate_gs_args(" singleLogical" , " add.image" , add.image )
338342 .validate_gs_args(" singleLogical" , " raster" , raster )
339343 .validate_gs_args(" singleInteger" , " dpi" , dpi )
@@ -362,7 +366,7 @@ setMethod("plotGraphSpace", "GraphSpace",
362366 ggi <- ggi + theme_gspace_coords(theme = theme ,
363367 is_norm = pars $ is.normalized , xlab = xlab , ylab = ylab ,
364368 txt_size = font.size , leg_size = font.size ,
365- bg_color = bg.color )
369+ bg_colour = bg.colour )
366370 }
367371 }
368372
@@ -372,7 +376,7 @@ setMethod("plotGraphSpace", "GraphSpace",
372376 # --- add node labels
373377 if (! is.null(node.labels )){
374378 ggp <- .add_labels1(ggp , nodes , node.labels ,
375- label.size , label.color )
379+ label.size , label.colour )
376380 } else if (add.labels ){
377381 ggp <- .add_labels2(ggp , nodes )
378382 }
@@ -381,7 +385,7 @@ setMethod("plotGraphSpace", "GraphSpace",
381385 ggp <- ggp + theme_gspace_coords(theme = theme ,
382386 is_norm = pars $ is.normalized , xlab = xlab , ylab = ylab ,
383387 txt_size = font.size , leg_size = font.size ,
384- bg_color = bg.color )
388+ bg_colour = bg.colour )
385389
386390 if (raster ){
387391 ggp <- ggrastr :: rasterize(ggp , layers = " GraphSpace" ,
@@ -440,7 +444,7 @@ plot.GraphSpace <- function(x, ...) {
440444# -------------------------------------------------------------------------------
441445# ' @title Accessors for fetching slots from a GraphSpace object
442446# '
443- # ' @description \code{getGraphSpace} retrives information from
447+ # ' @description \code{getGraphSpace} retrieves information from
444448# ' individual slots available in a GraphSpace object.
445449# '
446450# ' @param gs A preprocessed \linkS4class{GraphSpace} class object
@@ -458,7 +462,7 @@ plot.GraphSpace <- function(x, ...) {
458462# ' # Create a new GraphSpace object
459463# ' gs <- GraphSpace(gtoy1)
460464# '
461- # ' # Get the 'summary ' slot in gs
465+ # ' # Get the 'graph ' slot in gs
462466# ' getGraphSpace(gs, what = 'graph')
463467# '
464468# ' @import methods
@@ -835,8 +839,7 @@ setMethod("gs_edge_attr<-", "GraphSpace", function(x, name, ..., value) {
835839 x @ graph <- .validate_igraph(g )
836840 nodes <- .get_nodes(x @ graph )
837841 if (x @ pars $ is.normalized ) {
838- nodes $ x <- x @ nodes $ x
839- nodes $ y <- x @ nodes $ y
842+ nodes [x @ nodes $ name , c(" x" ," y" )] <- x @ nodes [, c(" x" ," y" )]
840843 }
841844 x @ nodes <- nodes
842845 return (x )
0 commit comments