Skip to content

Instantly share code, notes, and snippets.

@sebastiansauer
Last active June 12, 2017 17:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastiansauer/1ba64a684f55d6dec2782ed07b93affa to your computer and use it in GitHub Desktop.
Save sebastiansauer/1ba64a684f55d6dec2782ed07b93affa to your computer and use it in GitHub Desktop.
Create a graph with DiagrammeR, easy example
# Plot path diagram with {DiagrammeR}
data(Wage, package = "ISLR") #load data
lm_wage <- lm(wage ~ health + age + health:age, data = Wage) # run linear model
summary(lm_wage)
library(semPlot) # plot path diagram with {semPlot}
semPaths(lm_wage, whatLabel = "est", vsize.man = 16, edge.label.cex=0.6)
library(DiagrammeR) # now plot with {DiagrammeR}
# define edges:
edges_set <- data.frame(
from = c("ref", "age", "ref_age", "error"),
to = c("wage", "wage", "wage", "wage"),
stlye = c("solid", "solid", "solid", "dashed"),
label = c("-1.8ns"," 0.51***", "0.43**", "78.66***")
)
# define nodes:
nodes_set <- data.frame(
nodes = c("ref", "age", "ref_age", "wage", "error"),
shape = c("square", "square", "circle", "square", "square")
)
# define visual attributes of nodes
node_attrs <- c("style = filled", "fillcolor = '#00998a'",
"color = gray", "shape = circle", "fontname = Helvetica",
"width = 1")
# now define graph object
my_graph <- create_graph(
nodes = nodes_set,
edges_df = edges_set,
node_attrs = node_attrs,
graph_attrs = "rankdir = LR")
# print it
render_graph(my_graph, width = 500, height = 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment