Skip to content

Preserving string-ness #131

@mailund

Description

@mailund

I don't know if this is an error or not, but is there a way to prevent character vectors in mappings to be turned into factors?

In the code below, I create a small graph and I provide a string vector for edge.id (and another, other_string, in case edge.id is treated specially (which doesn't seem to be the case)). I want to be able to map edge ids to other labels in a table, so I would like the vector(s) to be strings that I can access inside the aesthetics. I don't use a table in the example but a function so I can print the parameter, and that shows that these string vectors have been turned into factors.

library(tibble)
library(tidygraph)
library(ggraph)
nodes <- tribble(
    ~label,
    "foo",
    "bar",
    "baz",
)
edges <- tribble(
    ~from, ~to, ~edge.id, ~other_string,
    "foo", "bar", "foobar", "qux",
    "foo", "baz", "foobaz", "qax"
)
tg <- tbl_graph(nodes = nodes, edges = edges)
tg

map_id <- function(ids) {
    print(ids)
    as.character(ids)
}
ggraph::ggraph(tg) +
    ggraph::geom_edge_link(
        aes(label = map_id(edge.id))
    )
ggraph::ggraph(tg) +
    ggraph::geom_edge_link(
        aes(label = map_id(other_string))
    )

I can, of course, turn them back into strings, but that seems like a roundabout way to do this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions