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

Preserving string-ness #131

Closed
mailund opened this issue May 10, 2018 · 3 comments
Closed

Preserving string-ness #131

mailund opened this issue May 10, 2018 · 3 comments

Comments

@mailund
Copy link

mailund commented May 10, 2018

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.

@dmi3kno
Copy link

dmi3kno commented May 10, 2018

It seems like some of the underlying functions use as.data.frame() without specifying stringsAsFactors=FALSE. When you run options(stringsAsFactors = FALSE) before your sample code, text in edge labels (and text in node labels if any) is returned as character.

@thomasp85
Copy link
Owner

Thanks - this seems like an oversight I should get fixed

@mailund
Copy link
Author

mailund commented May 11, 2018

Adding stringsAsFactors = FALSE to the two as.data.frame calls in edges.R fixed the issue, see #132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants