0

I have code that generates diagram with diagrammer library in R. For data source I use xlsx file that you can download at this link

library("xlsx")
df <-xlsx::read.xlsx("animals.xlsx", sheetIndex = 1, header = TRUE, encoding = "UTF-8")

With first lines I'm importing a file in to R. Then using col1 and col2 to create a diagram

uniquenodes <- unique(c(df$col1, df$col2))

library(DiagrammeR)

nodes <- create_nodes(nodes=seq(uniquenodes), type="number", label=uniquenodes)
edges <- create_edges(from=match(df$col1, uniquenodes), to=match(df$col2, uniquenodes), rel="related")
g <- create_graph(nodes_df=nodes, edges_df=edges)
render_graph(g)

After code is used I get this picture:

enter image description here

When it should look like this:

enter image description here

Deividas Kiznis
  • 431
  • 1
  • 6
  • 20
  • what version of `DiagrammeR` are you using? (`packageVersion("DiagrammeR")`. When I use version 0.8.1, your code works just fine. When using the most recent version (0.9.0) your code fails entirely as the API has changed somewhat. Also, when I say it works find under 0.8.1, I'm assuming that I've recreated your data structure correctly. You haven't actually shown us what is in `df`, so we can't exactly reproduce your results. – Benjamin Feb 06 '17 at 19:11
  • Every time I use DiagrammeR I get this warning message "package ‘DiagrammeR’ was built under R version 3.3.2 " and I use 3.3.1 version. My df is xlsx file that you can download I have left a link – Deividas Kiznis Feb 06 '17 at 19:18
  • Aha! Try adding `stringsAsFactors = FALSE` to your `read.xlsx` call. – Benjamin Feb 06 '17 at 19:23
  • I see can you give me example of how to use this line `stringsAsFactors = FALSE` – Deividas Kiznis Feb 06 '17 at 19:33

1 Answers1

1

Short Answer

Change your data import code to:

df <-xlsx::read.xlsx("animals.xlsx", 
                     sheetIndex = 1, 
                     header = TRUE, 
                     encoding = "UTF-8",
                     stringsAsFactors = FALSE)

Long Explanation

It seems that your columns are actually factors, not characters. I will recreate your data frame here (I'm not using the xlsx package because I'm too lazy to make the system changes necessary to get it to install)

df <- data.frame(col1 = c("Cat", "Dog", "Bird"),
                 col2 = c("Feline", "Canis", "Avis"))
uniquenodes <- unique(c(df$col1, df$col2))

uniquenodes
[1] 2 3 1

Using your code when the columns are factors results in the figure you obtained. Your code will work as desired if the columns are character strings. Notice the difference here:

df <- data.frame(col1 = c("Cat", "Dog", "Bird"),
                 col2 = c("Feline", "Canis", "Avis"),
                 stringsAsFactors = FALSE)
uniquenodes <- unique(c(df$col1, df$col2))

uniquenodes

library(DiagrammeR)


nodes <- create_nodes(nodes=seq(uniquenodes), type="number", label=uniquenodes)
edges <- create_edges(from=match(df$col1, uniquenodes), to=match(df$col2, uniquenodes), rel="related")
g <- create_graph(nodes_df=nodes, edges_df=edges)
render_graph(g)
Benjamin
  • 16,897
  • 6
  • 45
  • 65